是的,這是可能的。我沒有使用element_text
,而是使用grid
包中的textGrob
。我認爲這對添加特定註釋更加靈活。這應該工作:
library(grid)
#Grob to store your text
title_text <- textGrob("Title",x=0.5,y=-0.2,gp=gpar(col="white",fill="black"))
#Dynamic Grob to store your background box - size adjusts to size of your title
title_box <- rectGrob(x=title_text$x,y=title_text$y,
width = unit(3,"mm")+unit(1,"grobwidth",title_text),
height=unit(3,"mm")+unit(1,"grobheight",title_text),
gp=gpar(col="black",fill="black"))
#Plot, adding in the grobs
p<-ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()+theme(plot.title=element_text(vjust=0.5, hjust=0.5),
plot.margin = unit(c(1,1,5,1), "lines")) +
annotation_custom(grobTree(title_box,title_text))
#Creating Gtable so we can override clipping and place the on the bottom
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
#Drawing our plot
grid.draw(gt)
[這](http://stackoverflow.com/questions/10014187/displaying-text-below-the-plot-generated-by-ggplot2)可能是有用的 – bouncyball