如何将嵌套的 plot_grid 缩放到相同的大小?
How can I scale nested plot_grid to the same size?
我有 7 个地块,我将它们组织成 2 列,一列有 3 个地块,另一列有 4 个地块。为此,我使用了 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")
您可以将空图放入第一列:
col1 <- plot_grid(plot1, plot2, plot3, NULL, align = "v", ncol = 1)
但是,对我来说,这不是嵌套绘图网格的情况。嵌套绘图网格专门用于组合具有复杂排列的绘图,例如一列中的一个图跨越另一列中的两行。对于你想要的,我会做一个 plot_grid()
调用,就像 建议的那样:
plot_grid(plot1, plot4, plot2, plot5, plot3, plot6, NULL, plot7,
ncol = 2, align = "hv")
另请注意,当您指定特定的纵横比时,align
选项不起作用。您可以指定纵横比或对齐。在大多数情况下,没有必要指定纵横比,您可以在保存图窗时指定纵横比。 save_plot()
函数特别采用纵横比选项(但应用于整个图像,而不是绘图区域)。
我上周刚做过这件事!尝试只调用一次 plot_grid,在你想要空位的地方使用 NULL。
myplotlist <- list(plot1, plot2, plot3, NULL, plot4, plot5, plot6, plot7, plot8)
plot_grid(plotlist=myplotlist, align = "v", ncol = 2)
如果你想保持原始绘图大小差异并且也对齐它们,我有一个简单的作弊修复,基于上面@ClausWilke 的回答。
人们总是可以使用 cowplot:plot_grid 通过添加 "nested" NULL 图并更改它们的 height/width 比率来微调图对齐。请参阅以下解决方案:
group1<-plot_grid(plot1, plot2, plot3, nrow = 3)
group2<-plot_grid(plot4, plot5, plot6, plot7, NULL, rel_heights = c(1,1, 1,1,0.001), nrow = 5)
plot_grid(group1, group2, align='h', nrow=1)
这是输出:
在轴参数无法完成工作的情况下,这对我帮助很大。
我有 7 个地块,我将它们组织成 2 列,一列有 3 个地块,另一列有 4 个地块。为此,我使用了 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")
您可以将空图放入第一列:
col1 <- plot_grid(plot1, plot2, plot3, NULL, align = "v", ncol = 1)
但是,对我来说,这不是嵌套绘图网格的情况。嵌套绘图网格专门用于组合具有复杂排列的绘图,例如一列中的一个图跨越另一列中的两行。对于你想要的,我会做一个 plot_grid()
调用,就像
plot_grid(plot1, plot4, plot2, plot5, plot3, plot6, NULL, plot7,
ncol = 2, align = "hv")
另请注意,当您指定特定的纵横比时,align
选项不起作用。您可以指定纵横比或对齐。在大多数情况下,没有必要指定纵横比,您可以在保存图窗时指定纵横比。 save_plot()
函数特别采用纵横比选项(但应用于整个图像,而不是绘图区域)。
我上周刚做过这件事!尝试只调用一次 plot_grid,在你想要空位的地方使用 NULL。
myplotlist <- list(plot1, plot2, plot3, NULL, plot4, plot5, plot6, plot7, plot8)
plot_grid(plotlist=myplotlist, align = "v", ncol = 2)
如果你想保持原始绘图大小差异并且也对齐它们,我有一个简单的作弊修复,基于上面@ClausWilke 的回答。
人们总是可以使用 cowplot:plot_grid 通过添加 "nested" NULL 图并更改它们的 height/width 比率来微调图对齐。请参阅以下解决方案:
group1<-plot_grid(plot1, plot2, plot3, nrow = 3)
group2<-plot_grid(plot4, plot5, plot6, plot7, NULL, rel_heights = c(1,1, 1,1,0.001), nrow = 5)
plot_grid(group1, group2, align='h', nrow=1)
这是输出:
在轴参数无法完成工作的情况下,这对我帮助很大。