I have data that has two columns - date and values. I can create density plot with geom_density function. I would like to fill it with gradient color f.e. blue to red but I can't find how to do it. Anything I find on the internet requires Y value but I only have one variable.
Example of data:
Month_day; Temperature_anomaly
01-01; -1.24
01-02; 0.31
01-03; 1.13
01-04; -0.16
Code:
ggplot(data, aes(x = Temperature_anomaly)) +
geom_density()
This creates simple density plot but have do I create single density plot and add gradient fill color based on the same value(Temperature_anomaly) to it?
An alternative solution using the {ggridges} package:
set.seed(123)
data <- data.frame(Temperature_anomaly = rnorm(20))
library(ggridges)
ggplot(data, aes(x = Temperature_anomaly, y = 1, fill = after_stat(x))) +
geom_density_ridges_gradient()+
scale_fill_gradient2(low="blue", mid = "white", high="red")
which gives: