ggplot直方图颜色渐变

Ila*_*naS 2 r ggplot2

我试图在直方图中可视化来自 ggplot2 的钻石数据,其中显示克拉的分布。

我尝试将 aes 值从 ggplot 移动到 geom_histogram(),尝试 ..fill..,并以不同的方式操作代码,但结果是相同的。

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

随着克拉的增加,我希望看到我的直方图从蓝色变为黄色,但我仍然看到它是灰色的。

C. *_*aun 5

尝试使用fill=..x..

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

带有颜色渐变的直方图

  • 谢谢!我想我对我读到的文档和其他问题感到困惑,所以当我看到 ..x.. 我想我需要用我的 x 替换 x 。 (2认同)