質問
別々のプロット (ggplot2) を、異なる y 軸と異なるプロットの高さでどのように組み合わせ、アライメントを保持しますか?
詳細
(method1) で異なる y 軸単位を使用してプロットを結合するとgrid.arrange
、それらは整列しません。これを回避する 1 つの方法はgtable
(method2) を使用することですが、プロットの相対的な高さを調整できません。
例
require(ggplot2)
#Make two plots, with different y axis
x = c(1, 5)
y= c(.1, .4)
data1<-data.frame(x,y)
top<-
ggplot(data1, aes(x=x, y=y))+
geom_line()
x = c(1, 5)
y= c(100000, 400000)
data2<-data.frame(x,y)
bottom<-
ggplot(data2, aes(x=x, y=y))+
geom_line()
# Method 1 - Grid Extra
require(gridExtra)
grid.arrange(top, bottom, heights=c(.6,.3))
方法 1 の結果は次のプロットになります。y 軸のラベルの長さが異なるため、位置がずれています。
#Method 2 - gtable
require(gtable)
#Extract Grobs
g1<-ggplotGrob(top)
g2<-ggplotGrob(bottom)
#Bind the tables
g<-gtable:::rbind_gtable(g1, g2, "first")
#Remove a row between the plots
g <- gtable_add_rows(g, unit(-1,"cm"), pos=nrow(g1))
#draw
grid.newpage()
grid.draw(g)
方法 2 ではプロットが整列されますが、各プロットの高さを調整することはできません。
ありがとう!