【问题标题】:Place Annotation at Center of Plot with ggplot2使用 ggplot2 在绘图中心放置注释
【发布时间】:2014-08-25 14:24:50
【问题描述】:

我想在几个 ggplot 对象的中心放置一个注释。

我研究并发现了一些类似的问题,例如:Relative positioning of geom_text in ggplot2?

到目前为止,我找到的唯一答案是操纵绝对范围(例如“,y = ymax/2”)。

我想在打印到 .pdf 之前循环添加注释层。我可以使用 +/- Inf 将注释放在角落,如下所示:

plot.list<-list()
g<- qplot(1,1)

plot.list[[length(plot.list)+1]]<-g
plot.list[[length(plot.list)+1]]<-g

pdf("MyReport.pdf"
    ,width = 14
    ,height=8.5
    ,paper="a4r")
for(i in 1:length(plot.list)){
  print(plot.list[[i]]+
          annotate("text",x=Inf,y=Inf,hjust=1,vjust=1
                   ,label="PLEASE DO NOT DISTRIBUTE"
                   ,fontface="bold",color="darkred",alpha=0.3))
}
dev.off()

如何将注释放在中心而不是角落?

【问题讨论】:

  • annotation_custom should work
  • @baptiste - 感谢您为我指明正确的方向。 annotation_custom 的帮助文本不清楚如何进行非相对定位(即不依赖数据)。我正在尝试类似: plot.list[[i]]+annotation_custom(grob=splitTextGrob("PRELIMINARY, PLEASE DO NOT DISTRIBUTE"),xmin = 1,xmax=5, ymin = 1, ymax=100) 但是这个仍然依赖于 x 和 y 尺度
  • 如果您使用 +/- Inf 作为限制,则 grob 应该跨越整个情节,因此文本应该位于中心

标签: r ggplot2 gtable


【解决方案1】:
library(grid) # for textGrob

qplot(1,1) +
    annotation_custom(grid::textGrob("there"), 
                      xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) 

【讨论】:

  • 错误:annotation_custom 仅适用于笛卡尔坐标运行rlang::last_error() 以查看错误发生的位置。看起来它不再起作用了。万岁...
猜你喜欢
  • 2016-03-25
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
相关资源
最近更新 更多