过去可以通过关闭裁剪功能将文本放置在绘图边距上。在ggplot2_2.2.0中,在使用构面的图中似乎不再可行(但如果不使用构面,则仍然可行)。我在这里发布了一个问题,但尚未解决。在此期间,任何解决方法的想法将不胜感激!
这是一个最小的(非工作)示例:
library(ggplot2)
library(grid)
df.plot = data.frame(x = 1, y = 1, facet = 'facet', stringsAsFactors = F)
df.text = data.frame(x = 1, y = -0.3, label = 'test', facet = 'facet', stringsAsFactors = F)
p = ggplot(df.plot,aes(x = x, y = y))+
facet_grid(~facet)+ # 'test' is only printed outside of the plot if faceting is turned off
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)+
theme(plot.margin=unit(c(2,2,2,2),"cm"))
gt = ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] = "off"
grid.draw(gt)
Run Code Online (Sandbox Code Playgroud)
似乎通过合并此拉取请求,现在可以使用ggplot2.
我想你只需要添加clip = "off"的coord_cartesian功能。这样应该可以解决做gt = ggplot_gtable(ggplot_build(p))休耕的需要gt$layout$clip = "off"。
也就是说,这应该足够了(用ggplot23.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"))
Run Code Online (Sandbox Code Playgroud)
或者,正如我在使用 facets 在 ggplot中注释外部绘图区域中提到的那样,您可以使用cowplot::draw_label:
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"))
Run Code Online (Sandbox Code Playgroud)
