看来随着合并这个拉取请求 https://github.com/tidyverse/ggplot2/pull/2539,现在可以进行可配置的裁剪ggplot2
.
我想你只需要添加clip = "off"
in the coord_cartesian
功能。所以这应该解决做的需要gt = ggplot_gtable(ggplot_build(p))
其次是gt$layout$clip = "off"
.
也就是说,这应该足够了(测试过ggplot2
版本3.1.0):
p = ggplot(df.plot,aes(x = x, y = y))+
facet_grid(~facet)+
geom_point()+
geom_text(data = df.text,aes(x=x,y=y,label=label))+
coord_cartesian(xlim = c(0, 2),ylim=c(0,2),expand=F, clip = "off")+ # added clip = "off"
theme(plot.margin=unit(c(2,2,2,2),"cm"))
或者,正如我在在 ggplot 中使用构面在绘图区域外注释一次 https://stackoverflow.com/a/54189807/5193830,你可以利用cowplot::draw_label
:
cowplot::ggdraw(p) + cowplot::draw_label("test", x = 0.53, y = 0.13)