9

Titles are left-aligned by default starting with ggplot 2.2.0. To make them centered again has already been explained in this post:

Center Plot title in ggplot2

This works perfectly in my case as well, however not if I use theme_bw.

dat <- data.frame(
  time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(14.89, 17.23)
)

ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) + 
  geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") + 
  guides(fill=FALSE) +
  xlab("Time of day") + ylab("Total bill") +
  ggtitle("Average bill for 2 people")+
  theme(plot.title = element_text(hjust = 0.5))+
  theme_bw()

I tried passing the theme arguments to theme_bw()

theme_bw(plot.title = element_text(hjust = 0.5))

but that did`t work either.

Any ideas? Help is much appreciated

1 Answer 1

22

You just need to invert theme_bw and theme

ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) + 
  geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") + 
  guides(fill=FALSE) +
  xlab("Time of day") + ylab("Total bill") +
  ggtitle("Average bill for 2 people") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5))

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

We have to use theme_bw() in front of theme().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.