如何将ggplot标题与窗口对齐而不是绘制网格? [英] How to align ggplot title with window rather than plot grid?

查看:165
本文介绍了如何将ggplot标题与窗口对齐而不是绘制网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggplot2 版本0.9中,绘图标题对齐的行为已更改。而在v0.8.9中,对齐是相对于绘图窗口的,在v0.9中,对齐是相对于绘图网格的。

现在,我大多数人都同意这个是很理想的行为,我经常有很长的情节头衔。



问题:有没有一种方法可以将情节标题与情节窗口而不是情节网格?



我正在寻找能够自动对齐图表的解决方案。换句话说,使用 hjust 进行手动对齐不适用于我(我为每个项目在数百个图上运行此操作)。



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




  dat< p< p> -  data.frame(
text = c(
这让我对X品牌感到非常乐观,
这很清楚和容易理解,
我没有'
value = runif(3)

library(ggplot2)
ggplot(dat,aes(text,value))+
geom_bar(stat =identity)+
coord_flip()+
opts(title =考虑到您刚刚看到的广告,您是否同意以下声明?我同意...)+
theme_bw(16)






  p < - > 

ggplot(dat,aes(text,value))+
geom_bar(stat =identity)+
coord_flip()+
opts(title = (ggplot_build(p)),你同意下面的陈述吗?\\ n我同意......)+
theme_bw(16)

gt< - ggplot_gtable )
gt $ layout [which(gt $ layout $ name ==title),c(l,r)]< - c(1,max(gt $ layout $ r))
grid :: grid.draw(gt)

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




In ggplot2 version 0.9 the behaviour of the alignment of a plot title changed. Whereas in v0.8.9 the alignment was relative to the plot window, in v0.9 the alignment is relative to the plotting grid.

Now, whereas I mostly agree that this is desirable behaviour, I quite often have very long plot titles.

Question: Is there a way of aligning the plot title with the plot window rather than the plot grid?

I'm looking for an solution that does automatic alignment of the plot. In other words, manual alignment using hjust would not work for me (I run this on hundreds of plots for each project).

Any solution that used grid directly is also acceptable.


Some sample code and plot: (Notice how the title gets truncated at the right of window).

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)


解决方案

In ggplot2 0.9 you can easily change the layout.

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)

Perhaps, in the future version, ggplot2 will provide consistent interfaces for tweaking the layout.

这篇关于如何将ggplot标题与窗口对齐而不是绘制网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆