Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to apply color gradient to geom_smooth with ggplot in R?

Tags:

r

ggplot2

I would like to apply the scale colour gradient also to the smooth line. At the moment the code below set the color fix to red.

library(ggplot2)
a <- data.frame(year = 1:100, values = sin(1:100)*1000 + runif(100))
ggplot(a, aes(x = year, y = values, color = values )) + geom_line(size = 2)  +
scale_colour_gradient2(
  low = "blue",
  mid = "white" ,
  high = "red",
  midpoint = 10
 )+
geom_smooth(
 data = a,
 aes(x = year, y = values),
 color = "red",
 size = 2
 )

enter image description here

But when I set color = values it doesn't work. Instead it takes the default blue.

geom_smooth(
 data = a,
 aes(x = year, y = values, color = values),
 size = 2
 )

enter image description here

Thanks in advance.

like image 399
Martin S Avatar asked May 15 '16 18:05

Martin S


People also ask

How to change colour gradient in ggplot?

Key functions to change gradient colors The default gradient colors can be modified using the following ggplot2 functions: scale_color_gradient() , scale_fill_gradient() for sequential gradients between two colors. scale_color_gradient2() , scale_fill_gradient2() for diverging gradients.

How to change color in ggplot in R?

Change ggplot colors by assigning a single color value to the geometry functions ( geom_point , geom_bar , geom_line , etc). You can use R color names or hex color codes. Set a ggplot color by groups (i.e. by a factor variable). This is done by mapping a grouping variable to the color or to the fill arguments.

How to change color theme in ggplot?

Change colors manually A custom color palettes can be specified using the functions : scale_fill_manual() for box plot, bar plot, violin plot, etc. scale_color_manual() for lines and points.

How do I change color in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.


1 Answers

Use geom_smooth(aes(color=..y..)) to add a color aesthetic to geom_smooth. ..y.. is the vector of y-values internally calculated by geom_smooth to create the regression curve. In general, when you want to add an aesthetic to a summary value that's calculated internally, you need to map the aesthetic to that internal value. Here, the internal value is the ..y.. value of the smoothing function. In other cases it might be ..count.. for histograms or bar plots, or ..density.. for density plots.

Here's an example using your data. Note that I've tweaked a few of the plot parameters for illustration.

set.seed(48)
a <- data.frame(year = 1:100, values = sin(1:100)*1000 + runif(100))

ggplot(a, aes(x = year, y = values, color = values )) + 
  geom_line(size = 0.5)  +
  geom_smooth(aes(color=..y..), size=1.5, se=FALSE) +
  scale_colour_gradient2(low = "blue", mid = "yellow" , high = "red", 
                         midpoint=10) +
  theme_bw()

enter image description here

Note that the color of the regression line does not change much because its y-values span a small range relative to the data. Here's another example with fake data that generates a more wide-ranging regression curve.

set.seed(1938)
a2 <- data.frame(year = seq(0,100,length.out=1000), values = cumsum(rnorm(1000)))

ggplot(a2, aes(x = year, y = values, color = values )) + 
  geom_line(size = 0.5)  +
  geom_smooth(aes(color=..y..), size=1.5, se=FALSE) +
  scale_colour_gradient2(low = "blue", mid = "yellow" , high = "red", 
                         midpoint=median(a2$values)) +
  theme_bw()

enter image description here

like image 115
eipi10 Avatar answered Nov 15 '22 01:11

eipi10