在面板上始终将ggplot标题居中居中而不是PLOT

时间:2018-09-23 18:53:26

标签: r ggplot2

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

但是,我想使标题在整个面板中居中,而不是只针对情节。我过去通过修改hjust来推动标题以使其居中显示来完成此操作,但是hjust的值取决于标题的长度,这使得在我设置时真的很麻烦'm个批量生产图。

是否可以将ggplot的标题元素始终设置为在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

@ user20650的建议

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 package(v0.2.1)于2018-09-24创建

3 个答案:

答案 0 :(得分:2)

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

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)

enter image description here

答案 1 :(得分:1)

主题中现在可以选择调用 plot.title.position 将绘图标题居中到整个绘图区域,而不仅仅是绘图面板。如果您在 plot.title.position = "plot" 中设置 theme(),它会将标题和副标题居中到整个绘图区域,而不仅仅是面板上方。

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 center-aligned to the plot width.")+
  theme(plot.title = element_text(hjust = 0.5),
        plot.title.position = "plot")

This title is center-aligned to the plot width.

答案 2 :(得分:0)

另一种选择是使用patchwork库,它是plot_annotation,因此您将拥有:

mtcars %>% 
     rownames_to_column(var = "model") %>% 
     top_n(8,wt) %>% 
     ggplot(aes(x =model, y = wt))+
     geom_col()+
     coord_flip()+
     patchwork::plot_annotation(title="This title is very long and centered on the whole plot", 
          theme = theme(plot.title = element_text(hjust = 0.5)))

enter image description here