Iskandar The Pupsi
Iskandar The Pupsi

Reputation: 323

Center titles and subtitles nd in theme_minimal()

I have following plot:

ggplot(cars, aes(x = speed)) + 
  geom_bar()+
  labs(title="speed",
       subtitle="other info")+
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))

the theme layer centers the plot title and subtitle.

However, when I add theme_minimal() the centering does not apply anymore. I guess theme_minimal overwrites the theme specifications.

ggplot(cars, aes(x = speed)) + 
  geom_bar()+
  labs(title="speed",
       subtitle="other info")+
  theme(plot.title = element_text(hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5))+
 theme_minimal()

Trying to specifiy the title and subtitle position in theme minimal does not the way I though it might:

ggplot(cars, aes(x = speed)) + 
  geom_bar()+
  labs(title="speed",
       subtitle="other info")+
  theme_minimal(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))

Any suggestions how to center title and subtitle from within theme_minimal()?

Upvotes: 3

Views: 1576

Answers (1)

kath
kath

Reputation: 7724

Your guess is correct and adding theme_minimal after you specify a theme overrides the settings. Thus, you need to change the order of your ggplot-call.

ggplot(cars, aes(x = speed)) + 
  geom_bar() +
  labs(title = "speed", subtitle = "other info") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))

This comes from the layering in ggplot, you can also see this, when you specify e.g. a color scale twice, then you get a warning, saying that the first one will be overwritten. Also your plot show does values on top which are plotted last when you plot several layers (i.e. several geom_point-calls).

Upvotes: 3

Related Questions