ggplot2 中的中心图标题 [英] Center Plot title in ggplot2
本文介绍了ggplot2 中的中心图标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个简单的代码(以及我今天早上的所有脚本)已经开始在 ggplot2 中给我一个偏离中心的标题:
Ubuntu 版本:16.04R 工作室版本:版本 0.99.896R 版本:3.3.2GGPLOT2 版本:2.2.0
我今天早上刚安装了上面的东西来尝试解决这个问题...
dat <- data.frame(时间=因子(c(午餐",晚餐"),水平= c(午餐",晚餐")),total_bill = c(14.89, 17.23))# 添加标题、窄条、填充颜色和更改轴标签ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +geom_bar(颜色=黑色",填充=#DD8888",宽度=.8,统计=身份")+指南(填充=假)+xlab(时间") + ylab(总账单") +ggtitle(2人的平均账单")
解决方案
来自ggplot 2.2.0
的发布消息:
ggplot() +ggtitle("使用主题(plot.title = element_text(hjust = 0.5)) 居中") +主题(plot.title = element_text(hjust = 0.5))
This simple code (and all my scripts from this morning) has started giving me an off center title in ggplot2:
Ubuntu version: 16.04
R studio version: Version 0.99.896
R version: 3.3.2
GGPLOT2 version: 2.2.0
I have freshly installed the above this morning to try and fix this...
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")
解决方案
From the release news of ggplot 2.2.0
: "The main plot title is now left-aligned to better work better with a subtitle". See also the plot.title
argument in ?theme
: "left-aligned by default".
As pointed out by @J_F, you may add theme(plot.title = element_text(hjust = 0.5))
to center the title.
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))
这篇关于ggplot2 中的中心图标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文