ggplot 直方图 颜色渐变

Posted

技术标签:

【中文标题】ggplot 直方图 颜色渐变【英文标题】:ggplot histogram color gradient 【发布时间】:2019-08-22 02:13:07 【问题描述】:

我正在尝试在直方图中可视化来自 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')

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

【问题讨论】:

【参考方案1】:

试试fill=..x..:

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

【讨论】:

谢谢!我想我对阅读的文档和其他问题感到困惑,所以当我看到 ..x.. 时,我想我需要用我的 x 替换 x。

以上是关于ggplot 直方图 颜色渐变的主要内容,如果未能解决你的问题,请参考以下文章