你好 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 )
你以前知道如何操纵 grobs 的高度grid.arrange()
吗?(欢迎任何其他建议!)此外,如何将总面积的三分之二分配给plot_1
?
另请注意,我的真实情节实际上具有 a coord_flip()
,但我认为它与这个问题无关。
谢谢你的帮助!