如何替换geom_text指南(图例)的传奇“子弹”

时间:2014-02-23 17:47:54

标签: r ggplot2

我想替换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 个答案:

答案 0 :(得分:5)

这只是一个黑客攻击。

使用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

答案 1 :(得分:4)

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

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

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

指定引导参数的另一种方法是使用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)))

生成的图形是相同的。