在 multi-panel 中设置单个面板的宽度和高度 plot in cowplot::plot_grid
Setting width and height of a single panel in multi-panel plot in cowplot::plot_grid
我正在使用 ggplot2
和 cowplot
包制作 multi-panel 图,但我需要更改单个图的高度。最简单的例子是
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)
如您所见,由于包含 x-axis 标题,与同一行中的其他两个面板相比,最右上方面板中的 y 轴已缩小。
那么我该如何设置这个单个面板的相对高度和宽度,以便 y-axis 与顶行面板的 y-axis 对齐?
您不能使用 rel_heights =
和 rel_widths() =
参数设置单个面板,当我尝试添加 axis = "tblr"
和 align = "hv"
参数时,我收到错误消息
Error in `[.unit.list`(sizes[[x]], list_indices[[x]]) :
index out of bounds (unit list subsetting)
patchwork
package can get the job done. egg
or multipanelfigure
packages might work too。
# 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 package (v0.3.0)
于 2020 年 3 月 26 日创建
@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)
我正在使用 ggplot2
和 cowplot
包制作 multi-panel 图,但我需要更改单个图的高度。最简单的例子是
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)
如您所见,由于包含 x-axis 标题,与同一行中的其他两个面板相比,最右上方面板中的 y 轴已缩小。
那么我该如何设置这个单个面板的相对高度和宽度,以便 y-axis 与顶行面板的 y-axis 对齐?
您不能使用 rel_heights =
和 rel_widths() =
参数设置单个面板,当我尝试添加 axis = "tblr"
和 align = "hv"
参数时,我收到错误消息
Error in `[.unit.list`(sizes[[x]], list_indices[[x]]) :
index out of bounds (unit list subsetting)
patchwork
package can get the job done. egg
or multipanelfigure
packages might work too。
# 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 package (v0.3.0)
于 2020 年 3 月 26 日创建@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)