你好,R和ggplot2社区!
我想水平对齐两个用ggplot2
生成的图。一个有刻面(和刻面条!)和图例,但第二个没有。它们共享同一个Y轴,我想将其对齐。我可以用grobs的宽度垂直对齐地块,但我无法用高度来计算。
这里有一段代码来支持我的问题。
library( ggplot2 )
library( gridExtra )
plot_1 <- ggplot() +
geom_point(
data = iris,
aes(
x = Petal.Length,
y = Petal.Width,
colour = Sepal.Length
)
) +
facet_grid(
. ~ Species
) +
theme(
legend.position = "bottom"
)
plot_2 <- ggplot() +
geom_point(
data = iris,
aes(
x = Sepal.Width,
y = Petal.Width
)
)
g1 <- ggplotGrob( plot_1 )
g2 <- ggplotGrob( plot_2 )
# Here, how to manipulate grobs to get plot_2 scaled to the plot_1's Y axis? (ie, with an empty area right of the plot_1's legend)
grid.arrange( g1, g2, ncol = 2 )
你知道如何在grid.arrange()
之前操纵丁坝的高度吗?(欢迎任何其他建议!)此外,如何将总面积的三分之二分配给plot_1
?
还要注意,我的真实情节实际上有一个coord_flip()
,但我认为它与这个问题无关。
谢谢你的帮助!
一个简单的解决方案是创建一个空白面板:
blank<-grid.rect(gp=gpar(col="white"))
然后使用grid.array创建一个两列网格,其中第二列实际上由三行组成。
grid.arrange(g1, arrangeGrob(blank,g2,blank,ncol=1,
heights=c(0.2,0.9,0.2)),ncol=2)
玩弄heights
的论点应该会为我们提供所需的结果。
关于你的附加问题,grid.arrange
的heights
和widths
论点在那里也应该很有用。