0

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.

jira
  • 13
  • 2
  • 8
  • It's easier to help you if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can actually run the code to see what it looks like. – MrFlick Mar 20 '17 at 23:36
  • Tried my best above, thanks – jira Mar 21 '17 at 00:06
  • Possible duplicate: http://stackoverflow.com/questions/9613578/changing-standard-error-color-for-geom-smooth . If you want to change the ribbon color, use `fill=`, not `color=` – MrFlick Mar 21 '17 at 03:07
  • I don't know. I tried that before and again now: When I replace `colour=condition` by `fill=condition`, I get all black error bars and coloured regression lines and funnels (pink and green). When I also replace `scale_colour_grey` by `scale_fill_grey`, the funnels change the way I want to, but all error bars are still black and both regression lines blue... – jira Mar 21 '17 at 09:22
  • As for the duplicate suggestion: I think the question is similar but his case must be different. The solution there doesn't work for me, I tried it before. – jira Mar 21 '17 at 09:57

1 Answers1

2

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

Djork
  • 3,319
  • 1
  • 16
  • 27
  • Thank you very much! I didn't realize I had to enter **both** `colour` and `fill` as well as `scale_colour_grey` and `scale_fill_grey`. That works now. You're right, I do want to distinguish the linetypes. Can you tell me how? I tried it in several ways, I added `linetype_size_shape=condition` to `ggplot` and modified `errorbar` in `stat_summary(fun.data=mean_cl_boot, geom="errorbar")` but nothing changes. `geom_errorbar(aes(linetype = condition)))` isn't accepted...(I'm very new to ggplot2). Thanks in advance! – jira Mar 21 '17 at 09:52
  • @jira I edited my answer to include linetype, it is set in `(aes(linetype = condition)`, and can be set in the parent ggplot as I did above that changes linetype for both errorbar and regression line or individually within `stat_summary` or `geom_smooth` if you want only the errorbars or the regression line to change linetype. I also recommend faceting when data overlap too much. Please accept the answer if you are satisfied so this can be marked as answered. – Djork Mar 22 '17 at 17:56
  • In my real data, the funnels don't actually overlap as much. My reproducible example is just not quite the same. Thank you once again. – jira Mar 23 '17 at 20:13
  • Your example works well for me. But for the sake of trying, I added `linetype(values=c("solid", "dashed"))` to `geom_smooth` or `stat_summary`. It only works to change *both* regression lines and *all* errorbar identically by adding `lty="dashed"` to `geom_smooth` or `stat_summary`. Apparently, differentiation doesn't work like this... – jira Mar 23 '17 at 20:22