为 r 中的拼凑图添加通用 ylab

问题描述 投票:0回答:2

我可以为common_ylab生成的

plots
创建
一个
patchwork::wrap_plots()

但我想知道如何添加那个

common_ylab
到那些
plots
(试过:
plots + common_ylab
)?

注意: 假设我们只能访问

plots
对象,而无法操作单个
p
对象。

library(ggplot2)
library(patchwork)
library(grid)

p1 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p2 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p3 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p4 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")


(plots = wrap_plots(p1,p2,p3,p4))

# Common ylab:
(common_ylab = grid::grid.draw(grid::textGrob("VS", x = 0.02, rot = 90)))

# Tried the following without success:
plots + common_ylab
r function ggplot2 plot tidyverse
2个回答
0
投票

我倾向于在这里添加一个虚拟的ggplot:

p5 <- ggplot() + geom_text(aes(1, 1, label = "vs"), angle = -90) + theme_void()

(plots | p5) + plot_layout(widths = c(12, 1))


0
投票

使用 patchwork 1.2.0,您现在可以在

axes = "collect_y"
中使用新参数
plot_layout

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p2 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p3 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p4 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")


wrap_plots(p1, p2, p3, p4) +
  plot_layout(axes = "collect_y") &
  ylab("VS")

© www.soinside.com 2019 - 2024. All rights reserved.