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.
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
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)
title = element_text(hjust = 0.5)) to center the title.
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.
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)
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With