r - ggplot2 中的中心图标题

标签 r ggplot2 center ggtitle

这个简单的代码(以及今天早上我的所有脚本)已经开始在 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")

enter image description here

最佳答案

摘自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")

enter image description here

ggplot() +
  ggtitle("Use theme(plot.title = element_text(hjust = 0.5)) to center") +
  theme(plot.title = element_text(hjust = 0.5))

enter image description here

关于r - ggplot2 中的中心图标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40675778/

相关文章:

css - Wordpress 菜单居中对齐且间距均匀

r - table() 的 as.data.frame 总结频率

r - dateRangeInput ui 的一部分隐藏在 Shiny 的仪表板标题下

r - 如何在 R 中匹配两个具有约束的数据框?

r - 对 GGplot2 使用 react 数据集?

html - 如何在html中将中间的两个按钮与一定的边距对齐

r - 在使用 R 查询之前从 SQLite 数据库附加两个表

r - 使用ggplot2 barplot在R中构建二进制迷你图

r - 使用自定义标签函数在 ggplot 中标记 y 轴

html - 如何使边框居中