【问题标题】:ggplot2 - change colour in confidence intervalggplot2 - 在置信区间内改变颜色
【发布时间】:2017-03-23 01:52:31
【问题描述】:

我想将置信区间的颜色变成与它们所属的误差线相同的颜色。

 rat3 <- rep(c(1:6,6:2,3:1,2:4), 20)
 logRT <- rep(seq(from=6, to=7.6, by=0.1), 20)
 condition <- rep(c("c","i"),170)          
 condition <- as.factor(condition)           #turns to 1-2, is c-i in my data
 meto <- cbind(rat3, logRT, condition)
 meto <- as.data.frame(meto)             #this produces a df similar to mine

这是情节的代码:

  barmeto <- ggplot(meto, aes(rat3, logRT, colour=condition)) #assign 
  barmeto + geom_smooth(method="lm") + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", lwd=1.5) +
  scale_colour_grey(start=0.15, end=0.5)

它会生成带有 2 个相同浅灰色的漏斗的图像。抱歉,除了这样,我不能上传、嵌入或发布图片链接: https://stats.stackexchange.com/questions/268740/ggplot2-change-colour-of-ci

我需要两个漏斗有不同的颜色。很短的应该是黑色的,就像它所属的误差线一样(短漏斗黑色),长的应该是深灰色的,就像它的误差线一样。

如果有任何帮助,我将不胜感激,谢谢!到目前为止,我在这里找到的所有食谱都不适合我。

【问题讨论】:

  • 如果您提供带有示例输入数据的reproducible example 会更容易为您提供帮助,这样我们就可以实际运行代码来查看它的样子。
  • 在上面尽力了,谢谢
  • 可能重复:stackoverflow.com/questions/9613578/…。如果要更改功能区颜色,请使用fill=,而不是color=
  • 我不知道。我现在一次又一次地尝试过:当我用fill=condition 替换colour=condition 时,我得到所有黑色的误差线和彩色回归线和漏斗(粉红色和绿色)。当我还将scale_colour_grey 替换为scale_fill_grey 时,漏斗会改变我想要的方式,但所有误差线仍然是黑色的,两条回归线都是蓝色的......
  • 至于重复的建议:我认为问题相似,但他的情况必须不同。那里的解决方案对我不起作用,我之前尝试过。

标签: r plot ggplot2


【解决方案1】:

要重现您链接到的所需绘图,您应该将aes 填充和颜色设置为分解条件,并添加scale_fill_grey

# factor condition variable in meto
meto$condition <- factor(meto$condition)

# plot with fill and colour aes
barmeto <- ggplot(meto, aes(rat3, logRT, colour=condition, fill=condition))
barmeto + geom_smooth(method="lm") + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", lwd=1.5) +
  scale_colour_grey(start=0.15, end=0.5) +
  scale_fill_grey(start=0.15, end=0.5)

由于您的回归线在此示例案例中重叠,您可能还希望为每个条件设置线型或线宽。您的灰度值也非常接近彼此和背景,请尝试更多不同的值和/或设置为 theme_bw()。

例如,下面带有大小 aes 和 theme_bw() 的代码会生成下面的图:

barmeto <- ggplot(meto, aes(rat3, logRT, colour=condition, fill=condition, size=condition))
barmeto + geom_smooth(method="lm") + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar") +
  scale_colour_grey(start=0.1, end=0.5) +
  scale_fill_grey(start=0.1, end=0.5) + 
  scale_size_manual(values=c(2,1)) +
  theme_bw()

编辑回答评论问题,编辑线型你可以在aes上设置,线型选择可以在这里看到:http://www.cookbook-r.com/Graphs/Shapes_and_line_types/

barmeto <- ggplot(meto, aes(rat3, logRT, colour=condition, fill=condition, size=condition, linetype=condition))
barmeto + geom_smooth(method="lm") + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar") +
  scale_colour_grey(start=0.1, end=0.5) +
  scale_fill_grey(start=0.1, end=0.5) + 
  scale_size_manual(values=c(2,1)) +
  scale_linetype_manual(values=c("solid", "twodash")) +
  theme_bw()

你仍然有回归线的 se 重叠的问题,在这种情况下,你可能更喜欢按条件进行 facet wrap:

barmeto <- ggplot(meto, aes(rat3, logRT, colour=condition, fill=condition))
barmeto + geom_smooth(method="lm") + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", lwd=1.5) +
  scale_colour_grey(start=0.15, end=0.5) +
  scale_fill_grey(start=0.15, end=0.5) +
  facet_wrap(~condition) + theme_bw()

【讨论】:

  • 非常感谢!我没有意识到我必须输入 both colourfill 以及 scale_colour_greyscale_fill_grey。现在可以了。你是对的,我确实想区分线型。你能告诉我怎么做吗?我尝试了几种方式,我将linetype_size_shape=condition添加到ggplot并在stat_summary(fun.data=mean_cl_boot, geom="errorbar")中修改了errorbar,但没有任何变化。 geom_errorbar(aes(linetype = condition))) 不被接受......(我对 ggplot2 很陌生)。提前致谢!
  • @jira 我编辑了我的答案以包含线型,它设置在(aes(linetype = condition) 中,并且可以像我在上面所做的那样在父 ggplot 中设置,更改误差线和回归线的线型或单独在其中stat_summarygeom_smooth 如果您只希望误差条或回归线更改线型。我还建议在数据重叠过多时进行分面。如果您满意,请接受答案,以便将其标记为已回答。
  • 在我的真实数据中,漏斗实际上并没有太多重叠。我的可重现示例并不完全相同。再次感谢您。
  • 你的例子很适合我。但为了尝试,我将linetype(values=c("solid", "dashed")) 添加到geom_smoothstat_summary。它只能通过将lty="dashed" 添加到geom_smoothstat_summary 来相同地更改both 回归线和all 误差条。显然,分化不是这样工作的……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多