首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ggplot和patchwork的嵌套绘图的中心标记

使用ggplot和patchwork的嵌套绘图的中心标记
EN

Stack Overflow用户
提问于 2021-10-20 10:39:35
回答 1查看 145关注 0票数 2

在下面的示例中,如何正确地将嵌套图的标记居中?我想要将第二个面板的标签/标题居中,就像第一个面板一样。

或者,使用多个标题/副标题也可以代替标签(我尝试过使用标题,但当组合绘图时,下方图形的标题会消失)。在这里,设置element_text(hjust = 0.5)似乎也没有任何效果。

代码语言:javascript
复制
library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')

ptop <- p1
pbot <- p1 + p2 + p3 + p4
combined <- ptop + pbot +
  plot_layout(ncol = 1, guides = "collect") +
  plot_annotation(tag_levels = list(c("(a) Panel a", "(b) Panel b "))) &
  theme(plot.tag = element_text(hjust = 0.5, size = 30),
        plot.tag.position = "top",
        legend.position = "bottom") 
combined

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-22 18:38:51

我能够将它作为一个附加组件与cowplot一起使用

代码语言:javascript
复制
library(ggplot2)
library(patchwork)
library(cowplot)

p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle(label = "",
          subtitle = 'Plot 1') 

p2 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle(label = "",
          subtitle = 'Plot 1')

p3 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle(label = "",
          subtitle = 'Plot 2')

p4 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle(label = "",
          subtitle = 'Plot 3') +
  theme(legend.position = "none")

p5 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle(label = "",
          subtitle = 'Plot 4')

ptop <- p1 +  plot_annotation("(a) Panel a")& theme(plot.title = element_text(hjust = 0.5))
pbot <- p2 + p3 + p4 + p5 +  plot_annotation("(b) Panel b") & theme(plot.title = element_text(hjust = 0.5))
legend_b <- get_legend(
  p4+
  theme(legend.position = "bottom",
        legend.direction = "horizontal")
)

plot_grid(ptop, pbot, legend_b, ncol = 1, nrow = 3, rel_heights = c(1, 1,0.25))

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69644490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档