如何将ggplot注释添加到我的图形传奇中?

时间:2017-11-21 12:59:34

标签: r ggplot2 legend mean

我正在尝试将我的ggplot注释放入图例中。目前,我的数字看起来像这样:

ggplot image

我添加的水平线代表每种浓度的平均值。如何将其添加到图例中,以便每种颜色都有一条水平线?

这是我的代码:

 ggplot(all,aes(x=Concentration, y=Distance, colour=Genotype))+
 geom_jitter(width=0.3, height=0, show.legend=TRUE)+
 theme(panel.background = element_rect(fill = "white")) +
 theme(axis.line.x = element_line(color = "black"),
    axis.line.y = element_line(color = "black"))+
 scale_y_continuous(breaks=seq(0, 8, 1))+
 xlab("Caffeine concentration (mM)")+
 ylab("Distance travelled (arbitrary units)")+
 annotate("segment", x=0.8,xend = 1.2, y= 2.86, yend = 2.86, 
 colour="black")+
 annotate("segment", x=1.8,xend = 2.2, y= 1.86, yend = 1.86, colour="black")+
  annotate("segment", x=2.8,xend = 3.2, y= 1.11, yend = 1.11, colour="black")+
  annotate("segment", x=0.8,xend = 1.2, y= 2.41, yend = 2.41, colour="green3")+
  annotate("segment", x=1.8,xend = 2.2, y= 2.16, yend = 2.16, colour="green3")+
  annotate("segment", x=2.8,xend = 3.2, y= 2.24, yend = 2.24, colour="green3")+
  scale_colour_manual(values = c("Q24" = "black", "Q35" = "green3"))

示例数据:

Concentration   Distance    Genotype
0   1   Q24
0   4   Q24
5   1   Q24
5   0   Q24
10  0   Q24
10  1   Q24
0   1   Q35
0   3   Q35
0   4   Q35
5   0   Q35
5   1   Q35
10  0   Q35
10  2   Q35

2 个答案:

答案 0 :(得分:0)

你要小心过度使用ggplot中的注释。如上所述here,它们“对于添加小注释很有用”。在您的情况下,您尝试显示的数据非常多,据我所知,在这种情况下添加图例没有简单的方法。

更简单的方法是使用ggplot中的geom_segment函数。首先,我们为注释行创建一个数据框:

data <- data.frame(group = c("One", "One", "Two", "Two", "Two"), 
                   start = c(1.8, 2.8, 0.8, 1.8, 2.8), 
                   finish = c(2.2, 3.2, 1.2, 2.2, 3.2), 
                   y = c(1.86, 1.11, 2.41, 2.16, 2.24))

看起来像这样:

group start finish    y
One   1.8    2.2 1.86
One   2.8    3.2 1.11
Two   0.8    1.2 2.41
Two   1.8    2.2 2.16
Two   2.8    3.2 2.24

只是绘制线条:

ggplot(data) +
  geom_segment(aes(x = start, y = y, xend = finish, yend = y, colour = group))  +
  scale_colour_manual(values = c("black", "green3"))

enter image description here

答案 1 :(得分:0)

尝试使用geo_segment()

all <- read.table(header = TRUE, text = "
                  Concentration   Distance    Genotype
0   1   Q24
0   4   Q24
5   1   Q24
5   0   Q24
10  0   Q24
10  1   Q24
0   1   Q35
0   3   Q35
0   4   Q35
5   0   Q35
5   1   Q35
10  0   Q35
10  2   Q35
                  ")


seg <- data.frame(x = c(0.8, 1.8, 0.8, 1.8), y = c(2.86, 1.86, 2.41, 2.16),
                  xend = c(1.2, 2.2, 1.2, 2.2), yend = c(2.86, 1.86, 2.41, 2.16),
                  color = c("Q24", "Q24", "Q35", "Q35"))

library(ggplot2)
ggplot(all,aes(x=Concentration, y=Distance, colour=Genotype))+
    geom_jitter(width=0.3, height=0, show.legend=TRUE)+
    theme(panel.background = element_rect(fill = "white")) +
    theme(axis.line.x = element_line(color = "black"),
          axis.line.y = element_line(color = "black"))+
    scale_y_continuous(breaks=seq(0, 8, 1))+
    xlab("Caffeine concentration (mM)")+
    ylab("Distance travelled (arbitrary units)")+
    geom_segment(data = seg, aes(x = x, y = y, xend = xend, yend = yend, color = color)) +
    scale_colour_manual(values = c("Q24" = "black", "Q35" = "green3"))