默认情况下,标题是左对齐的,从ggplot 2.2.0开始.为了使它们再次居中,这篇文章已经解释过了:
这在我的情况下也很完美,但是如果我使用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()
Run Code Online (Sandbox Code Playgroud)
我尝试将主题参数传递给 theme_bw()
theme_bw(plot.title = element_text(hjust = 0.5))
Run Code Online (Sandbox Code Playgroud)
但那也没有用.
有任何想法吗?非常感谢帮助
abi*_*hat 11
你只需要反转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))
Run Code Online (Sandbox Code Playgroud)