在ggplot2中居中打印标题
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开始,标题默认为左对齐。通过将以下内容添加到图中,标题可以居中:
theme(plot.title = element_text(hjust = 0.5))
但是,如果你创造了许多情节,到处添加这条线可能会很繁琐。用户也可以使用以下命令更改ggplot的默认行为
在ggplot2的2.2.0版本后,标题默认左对齐。要实现居中,可以在图中添加特定代码。此外,可以通过修改ggplot的默认设置,使得后续创建的所有图表标题自动居中。这种方法更便于批量处理。
订阅专栏 解锁全文

943

被折叠的 条评论
为什么被折叠?



