2017-07-27 216 views
0

从ggplot 2.2.0开始,标题默认是左对齐的。为了让他们再次为中心已经在这个岗位已经解释:使用theme_bw在ggplot2中绘制标题

Center Plot title in ggplot2

这在我的情况下完美的作品为好,但不是如果我用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() 

我试图传递的主题参数theme_bw()

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

但did`t工作无论是。

任何想法?帮助深表感谢

回答

4

你只需要反转theme_bwtheme

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

+0

是的,就是这样。实际上很容易,但没有看到它。谢谢! – HannesZ

+1

Hi @HannesZ如果这解决了你的问题,请考虑通过点击复选标记来接受它。这向更广泛的社区表明,您已经找到了解决方案,并为答复者和您自己提供了一些声誉。没有义务这样做。 – AntoineBic

+0

对不起,我以为我已经做到了。我接受得太快:) – HannesZ

0

我一直有同样的问题,但它似乎是所有需要快速switcheroo!

+1

你应该发布'快速切换',以便它实际上可以作为答案;) – mhz

+0

我也有'theme'和'theme_bw'错误的方式。 – Steve