Smooth colors in geom_line

2019-04-16 02:36发布

问题:

Using ggplot2, I would like color lines "smoothly" in a plot where I have only few datapoints. As is, the scales I tried (e.g. scale_color_gradient2) do not seem to interpolate colors, but instead color segments monochromatically.

Code example:

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

回答1:

You could increase the amount 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()


标签: r colors ggplot2