使用theme_bw在ggplot2中绘制标题
问题描述:
从ggplot 2.2.0开始,标题默认是左对齐的。为了让他们再次为中心已经在这个岗位已经解释:使用theme_bw在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工作无论是。
任何想法?帮助深表感谢
答
你只需要反转theme_bw
和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))
是的,就是这样。实际上很容易,但没有看到它。谢谢! – HannesZ
Hi @HannesZ如果这解决了你的问题,请考虑通过点击复选标记来接受它。这向更广泛的社区表明,您已经找到了解决方案,并为答复者和您自己提供了一些声誉。没有义务这样做。 – AntoineBic
对不起,我以为我已经做到了。我接受得太快:) – HannesZ