繁体   English   中英

我怎样才能在ggplot中有奇数个不同宽度的图?

[英]How can I have odd number of plots with different widths in ggplot?

我有 7 个图,我想将它们放在一页上,两列四行。

我尝试使用以下两个代码将它们放在一页中。 由于 4 号地块比其他地块长,我把它作为最后一个地块。 但是,由于我指定了两列,所以最后一个图的宽度与第一列图的宽度相同。 我试图为最后一个图设置不同的宽度,但它不起作用。 我需要更改什么才能纠正它?

ggarrange(plts[[1]], plts[[2]], plts[[3]], plts[[7]], plts[[5]], plts[[6]], plts[[4]],
          heights = c(1, 1, 1, 1, 1, 1, 15), widths = c(11, 13, 11, 13, 11, 13, 25), 
          ncol = 2, nrow = 4)

和,

grid.arrange(
  grobs = plts,
  widths = c(11, 13),
  heights = c(5, 5, 5, 15, 5, 5, 5),
  layout_matrix = rbind(c(1, 2),
                        c(3, 7),
                        c(5,6),
                        c(4))
)

下图显示了问题: 在此处输入图像描述

如果您对其他包开放,也许patchwork 包会适合您的用例,例如

library(tidyverse)
library(patchwork)

ggplot_list <- list()
for (var in seq_along(mtcars)){
  ggplot_list[[var]] <- ggplot(mtcars, aes(x = mtcars[[!!var]])) +
    geom_histogram()
}

layout <- "
ABCD
EFGG
"

ggplot_list[[1]] + ggplot_list[[2]] + ggplot_list[[3]] + ggplot_list[[4]] + ggplot_list[[5]] + ggplot_list[[6]] + ggplot_list[[7]] +
  plot_layout(design = layout)

创建于 2022-12-20,使用reprex v2.0.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM