使用ggplot2在图表中央放置注释

4
我希望在几个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 应该可以工作 - baptiste
@baptiste - 感谢您指引我正确的方向。annotation_custom 的帮助文本对于如何进行非相对定位(即不依赖数据)并不清晰。我正在尝试类似这样的东西:plot.list[[i]]+annotation_custom(grob=splitTextGrob("初步,请勿分发"),xmin = 1,xmax=5, ymin = 1, ymax=100) 但是这仍然依赖于 x 和 y 刻度。 - Tim_K
1
如果您使用+/- Inf作为限制,则grob应跨越整个绘图,因此文本应放置在中心。 - baptiste
1个回答

9
library(grid) # for textGrob

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

enter image description here


错误:annotation_custom仅适用于笛卡尔坐标系。 运行rlang::last_error()以查看发生错误的位置。看起来它不再起作用了。好耶... - Benbob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接