Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consistently center ggplot title across PANEL not PLOT

Tags:

r

ggplot2

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

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.

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

Suggestion by @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  )

Created on 2018-09-24 by the reprex package (v0.2.1)

like image 274
grad_student Avatar asked Sep 23 '18 18:09

grad_student


People also ask

How do I center a title in ggplot R?

title = element_text(hjust = 0.5)) to center the title.

How do I change the position of a caption in ggplot2?

position of the theme function allows aliging the caption to the panel ( "panel" , default) or the whole plot (“ plot ”). Note that the default for the caption is right alignment, so you can set hjust = 0 to move the caption to the left of the whole plot.


2 Answers

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)

enter image description here

like image 113
Weihuang Wong Avatar answered Sep 20 '22 12:09

Weihuang Wong


There is now the option in theme to call plot.title.position to center the plot title to the entire plotting area, not just the plot panel. If you set plot.title.position = "plot" in theme(), it will center the title and subtitle to the whole plotting area, not just above the panel.

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.

like image 33
Casey Avatar answered Sep 17 '22 12:09

Casey