我正在尝试使用安排多个地块grid.arrange.它完成了本书的工作,并在致电时:
p1 <- ggplot(subset(mtcars, cyl = 4), aes(wt, mpg, colour = cyl)) + geom_point()
p2 <- ggplot(subset(mtcars, cyl = 8), aes(wt, mpg, colour = cyl)) + geom_point()
grid.arrange(p1, p2, ncol = 2)
Run Code Online (Sandbox Code Playgroud)
我得到两个很好的图,大小对称:

我的图表引用了不同的参数,但它们对组共享相同的颜色编码.所以我想删除除了一个以外的所有传奇,找到一个不错的地方.
但是,当我尝试:
p3 <- ggplot(subset(mtcars, cyl = 8), aes(wt, mpg, colour = cyl)) + geom_point() + guides(colour=FALSE)
grid.arrange(p3, p2, ncol = 2)
Run Code Online (Sandbox Code Playgroud)
没有图例的情节得到(正确)更大:

我想保持大小(作为x轴的长度)在图表中保持相同.
我知道我可以在这里使用faceting,但是我还需要结合使用facet(我认为)很难实现的各种图形.
有可能做到grid.arrange吗?还有其他解决方案吗?
题
如何组合单独的图(ggplot2),不同的y轴和不同的绘图高度,但保持对齐?
详情
将图表与grid.arrange(方法1)组合在一起时,使用不同的y轴单位时,它们不会对齐.解决这个问题的一种方法是使用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))
Run Code Online (Sandbox Code Playgroud)
方法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)) …Run Code Online (Sandbox Code Playgroud)