ggplot2图例:geom_point和geom_line的不同颜色

时间:2014-08-04 21:26:03

标签: r ggplot2 legend

我正在尝试制作包含点和线的图表;在一组只有点的图形中,我一直在使用一组一致的颜色/填充/形状组合作为我的分组因子(条件)的级别,所以我想在这个图表中保持相同。但是,我现在还想添加一些行,并希望手动指定行颜色。到现在为止还挺好;麻烦的是,我真的希望图形有一个图例,显示点的颜色/填充/形状以及线条的颜色和线条类型。有没有人对如何实现这一点有任何想法,也许有新的指南传奇的东西(我尚未包裹我的脑袋)?任何建议将不胜感激。

(另外,如果这有助于解释为什么我试图以这种方式做事,数据包含从条件B开始的样本,并被转移到条件C;我试图强调dv会发生什么当进行改变时。作为参考,我也在整个实验过程中显示了条件B或条件C中保存的样品的数据,但我不希望这些线条如此突出,因为它们不是曲线图。)

如何获取图例中的线条颜色以匹配图形中的线条颜色?

enter image description here

mydata <- structure(list(Condition = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 5L, 5L, 6L), .Label =
c("CondA", "CondB", "CondC", "CondD", "BeforeChange", "AfterChange"), class =
"factor"), Day = c(1, 3, 6, 9, 12, 15, 18, 21, 1, 3, 6, 9, 12, 15, 18, 21, 1,
3, 6, 9, 12, 15, 18, 21, 13, 14, 14), dv = c(7.18272766270665,
7.42730999263847, 7.66391016865676, 7.602389725654, 7.39221477975877,
7.46192935291398, 7.62075582981643, 7.47839284216667, 6.56987476062938,
6.69091456174267, 6.6697752192633, 6.35890571005243, 6.93927313468223,
6.65712651759238, 6.93055110758838, 7.0391827047215, 7.15066983259602,
7.59782467658902, 7.59892456901464, 7.42888298870538, 7.49983551294577,
7.30476290695126, 6.99896760719176, 6.97838387933162, 7.434811, 7.369787,
7.369787 ), se = c(0.152197789175902, 0.0396497303055128, 0.0506439629227802, 
0.109997525500822, 0.114500535946067, 0.0736412492173867, 0.111473540926106, 
0.0808019418670817, 0.133004775453176, 0.134977834863898, 0.167639712420762, 
0.25651178518586, 0.0717077961089037, 0.164745732598605, 0.171383889660418, 
0.133281180227772, 0.230898256311926, 0.178695661660643, 0.0427233153730758, 
0.105571418251551, 0.10389399069655, 0.110566732681714, 0.152512756205137, 
0.0759939810936424, 0.106119, 0.108343, 0.108343)), .Names = c("Condition", "Day",
"dv", "se"), row.names = 9:35, class = "data.frame")

require(ggplot2)
require(scales)
require(grid)

ggplot() +
  theme(legend.key.width = unit(2.5, "lines")) + 
  scale_fill_manual(values = c("CondB" = "white", "CondC" = "black", "BeforeChange" = "white", "AfterChange" = "black"), breaks = c("CondB", "CondC", "BeforeChange", "AfterChange")) + 
  scale_color_manual(values = c("CondB" = "grey30", "CondC" = "grey50", "BeforeChange" = "grey30", "AfterChange" = "grey50"), breaks = c("CondB", "CondC", "BeforeChange", "AfterChange")) + 
  scale_shape_manual(values = c("CondB" = 22, "CondC" = 21, "BeforeChange" = 22, "AfterChange" = 21), breaks = c("CondB", "CondC", "BeforeChange", "AfterChange")) + 
  scale_linetype_manual(values = c("CondB" = "dashed", "CondC" = "longdash", "BeforeChange" = "solid", "AfterChange" = "solid"), breaks = c("CondB", "CondC", "BeforeChange", "AfterChange")) + 

  geom_line(data = mydata[mydata$Condition %in% c("CondB"), ], aes(x=as.numeric(as.character(Day)), y=dv, group=Condition, linetype = Condition), color = "grey60") + 
  geom_line(data = mydata[mydata$Condition %in% c("CondC"), ], aes(x=as.numeric(as.character(Day)), y=dv, group=Condition, linetype = Condition), color = "grey50") + 
  geom_line(data = mydata[mydata$Condition %in% c("BeforeChange"), ], aes(x=as.numeric(as.character(Day)), y=dv, linetype = Condition), color = "grey30") + 
  geom_line(data = mydata[mydata$Condition %in% c("AfterChange"), ], aes(x=as.numeric(as.character(Day)), y=dv, linetype = Condition), color = "black") + 

  geom_point(data = mydata[mydata$Condition %in% c("CondB", "CondC", "BeforeChange", "AfterChange") & mydata$Day %in% c(1, 3, 6, 9, 12, 15, 18, 21), ], aes(x=as.numeric(as.character(Day)), y=dv, color = Condition, fill = Condition, shape = Condition), size=3) + 

  scale_y_continuous(breaks = 5:8, labels = math_format(10^.x)) + 
  geom_segment(aes(x=14, y=7.77, xend=14, yend=7.41), arrow=arrow(length=unit(0.3, "cm"), type = "closed", angle = 20), size=0.7) 

1 个答案:

答案 0 :(得分:0)

您可以使用override.aes中的guide_legend覆盖图例的美学效果。但是,我最初在更改颜色方面遇到了麻烦,因为我通常使用color而不是colour而且在这里没有用。

guides(linetype = guide_legend(override.aes = list(colour = c("grey60", "grey50", "grey30", "black"))))

这样做的缺点是颜色也会因点的轮廓而改变,并且您的图例点不再与图形完全匹配。您可以考虑使线条与点轮廓颜色相同。