如何将ggplot标题与窗口对齐而不是绘制网格?

And*_*rie 12 r ggplot2

ggplot2版本0.9中,绘图标题的对齐行为发生了变化.而在v0.8.9中,对齐是相对于绘图窗口的,而在v0.9中,对齐是相对于绘图网格的.

现在,虽然我大多同意这是理想的行为,但我经常有很长的情节标题.

问题:有没有一种方法可以将绘图标题与绘图窗口而不是绘图网格对齐?

我正在寻找能够自动对齐情节的解决方案.换句话说,手动对齐使用hjust对我来说不起作用(我在每个项目的数百个图上运行).

任何grid直接使用的解决方案也是可接受的.


一些示例代码和图:(注意标题如何在窗口右侧被截断).

dat <- data.frame(
  text = c(
    "It made me feel very positive to brand X", 
    "It was clear and easy to understand",
    "I didn't like it al all"),
  value=runif(3)
)
library(ggplot2)
ggplot(dat, aes(text, value)) + 
  geom_bar(stat="identity") +
  coord_flip() +
  opts(title="Thinking about the ad that you've just seen, do you agree with the following statements? I agree that...") +
  theme_bw(16)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

koh*_*ske 14

在ggplot2 0.9中,您可以轻松更改布局.

p <- ggplot(dat, aes(text, value)) + 
  geom_bar(stat="identity") +
  coord_flip() +
  opts(title="Thinking about the ad that you've just seen,\ndo you agree with the following statements?\nI agree that...") +
  theme_bw(16)

gt <- ggplot_gtable(ggplot_build(p))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r))
grid::grid.draw(gt)
Run Code Online (Sandbox Code Playgroud)

也许,在未来的版本中,ggplot2将提供一致的界面来调整布局.

在此输入图像描述

  • 有谁知道ggplot2_2.2.0是否有更简单的方法可以做到这一点? (4认同)