始终将ggplot标题居中于PANEL而不是PLOT [英] Consistently center ggplot title across PANEL not PLOT

查看:114
本文介绍了始终将ggplot标题居中于PANEL而不是PLOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认对齐方式是使ggplot标题与plot.background元素左对齐.其他人注意到,您可以使用plot.title = element_text(hjust = 0.5)将标题居中.

The default alignment is for a ggplot title to be left-aligned to the plot.background element. Others have noted that you can use plot.title = element_text(hjust = 0.5) to center the title.

  • How to center ggplot plot title
  • Center Plot title in ggplot2

但是,我想使标题在整个面板中居中,而不是只针对情节.我过去通过修改hjust来推动标题以使其居中显示来完成此操作,但是hjust的值取决于标题的长度,这使得在批量处理时进行设置非常繁琐,产生图.

However, I want to center the title across the entire panel as oppose to just the plot. I have done this in the past by modifying the hjust to push the title so that it appears centered, but value of hjust is dependent on the length of the title which makes it really tedious to set when I'm batch-producing graphs.

是否可以将ggplot的标题元素始终设置为在panel.background的中心位置?

Is it possible to consistently set the title elements of a ggplot to be centered across the panel.background?

library(reprex)
library(tidyverse)
data(mtcars)

mtcars %>% 
  rownames_to_column(var = "model") %>% 
  top_n(8,wt) %>% 
  ggplot(aes(x =model, y = wt))+
  geom_col()+
  coord_flip()+
  labs(title="This title is left-aligned to the plot")

mtcars %>% 
  rownames_to_column(var = "model") %>% 
  top_n(8,wt) %>% 
  ggplot(aes(x =model, y = wt))+
  geom_col()+
  coord_flip()+
  labs(title="This title is center-aligned to the plot width.")+
  theme(plot.title = element_text(hjust = 0.5))

mtcars %>% 
  rownames_to_column(var = "model") %>% 
  top_n(8,wt) %>% 
  ggplot(aes(x =model, y = wt))+
  geom_col()+
  coord_flip()+
  labs(title="Short title, still works")+
  theme(plot.title = element_text(hjust = 0.5))

mtcars %>% 
  rownames_to_column(var = "model") %>% 
  top_n(8,wt) %>% 
  ggplot(aes(x =model, y = wt))+
  geom_col()+
  coord_flip()+
  labs(title="This title is roughly center-aligned to panel")+ 
  theme(plot.title = element_text(hjust = 0.37)) # I know I can adjust this, but it would require a manual fix each time

p <- mtcars %>% 
  rownames_to_column(var = "model") %>% 
  top_n(8,wt) %>% 
  ggplot(aes(x =model, y = wt))+
  geom_col()+
  coord_flip()+
  #labs(title="Short title, still works")+
  theme(plot.title = element_text(hjust = 0.5)) 

gridExtra::grid.arrange( top=grid::textGrob("This title is center-aligned to panel"),p  )

reprex软件包(v0.2.1)于2018-09-24创建

推荐答案

一种方法是更改​​标题组的位置,使其从左边缘开始:

One approach is to change the position of the title grob so that it starts from the left edge:

p <- mtcars %>% 
  rownames_to_column(var = "model") %>% 
  top_n(8,wt) %>% 
  ggplot(aes(x =model, y = wt))+
  geom_col()+
  coord_flip()+
  labs(title="This title is left-aligned to the plot") +
  theme(plot.title = element_text(hjust = 0.5))

library(gridExtra)
g <- ggplotGrob(p)
g$layout$l[g$layout$name == "title"] <- 1
grid::grid.draw(g)

这篇关于始终将ggplot标题居中于PANEL而不是PLOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆