2016-12-12 4 views
0

私はggplotの2つのデータセットを並べて、y-axisを共有しています。ggplotのスティッチファセット

これはggplotfacet_wrapを使用すると考えられましたが、どのようにしてstitchを見つけましたか。

df.1 <- data.frame(x=c(-0.678071905112638,1.32192809488736,-0.678071905112638,1.32192809488736,-0.678071905112638,1.32192809488736), 
        y=c(62.8805462356349,73.027603062927,88.4090942806369,87.6879626013305,55.9895740872068,93.5396099910227), 
        side=1,stringsAsFactors = F) 

df.2 <- data.frame(x=c(1.32192809488736,3.32192809488736,1.32192809488736,1.32192809488736), 
        y=c(73.027603062927,7.33717302418609,87.6879626013305,93.5396099910227), 
        side=2,stringsAsFactors = F) 

df <- rbind(df.1,df.2) 
df$side <- factor(df$side,levels=c(1,2)) 

require(ggplot2) 
ggplot(df,aes(x=x,y=y))+geom_point()+facet_wrap(~side,ncol=2,scales="free")+stat_smooth(method="lm",formula=y~x,colour="black")+theme(strip.text.y=element_text()) 

enter image description here

どのように私は右面のy軸を取り除くと、彼らは、単一の図として表示されますので、ファセットの間のスペースを削除しますか:これは私がこれまで持っているものでしょうか?また、同じy軸座標を持つ必要があります。

私が2つを使用している理由は明らかです。facetsはそれぞれdfに1枚の絵を付けているからです。

答えて

1

あなたは、スペースを削除するにはファセット間のマージンを調整することができます。また、scales="free_x"を使用すると、x軸のスケールだけが空き、y軸のスケールは同じになります。 y軸のスケールが各ファセットに同じなので、スケールが右側面に繰り返されることはありません。

theme_set(cowplot::theme_cowplot()) 

ggplot(df,aes(x=x,y=y))+geom_point() + 
    facet_wrap(~side, ncol=2, scales="free_x") + 
    stat_smooth(method="lm", formula=y~x, colour="black") + 
    theme(panel.spacing = unit(-1.25, "lines")) 

enter image description here

しかし、あなたはまた、面取りを回避し、色を使用して、別の行を得ることができます美的。このアプローチは、単一のパネル上のすべてを置くので、あなたは、ファセット間のxスケールを並べる心配する必要はありません。

ggplot(df,aes(x=x, y=y)) + 
    geom_point() + 
    stat_smooth(method="lm",formula=y~x, aes(colour=side)) 

enter image description here

1

ylimでy制限を設定します。ギャップをpanel.spacing.xで調整します。不要なラベルをaxis.text.yで、目盛りをaxis.ticksで削除します。

ggplot(df,aes(x=x,y=y))+geom_point()+ ylim(0, 120) + 
     facet_wrap(~side,ncol=2,scales="free")+ 
     stat_smooth(method="lm",formula=y~x,colour="black")+ 
     theme(strip.text.y=element_text(), axis.text.y = element_blank(), 
     axis.ticks = element_blank(), panel.spacing.x=unit(0, "lines")) 

enter image description here