jira
jira

Reputation: 13

ggplot2 - change colour in confidence interval

I want to turn the color of the confidence intervals the same color as the error bars they belong to.

 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

This is the code for the plot:

  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)

It produces an image with 2 funnels of the same light gray. Sorry I can neither upload nor embed nor post a link to the picture other than like this: https://stats.stackexchange.com/questions/268740/ggplot2-change-colour-of-ci

I need the two funnels to have different colours. The very short one should be black like the error bars it belongs to (the short funnel black), the long one darker gray, like its error bars.

I'd appreciate any help, thanks! So far none of the recipes I found here worked for me.

Upvotes: 0

Views: 8800

Answers (1)

Djork
Djork

Reputation: 3369

To reproduce the desired plot you linked to, you should set your aes fill and colour to factored condition, and add a 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)

Because your regression lines overlaps in this sample case, you might also like to set a linetype or linewidth per condition. Your grey values are also quite close to each other and the background, try more distinct values and/or set to theme_bw().

For example the code below with size aes and theme_bw() produces the plot below:

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

enter image description here

Edited to answer comment question, to edit linetype you can set it on aes, linetypes selections can be seen here: 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()

You still have a problem of the se of the regression line overlapping, in which case you might just prefer to facet wrap by condition:

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

enter image description here

Upvotes: 2

Related Questions