这个简单的代码(以及今天早上我的所有脚本)已经开始在 ggplot2 中给我一个偏离中心的标题:
Ubuntu version: 16.04
R studio version: Version 0.99.896
R version: 3.3.2
GGPLOT2 version: 2.2.0
我今天早上刚刚安装了上述内容,试图解决这个问题...
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
# Add title, narrower bars, fill color, and change axis labels
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")
最佳答案
摘自ggplot 2.2.0
发布消息:"The main plot title is now left-aligned to better work better with a subtitle" 。另请参阅 ?theme
中的 plot.title
参数:“默认左对齐”。
正如@J_F所指出的,您可以添加theme(plot.title = element_text(hjust = 0.5))
以使标题居中。
ggplot() +
ggtitle("Default in 2.2.0 is left-aligned")
ggplot() +
ggtitle("Use theme(plot.title = element_text(hjust = 0.5)) to center") +
theme(plot.title = element_text(hjust = 0.5))
关于r - ggplot2 中的中心图标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40675778/