2016-08-26 16 views
2

通常、ggplot2の連続変数の縮尺をscale_y_continuous(またはscale_x_continuous)に変更しました。私は通常、coord_cartesian(xlim=)(またはy軸の場合はylim=)を使用して軸の限界を設定します。以下のようにうまく動作するすべての次手動でggplot2のx軸とy軸にブレークを追加する

enter image description here

私は、x軸と言う、とを調整したい場合は、1-:デフォルトのスケールでは、次のプロットを作成し

#Some example data 
set.seed(100) 
b<-sample(1:10, 10) 
a<-sample(1:10, 10) 

df<-data.frame(a,b) 

#Graph without scales set (just default) 
library(ggplot2) 
ggplot(data=df, aes(a,b))+ 
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+ 
    geom_text(aes(label=rownames(df)), color="black") 

13(すなわち13目盛り1から始まり、13で終わる)私は次の操作を行います:

ggplot(data=df, aes(a,b))+ 
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+ 
    geom_text(aes(label=rownames(df)), color="black")+ 
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+ 
    coord_cartesian(xlim=c(0, 13)) 

enter image description here

をしかし

...とy軸のための...

ggplot(data=df, aes(a,b))+ 
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+ 
    geom_text(aes(label=rownames(df)), color="black")+ 
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+ 
    coord_cartesian(ylim=c(0, 13)) 

enter image description here

、私は同時に両方の軸のための非常に同じスケール調整を達成しようとすると、出力はありません私が期待しているもの(x軸とy軸の両方で1-13)を生成しません。

ggplot(data=df, aes(a,b))+ 
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+ 
    geom_text(aes(label=rownames(df)), color="black")+ 
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+ 
    coord_cartesian(ylim=c(0, 13))+ 
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+ 
    coord_cartesian(xlim=c(0, 13)) 

enter image description here

あなたが見ることができるように、x及びy軸は、正確に同じコードが両方の軸に渡されていても等価ではありません。なぜか分からない。どんな助けでも大歓迎です。

+1

ないように注意してくださいしかし、私は、 '' a''と '' b''収集するためにコードの1行(私の悪い)、単にデータフレームを追加するのを忘れ: '' DF <-data.frame(Aを、b) ''私は上記の例でそれを修正しました –

答えて

3
set.seed(100) 
df <- data.frame(b = sample(1:10, 10), a = sample(1:10, 10))  


ggplot(data=df, aes(a,b))+ 
theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black")) + 
geom_text(aes(label=rownames(df)), color="black") + 
scale_y_continuous(breaks = c(1,3,5,7,9,11,13)) + 
scale_x_continuous(breaks = c(1,3,5,7,9,11,13)) + 
coord_fixed(ylim=c(0, 13),xlim=c(0, 13)) 

enter image description here

+0

ああ、 '' coord_fixed''を参照してくださいありがとう! –

2

scale_*_continuous機能がlimitsパラメータは、あなたの代わりにcoord_cartesianを使用することができます:

ggplot(data=df, aes(a,b))+ 
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
         panel.grid.major = element_blank(), 
         panel.grid.minor = element_blank(), 
         axis.line = element_line(colour = "black"))+ 
    geom_text(aes(label=rownames(df)), color="black")+ 
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13), limits = c(0, 13)) + 
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13), limits = c(0, 13)) 

plot with proper limits

...またはあなたがcoord_cartesianでそれを行う上で主張すれば、それを1回の呼び出しで行います。それ以外の場合は、グラフをトリミングしてからもう一度トリミングします。これが問題の原因です。

ggplot(data=df, aes(a,b))+ 
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
         panel.grid.major = element_blank(), 
         panel.grid.minor = element_blank(), 
         axis.line = element_line(colour = "black"))+ 
    geom_text(aes(label=rownames(df)), color="black")+ 
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+ 
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+ 
    coord_cartesian(xlim=c(0, 13), ylim = c(0, 13)) 
# returns the same thing 
関連する問題