Smooth colors in geom_line

Using ggplot2 , I would like the colored lines to be โ€œsmoothlyโ€ on the graph, where I only have a few data points left. As with the scaling I tried (e.g. scale_color_gradient2 ), it seems not to interpolate the colors, but instead the color segments are monochromatic.

Code example:

 ggplot(data.frame(x=1:5)) + geom_line(aes(x=x, y=x, color=x), size=3) + scale_color_gradient2() 

enter image description here

+1
source share
1 answer

You can increase the number of points between 1 and 5:

 df <- data.frame(x=seq(1,5,0.001)) ggplot(df) + geom_line(aes(x=x, y=x, color=x), size=3) + scale_color_gradient2() 
+2
source

Source: https://habr.com/ru/post/949854/


All Articles