Lani
Lani

Reputation: 107

ggplot histogram color gradient

I am trying to visualize the diamonds data from ggplot2 in a histogram where I show the distribution of carats.

I've tried to move the aes values from ggplot to geom_histogram(), to try ..fill.., and to manipulate the code in different ways but the result is the same.

histogram<- ggplot(diamonds, aes(x=carat, fill=carat)) +
  geom_histogram(binwidth = 0.1) + scale_fill_gradient(low='blue', high='yellow')

I would expect to see my histogram go from blue to yellow as the carats increase but I still see it in grey.

Upvotes: 2

Views: 1259

Answers (1)

C. Braun
C. Braun

Reputation: 5201

Try with fill=..x..:

ggplot(diamonds, aes(x=carat, fill=..x..)) +
  geom_histogram(binwidth = 0.1) + scale_fill_gradient(low='blue', high='yellow')

histogram with color gradient

Upvotes: 5

Related Questions