向 r 中的拼凑图添加通用 xlab

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

我正在跟进这个很棒的答案。简而言之,假设我们只能访问

plots
对象,而无法操作单个
p
对象,那么我们如何向
xlab="mpg"
对象添加一个公共
plots
呢?

注意:

xlab
添加到 这个很棒的答案 会很棒。

library(ggplot2)
library(patchwork)

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


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

r function ggplot2 plot
2个回答
1
投票

一个选项可以是使用

plot_annotation
并创建一个
title
并将此标题的位置调整到您的 x 实验室位置,如下所示:

library(ggplot2)
library(patchwork)

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

(plots = wrap_plots(p1,p2,p3,p4)) +
  plot_annotation(title = "xlab") &
  theme(plot.title = element_text(vjust = -110, hjust = 0.50))

创建于 2023-01-21,使用 reprex v2.0.2


这也可以使用

caption
subtitle
来完成。


编辑:

您可以像这样组合 x 和 y 标签:

library(ggplot2)
library(patchwork)

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

(plots = wrap_plots(p1,p2,p3,p4)) +
 plot_annotation(title = "xlab",
                 subtitle = "ylab") &
 theme(plot.title = element_text(vjust = -110, hjust = 0.50),
       plot.subtitle = element_text(vjust = -55, hjust = -0.01, size = 12))

创建于 2023-01-21,使用 reprex v2.0.2


0
投票

patchwork 1.2.0 起,您可以在

axis_titles = "collect"
中使用新参数
plot_layout
:

library(ggplot2)
library(patchwork)

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

wrap_plots(p1, p2, p3, p4) +
  plot_layout(axis_titles = "collect")

See the result

如果您只想有一个通用的 x 轴标题,可以使用

axis_titles = "collect_x"
代替。

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