在ggplot2中心绘图标题 [英] Center Plot title in ggplot2
本文介绍了在ggplot2中心绘图标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好这个简单的代码(以及我今天上午的所有脚本)已经开始给我一个ggplot2中的偏离中心的标题了。
Ubuntu版本:16.04
R工作室版本:版本0.99.896
R版本:3.3.2
GGPLOT2版本:2.2.0
我今天早上刚刚安装了上述内容以尝试修复此问题....
dat < - data.frame(
time = factor(c(Lunch,Dinner),levels = c(午餐,晚餐)),
total_bill = c(14.89,17.23)
)
#添加标题,更窄的小节,填充颜色和更改轴标签
ggplot(data = dat,aes(x = time,y = total_bill,fill = time))+
geom_bar(color =black,fill =#DD8888,width = .8,stat = 身份)+
指南(fill = FALSE)+
xlab(时间)+ ylab(总账单)+
ggtitle(2人平均账单 )
解决方案
从 ggplot 2.2.0
:
ggplot()+
ggtitle(Use theme(plot.title = element_text(hjust = 0.5))to center)+
theme(plot.title = element_text(hjust = 0.5))
Hi this simple code (and all my scripts from this morning) has started giving me a 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屋!
查看全文