ggplot2 中的中心绘图标题
Posted
技术标签:
【中文标题】ggplot2 中的中心绘图标题【英文标题】:Center Plot title in ggplot2 【发布时间】:2017-04-02 05:27:56 【问题描述】:这个简单的代码(以及我今天早上的所有脚本)已经开始在 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")
【问题讨论】:
使用... + theme(plot.title = element_text(hjust = 0.5))
【参考方案1】:
来自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))
【讨论】:
当我改用... theme(plot.title = element_text(hjust = 'center'))
时,我收到错误警告消息:1: In unit(rep(xp, n), "npc") : NAs introduced by coercionv2: In validDetails.text(x) : NAs introduced by coercion
并且没有标题。这是一个错误吗?
@JamesHirschorn 这不是错误。用于hjust
的值必须是数字。
从ggplot2 3.3.0版本开始,标题默认居中。
我已经添加了这段代码theme(plot.title = element_text(hjust = 0.5))
,但是没有成功,很奇怪。
@ahbon 我意识到这真的很晚了,但你把它放到 ggplot 中的顺序似乎很重要。我遇到了同样的问题,直到我将它移到 ggplot 的所有输入中的提示底部,然后它才起作用。【参考方案2】:
如answer by Henrik 中所述,从 ggplot 2.2.0 开始,标题默认为左对齐。标题可以通过将其添加到情节中来居中:
theme(plot.title = element_text(hjust = 0.5))
但是,如果您创建许多绘图,则在任何地方添加这条线可能会很乏味。然后还可以使用
更改 ggplot 的默认行为theme_update(plot.title = element_text(hjust = 0.5))
运行此行后,之后创建的所有绘图都将使用主题设置 plot.title = element_text(hjust = 0.5)
作为默认设置:
theme_update(plot.title = element_text(hjust = 0.5))
ggplot() + ggtitle("Default is now set to centered")
要恢复原始 ggplot2 默认设置,您可以重新启动 R 会话或选择默认主题
theme_set(theme_gray())
【讨论】:
【参考方案3】:ggeasy
包有一个名为 easy_center_title()
的函数来执行此操作。我觉得它比theme(plot.title = element_text(hjust = 0.5))
更有吸引力,而且更容易记住。
ggplot(data = dat, aes(time, 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") +
ggeasy::easy_center_title()
请注意,在撰写此答案时,您需要从 GitHub 安装 ggeasy
的开发版本才能使用 easy_center_title()
。您可以通过运行remotes::install_github("jonocarroll/ggeasy")
来实现。
【讨论】:
【参考方案4】:如果您经常使用图形和 ggplot,您可能会厌倦每次都添加 theme()。如果您不想按照前面的建议更改默认主题,您可能会发现创建自己的个人主题更容易。
personal_theme = theme(plot.title =
element_text(hjust = 0.5))
假设您有多个图,p1、p2 和 p3,只需将personal_theme 添加到它们。
p1 + personal_theme
p2 + personal_theme
p3 + personal_theme
dat <- data.frame(
time = factor(c("Lunch","Dinner"),
levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
p1 = 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")
p1 + personal_theme
【讨论】:
以上是关于ggplot2 中的中心绘图标题的主要内容,如果未能解决你的问题,请参考以下文章
如何快速(优雅地)在 R 中的时间序列对象 `ts` 和日期框架之间进行迭代以进行 ggplot2 绘图?