I am trying to gradient shade under a density plot in R, using ggplot2. I keep getting the density plot, but no shading.
library(quantmod)
library(ggplot2)
library(ggfortify)
getSymbols('XLE')
energy <- coredata(Delt(XLE$XLE.Adjusted, k = 1)["2018-03-08::"])
ggplot(energy, aes(energy)) +
geom_density(aes(x = energy, fill = energy))+
scale_fill_gradient2( energy ,
low = "darkred", high = "navy", mid = "orange", midpoint = 0)
This yields a curve with no fill at all.
Here is a similar example with reproducible data:
test.data <- data.frame(exp(runif(1000,0,1)))
ggplot(test.data, aes(test.data))+
geom_density(aes(fill = test.data)) +
scale_fill_gradient(test.data, low = "navy", high = "red")
which yields