Fill Aesthetic not working in geom_histogram with numeric value

Viewed 398

I'm trying to make a histogram shaded with a fill aesthetic. I have to use a continuous variable for the fill aesthetic because I want to shade it using scale_fill_gradent2(). But ggplot2 just plots a gray histogram, with no fill yet throws no error. Here is a MWE:

library(ggplot2)

dat <- rnorm(1000)

ggplot() + 
  geom_histogram(aes(x=dat, fill=dat))

Creates:

enter image description here

How do I fill the histogram with a continuous color?

I'm using ggplot2 3.2.0.

1 Answers

To fill it with the x values, you do:

dat <- rnorm(1000)
ggplot() + geom_histogram(aes(x=dat,fill=..x..)) + scale_fill_viridis_c()

enter image description here

Related