r - 如何将嵌套的 plot_grid 缩放到相同的大小?

标签 r layout plot ggplot2 cowplot

我有 7 个地 block ,我将它们组织成 2 列,一列有 3 个地 block ,另一列有 4 个地 block 。为此,我使用了 cowplot 中的 plot_grid。结果几乎是完美的,但是,具有 3 个图的列具有更大的图。如何缩放此列以在所有图中获得相同的大小并对齐每列的第一个和最后一个图?

library(ggplot2)
library(cowplot)

Value <- seq(0,1000, by = 1000/10)
Index <- 0:10
DF <- data.frame(Index, Value)

plot1 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot2 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot3 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot4 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot5 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot6 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot7 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

col1 <- plot_grid(plot1, plot2, plot3, align = "v", ncol = 1)
col2 <- plot_grid(plot4, plot5, plot6, plot7, align = "v", ncol = 1)
plot_grid(col1, col2, ncol = 2, align = "hv")

enter image description here

最佳答案

您可以在第一列中放置一个空图:

col1 <- plot_grid(plot1, plot2, plot3, NULL, align = "v", ncol = 1)

但是,对我来说,这不是嵌套绘图网格的情况。嵌套绘图网格专门用于将具有复杂排列的绘图组合在一起,例如一列中的一个绘图跨越另一列中的两行。对于你想要的,我会做一个 plot_grid() 调用,就像 the other comment建议:

plot_grid(plot1, plot4, plot2, plot5, plot3, plot6, NULL, plot7,
          ncol = 2, align = "hv")

enter image description here

还请注意,当您指定特定的纵横比时,align 选项不起作用。您可以指定纵横比或对齐。在大多数情况下,没有必要指定纵横比,您可以在保存图形时这样做。 save_plot() 函数特别采用纵横比选项(但适用于整个图像,而不是绘图区域)。

关于r - 如何将嵌套的 plot_grid 缩放到相同的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47614662/

相关文章:

java - 屏幕尺寸 18.5x9 的布局

python - matplotlib 对数 y 轴条形图

r - ggplot2 情节在情节中的完美拟合

java - 如何创建 layout-small-land 文件夹?

ios - xamarin.forms,如何更改应用顶部布局

r - 如何在R中一起绘制两个直方图?

R scale_manual 使用2个字母作为点形

用R中的大括号替换方括号

RStudio - 崩溃后如何恢复数据?

r - 如何将sqlite日期转换为R中的日期