繁体   English   中英

ggplot2-在置信区间中更改颜色

[英]ggplot2 - change colour in confidence interval

我想将置信区间的颜色与其所属的误差线的颜色相同。

 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)

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

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

我将不胜感激,谢谢! 到目前为止,我在这里找到的任何食谱都没有对我有用。

要重现链接到的所需图,应将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()

在此处输入图片说明

编辑为回答评论问题,要编辑线型,您可以在es上设置它,可以在这里看到线型选择: 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()

您仍然会遇到回归线重叠的问题,在这种情况下,您可能只希望按条件进行换行:

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()

在此处输入图片说明

暂无
暂无

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

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