cowplot::plot_grid中如何设置多面板图中单个面板的宽度和高度?

3

我正在使用ggplot2cowplot包制作一个多面板图,但我需要改变单个图的高度。这最好通过一个例子来说明。

library(ggplot2)
library(cowplot)

p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.x = element_blank(),
                   axis.title.x = element_blank(),
                   legend.position = "none")
p2 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.x = element_blank(),
                   axis.title.x = element_blank(),
                   axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")
p3 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")
p4 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() + 
             theme(legend.position = "none")
p5 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + 
             geom_point() +
             theme(axis.text.y = element_blank(),
                   axis.title.y = element_blank(),
                   legend.position = "none")

pL <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) + geom_point()
l <- get_legend(pL)

# simple grid
plot_grid(p1, p2, p3, p4, p5, l, ncol = 3)

enter image description here

在右上方的面板中,由于包含了x轴标题,因此与同一行中的其他两个面板相比,y轴已经缩小了。

那么我该如何设置单个面板的相对高度和宽度,以便y轴与顶部行中的面板的y轴对齐?

您无法使用rel_heights =rel_widths() =参数来设置单个面板,并且当我尝试添加axis = "tblr"align = "hv"参数时,会出现错误消息。

Error in `[.unit.list`(sizes[[x]], list_indices[[x]]) : 
  index out of bounds (unit list subsetting) 

有没有必要使用cowplot?我觉得用facet_wrap构建图表可能更容易,然后只需使用theme(legend.position)将图例从默认位置移动即可。然而,如果必须使用cowplot,你可以看一下ggdrawdraw_plot函数。 - undefined
2个回答

6

patchwork包可以完成工作。eggmultipanelfigure包也可能有效

# https://github.com/thomasp85/patchwork
# install.packages("devtools", dependencies = TRUE)
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
#> 
#> Attaching package: 'patchwork'
#> The following object is masked from 'package:cowplot':
#> 
#>     align_plots

layout <- "
ABC
DEF
"
p1 + p2 + p3 + p4 + p5 + l +
  plot_layout(design = layout)

(p1 + p2 + p3)/(p4 + p5 + l) +
  plot_layout(nrow = 2) +
  plot_annotation(title = "Title",
                  subtitle = "Subtitle",
                  tag_levels = 'i',
                  tag_suffix = ')')

此示例由reprex软件包 (v0.3.0)于2020-03-26创建


1
非常好,@Tung。谢谢你! - undefined

5
@Tung的回答很好,但我也找到了cowplot的解决方案。你只需创建两个单独的面板,每行一个面板,然后可以使用align=axis=参数来对齐y轴,我们基于水平参考线垂直对齐y轴,所以我们指定align = "h"。
top_row <- plot_grid(p1, p2, p3, align = "h", axis = "l", ncol = 3)

bottom_row <- plot_grid(p4, p5, l, align = "h", axis = "l", ncol = 3)

plot_grid(top_row, bottom_row, ncol = 1)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接