【发布时间】:2016-12-12 16:57:37
【问题描述】:
我想在水平 ggplot2 条形图中左对齐 plot.title、plot.subtitle 和 plot.caption。
例子:
library("ggplot2") # ggplot2 2.2
df <- data.frame(type=factor(c("Brooklyn",
"Manhatten and\n Queens")),
value=c(15,30))
# manual hjust for title, subtitle & caption
myhjust <- -0.2
ggplot(df,
aes(x=type, y=value)) +
geom_bar(stat='identity') +
coord_flip() +
labs(
title = "This is a nice title",
subtitle = "A subtitle",
caption = "We even have a caption. A very long one indeed.") +
theme(axis.title=element_blank(),
plot.title=element_text(hjust = myhjust),
plot.subtitle=element_text(hjust = myhjust ),
plot.caption=element_text(hjust = myhjust))
如何将所有 3 个 labs 元素(plot.title、plot.subtitle 和 plot.caption)与 axis.text 的开始位置(红色垂直线,Manhatten 的“M”)对齐?
另外:为什么固定的myhjust 会导致plot.title、plot.subtitle 和plot.caption 出现3 个不同的水平位置?
【问题讨论】:
-
这是一个很好的问题,插图很好,解释也很清楚。这是我在使用 ggplot 时经常遇到的问题。虽然下面的 grid.arrange() 替代方案是可行的,但对我来说,这并不是这个问题的完整答案。例如,为什么相同的 hjust 值不会对标题、副标题和标题产生相同的影响?我认为答案是在每种情况下,参考点都是文本字符串的中间。虽然这对于居中对齐的文本是明智的,但它不适用于绘图区域之外的左对齐或右对齐。看起来这应该是一个简单的设置。