麻烦着色直方图

时间:2015-03-16 23:24:30

标签: r ggplot2

我目前正在尝试使用'ggplot2'为直方图上的一系列值着色。对于这个例子,我将使用'钻石'数据集。

当我执行以下命令时:

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

我得到了正确的情节:

enter image description here

但是当我使用具有等效代码的另一种语法时,我无法获得相同的结果。代码:

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))

结果:

enter image description here

你能告诉我我做错了吗?

感谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

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

这将为您提供与上面第一张相同的图片。

相关问题