The ggridges library has your answer.
Here is some reproducible data:
library(ggplot2)
library(ggridges)
input = runif(1000, 0, 1)
output = exp(input) + cos(input)^runif(1000,0,1)
Now we need to create a dummy-factor, wrap it all up in a data frame, then label the dummy variable as the factor:
name = rep("Name", length(output))
data = data.frame(input, output, name)
data$name = as.factor(data$name)
From here we can build the density plot:
ggplot(data, aes(x=output, y=name, fill=..x..))+
geom_density_ridges_gradient()+
scale_fill_gradient(low="orange", high="navy")
Which yields a gradient-filled density plot based on the x-axis values. Of course, from this point you can add/subtract whatever you want on the graph using the ggplot2 library.
Gradient Filled Density Plot - Example