简体   繁体   English

ggplot2:stat_summary中的多种颜色

[英]ggplot2: multiple colours in stat_summary

I have a plot in which I am displaying individual values from multiple subjects, coloured by group. 我有一个情节,其中我显示来自多个科目的个别值,按组着色。 Added to that are means per group, calculated using stat_summary. 除此之外,还有每组的均值,使用stat_summary计算。

I would like the two means to be coloured by group, but in colours other than the individual data. 我希望这两种方法可以按组进行着色,但是使用的颜色不同于单个数据。 This turns out to be difficult, at least when using stat_summary. 事实证明这很困难,至少在使用stat_summary时。 I have the following code: 我有以下代码:

ggplot(data=dat, 
       aes(x=Round, y=DV, group=Subject, colour=T1)) + 
  geom_line() + geom_point() + theme_bw() +
  stat_summary(fun.y=mean, geom="line", size=1.5,
               linetype="dotted", color="black",
               aes(group=T1))

Which produces this example graph . 这产生了这个示例图

The colour for the means created by stat_summary is set to black; stat_summary创建的均值的颜色设置为黑色; otherwise it would be red and blue like the individual data lines. 否则它会像个别数据线一样呈红色和蓝色。 However, it is not possible to set more than one colour - so color=c("black", "blue") does not work. 但是,无法设置多种颜色 - 因此color = c(“black”,“blue”)不起作用。

I've already tried scale_colour_manual as explained here , but this will change the colours of the individual data lines, leaving the mean lines unaffected. 我已经尝试过scale_colour_manual作为解释这里 ,但是这将改变个人数据线的颜色,使平均线未受影响。

Any suggestion how to solve this? 有什么建议如何解决这个问题? Code and data here . 代码和数据在这里

You need to create different values for the mapping to color: 您需要为要映射的颜色创建不同的值:

ggplot(data=iris, 
       aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + 
  geom_line() + geom_point() + theme_bw() +
  stat_summary(fun.y=mean, geom="line", size=1.5,
               linetype="dotted", aes(color=paste("mean", Species)))

结果情节

You can then use scale_color_manual to get specific colors. 然后,您可以使用scale_color_manual来获取特定颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM