如何对齐具有不同宽度的两个图,以便:
- 他们的 x 轴是同步的
- 顶行还包含一个常见的图例
我基本上希望每个 x 轴间隔对应于两个图上相同的厘米数。我试过这个:
library(cowplot)
library(tidyverse)
wide_plot = iris %>%
ggplot(aes(x = Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
theme(legend.position = "none") +
scale_x_continuous(limits = c(0,8))
narrow_plot = iris %>%
ggplot(aes(x = Sepal.Width, Sepal.Length, color = Species)) +
geom_point() +
theme(legend.position = "none") +
scale_x_continuous(limits = c(0,5) )
legend = cowplot::get_legend(ggplot(iris,aes(x = Sepal.Length, Sepal.Width, color = Species)) + geom_point() )
plot_grid(plot_grid(narrow_plot, legend), wide_plot, nrow = 2 )
这产生了这个情节:
但是上面的问题是顶部底部图上的 x 轴值不是“同步”的。
我试图修改 cowplot::plot_grid 中的 rel_widths 选项,但我希望有一个更准确的解决方案。