Place Annotation at Center of Plot with ggplot2

烈酒焚心 提交于 2019-12-20 01:37:21

问题


I'd like to place an annotation at the center of a several ggplot objects.

I've researched and found a few similar questions such as here: Relative positioning of geom_text in ggplot2?

So far, the only answer I've found is to manipulate the absolute range (e.g. ",y = ymax/2").

I'd like to add the annotation layer in a loop, prior to printing to .pdf. I can place the annotation in the corners by using +/- Inf, as follows:

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()

How can I place the annotation at the center, rather than the corner?


回答1:


library(grid) # for textGrob

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



来源:https://stackoverflow.com/questions/25488113/place-annotation-at-center-of-plot-with-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!