r - Applying a gradient fill on a density plot in ggplot2 -


is possible add gradient fill density plot in ggplot2, such color in chart changes along x-axis? in example below, fill argument appears ignored.

library(ggplot2) ggplot(data.frame(x = rnorm(100)), aes(x = x, fill = x)) + geom_density() 

there's option compute density hand:

set.seed(123) x <- rnorm(100) y <- density(x, n = 2^12) ggplot(data.frame(x = y$x, y = y$y), aes(x, y)) + geom_line() +    geom_segment(aes(xend = x, yend = 0, colour = x)) +    scale_color_gradient(low = 'green', high = 'red') 

enter image description here


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -