ggplot2我正在使用和包制作多面板图cowplot,但我需要更改单个图的高度。最简单地用一个例子来展示
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)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,通过包含 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)
Run Code Online (Sandbox Code Playgroud)
patchwork包可以完成工作。egg或者multipanelfigure包也可能起作用。
# 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)
Run Code Online (Sandbox Code Playgroud)

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

由reprex 包(v0.3.0)于 2020-03-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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5129 次 |
| 最近记录: |