gpt4 book ai didi

r - ggplot2 方面的问题并关闭剪辑

转载 作者:行者123 更新时间:2023-12-04 20:33:15 27 4
gpt4 key购买 nike

过去可以通过关闭剪辑将文本放在图边距上。在 ggplot2_2.2.0 中,这在使用分面的图中似乎不再可能(但如果不使用分面仍然有效)。我已经发布了一个问题 here但尚未得到解决。在此期间任何解决方法的想法将不胜感激!

这是一个最小(非)工作示例:

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)

最佳答案

似乎与合并 this pull request ,现在可以使用 ggplot2 进行可配置的剪辑.

我想你只需要添加 clip = "off"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"))

enter image description here

或者,正如我在 Annotate outside plot area once in ggplot with facets 中提到的,您可以使用 cowplot::draw_label :

cowplot::ggdraw(p) + cowplot::draw_label("test", x = 0.53, y = 0.13)

关于r - ggplot2 方面的问题并关闭剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41065829/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com