大家好,这是关于it技术的翻译。
我想要水平对齐两个使用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()
之前操纵grobs的高度吗?(欢迎任何其他建议!)此外,如何将总面积的三分之二分配给plot_1
?请注意,我的实际图表实际上具有
coord_flip()
,但我假设它与此问题无关。感谢您的帮助!
plot_2
所需的数据附加到您用于plot_1
的数据集中,并让facet_grid()
为您完成所有对齐工作。但是,这也意味着使用grid.arrange
会失去直接控制宽度的能力。 - Pewiplot_1
的代码中将iris
替换为iris2
就可以了。 - Pewiplot_1
和plot_2
不共享相同的 X 变量(以及断点、刻度标签和轴标签)。我的实际图形的 X 变量实际上具有不同的单位和跨度。facet_grid()
能够考虑到这种差异吗? - Ben