如何替换geom_text指南(图例)中的“符号”(bullet)?

6

我希望替换geom_text的图例(指引)中的“子弹”。现在是一个倾斜的a,但我想要一个大胖圆圈或正方形或任何其他形状,以强调颜色(更多)。

library(ggplot2)

majdf <- data.frame(lvl = rep(c("A", "B"), each = 50), val = c(rnorm(50, 1), rnorm(50, 3)))
majtxt <- data.frame(species = c("sp1", "sp2", "sp3"), geq = c(0.01, 2, 2.2))

ggplot(majdf, aes(x = val)) +
  geom_density() +
  geom_vline(data = majtxt, aes(xintercept = geq)) +
  geom_text(data = majtxt, aes(x = geq, y = 0.2, label = geq, color = species), angle = 90) +
  facet_wrap(~ lvl)

enter image description here

2个回答

6

这只是一个技巧。

使用geom_point创建图表:

p1 <- ggplot(majdf, aes(x = val)) +
  geom_density() +
  geom_vline(data = majtxt, aes(xintercept = geq)) +
  geom_point(data = majtxt, aes(x = geq, y = 0.2, label = geq, color = species), angle = 90) +
  facet_wrap(~ lvl)

geom_text相同:
p2 <- ggplot(majdf, aes(x = val)) +
  geom_density() +
  geom_vline(data = majtxt, aes(xintercept = geq)) +
  geom_text(data = majtxt, aes(x = geq, y = 0.2, label = geq, color = species), angle = 90) +
  facet_wrap(~ lvl)

变成一个Grob并找到哪个元素是指南:

g1 <- ggplotGrob(p1)
g1

TableGrob (8 x 10) "layout": 13 grobs
    z         cells       name                                     grob
1   0 ( 1- 8, 1-10) background          rect[plot.background.rect.2339]
2   1 ( 4- 4, 4- 4)    panel-1                gTree[panel-1.gTree.2263]
3   2 ( 4- 4, 7- 7)    panel-2                gTree[panel-2.gTree.2278]
4   3 ( 3- 3, 4- 4)  strip_t-1    absoluteGrob[strip.absoluteGrob.2306]
5   4 ( 3- 3, 7- 7)  strip_t-2    absoluteGrob[strip.absoluteGrob.2312]
6   5 ( 4- 4, 3- 3)   axis_l-1 absoluteGrob[axis-l-1.absoluteGrob.2299]
7   6 ( 4- 4, 6- 6)   axis_l-2         zeroGrob[axis-l-2.zeroGrob.2300]
8   7 ( 5- 5, 4- 4)   axis_b-1 absoluteGrob[axis-b-1.absoluteGrob.2285]
9   8 ( 5- 5, 7- 7)   axis_b-2 absoluteGrob[axis-b-2.absoluteGrob.2292]
10  9 ( 7- 7, 4- 7)       xlab             text[axis.title.x.text.2314]
11 10 ( 4- 4, 2- 2)       ylab             text[axis.title.y.text.2316]
12 11 ( 4- 4, 9- 9)  guide-box                        gtable[guide-box]
13 12 ( 2- 2, 4- 7)      title               text[plot.title.text.2337]

复制指南:

g2 <- ggplotGrob(p2)
g2[[1]][[12]] <- g1[[1]][[12]]
plot(g2)

enter image description here


这是否提供了所请求的大圆形/正方形? - Tyler Rinker

5
ggplot(majdf, aes(x = val)) +
  geom_point(data = majtxt, aes(x = geq, colour = species), 
             y = 0.2, size = 0) +
  geom_density() +
  geom_vline(data = majtxt, aes(xintercept = geq)) +
  geom_text(data = majtxt, aes(x = geq, y = 0.2, label = geq, color = species), 
            angle = 90, show_guide = FALSE) +
  facet_wrap(~ lvl) +
  scale_colour_discrete(guide=guide_legend(override.aes=list(size=4)))

enter image description here

如何操作:添加一个点几何图形,并使用适当的颜色映射。这将在图例中添加一个点。但是,为了防止它出现在绘图中,请将点的大小设置为0。在文本几何图形中,告诉它不要将那部分(旋转的a)添加到图例中(show_guide = FALSE)。最后,图例只会显示您想要的点而不是侧向的a;不幸的是,它以与绘图相同的大小绘制,即0。因此,使用guide_legendoverride.aes参数(该参数传递给scale_colour_discrete中的guide),将点的大小设置为“大”。

这种方法不需要将组成两个不同的图并将它们拼接在一起。

指定指南参数的另一种替代方法是使用guides函数而不是将其作为参数传递给scale_colour_manual

ggplot(majdf, aes(x = val)) +
  geom_point(data = majtxt, aes(x = geq, colour = species), 
             y = 0.2, size = 0) +
  geom_density() +
  geom_vline(data = majtxt, aes(xintercept = geq)) +
  geom_text(data = majtxt, aes(x = geq, y = 0.2, label = geq, color = species), 
            angle = 90, show_guide = FALSE) +
  facet_wrap(~ lvl) +
  guides(colour = guide_legend(override.aes=list(size=4)))

生成的图形是相同的。

如果您将图例分成多列,请确保通过比例尺传递列数,例如 scale_colour_discrete(guide = guide_legend(nrow = 2, override.aes = list(size = 4))),否则会出现混乱。向 R 聊天室的 Didzis 致敬:http://chat.stackoverflow.com/transcript/message/14908487#14908487 - Roman Luštrik
有人弄清楚为什么 override.aes 和 geom_text 不兼容了吗? - baptiste

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接