Bruno Ferreira
Bruno Ferreira

Reputation: 484

Trouble coloring histogram

I'm currently trying to color a range of values on a histogram using 'ggplot2'. For this example I’ll use the ‘diamonds’ dataset.

when I execute the follow comand:

qplot(carat, data=diamonds,geom="histogram", binwidth=0.01, fill=..count..) 
+ scale_fill_continuous(low="#F79420", high="#F79420", limits=c(1000,3000))

I get thw following and correct plot:

enter image description here

But when I use another syntax with equivalent code, I cannot get the same result. Code:

ggplot(diamonds, aes(x = carat)) + 
geom_histogram(
    binwidth = 0.01,
    fill=aes(y = ..count..)
    ) +
scale_fill_continuous(low="#F79420", high="#F79420", limits=c(1000,3000))

Result:

enter image description here

Can you please tell me what I’m doing wrong?

Thanks.

Upvotes: 0

Views: 53

Answers (1)

coffeinjunky
coffeinjunky

Reputation: 11514

Try this:

 ggplot(diamonds, aes(x = carat)) + 
  geom_histogram(
     binwidth = 0.01,
     aes(fill = ..count..)
   ) +
   scale_fill_continuous(low="#F79420", high="#F79420", limits=c(1000,3000))

This will give you the same picture as the first one above.

Upvotes: 1

Related Questions