Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change standard error color for geom_smooth

Tags:

I'm plotting some data using geom_smooth and looking for a way to change the color of the standard error shading for each line to match that line (ie., a red line would have it's standard error shaded red).

I've looked through the official ggplot2 documentation as well as the list of opts() at https://github.com/hadley/ggplot2/wiki/%2Bopts%28%29-List.

Any advice (or just confirmation of whether or not it's possible) is appreciated.

like image 385
Bryan Avatar asked Mar 08 '12 06:03

Bryan


People also ask

What is the GREY area in Geom_smooth?

The grey area [SE=TRUE] would be a zone that covers 95% confidence level [that the values will be within that area]. https://ggplot2.tidyverse.org/reference/geom_smooth.html [Level=95% by default, can be increased to 99%].

What does Geom_smooth () using formula YX mean?

The warning geom_smooth() using formula 'y ~ x' is not an error. Since you did not supply a formula for the fit, geom_smooth assumed y ~ x, which is just a linear relationship between x and y.

What does Stat_smooth method lm do?

stat_smooth: Add a smoother.Aids the eye in seeing patterns in the presence of overplotting.


2 Answers

Your (understandable) mistake is to think that you should be changing the color rather than the fill. The standard error shadings are made with geom_ribbon essentially, and they are a 2d area, so the "color" they are "filled" with is determined by fill, not colour.

Try:

geom_smooth(aes(...,fill = variable)) 

where variable is the same one you map to colour elsewhere.

like image 182
joran Avatar answered Sep 18 '22 15:09

joran


If you have multiple groups you simple define color = Vars and group = Vars within ggplot(aes()), and then additional aes(fill = Vars) within geom_smooth(aes(fill = Species)). (Based on answer here)

Dummy example:

# Make confidence intervals the same color as line by group ggplot(iris, aes(x = Sepal.Length,                   y = Sepal.Width,                   group = Species,                  color = Species)) +    geom_point() +    geom_smooth(aes(fill = Species))  # additional `aes()` referencing to confidence interval as a `fill` not as a `color`  

enter image description here

like image 28
maycca Avatar answered Sep 17 '22 15:09

maycca