bar分配连续的填充颜色

bar分配连续的填充颜色

本文介绍了为geom_bar分配连续的填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从以下格式的数据中生成条形图:

I am generating a bar chart from data that is in the following format:

count | month
------|-----------
1000  | 2012-01-01
10000 | 2012-02-01

我正在尝试为条形图分配连续的颜色.我希望将颜色渐变用作条形的填充颜色.

I am trying to assign continuous colors to my bar chart. I want a gradient of colors to be used as the fill color of the bars.

我正在尝试:

ggplot(userData, aes(month, count)) +
    geom_bar(stat = "identity") +
    scale_x_date() +
    # scale_fill_gradient(low="blue", high="red") +
    scale_fill_continuous(low="blue", high="red", limits=c(0,6500000)) +
    labs(x= "Time", y="Count")

但是,我没有得到理想的结果,并且图表中的条形仍然保持灰色,如下所示:

However, I am not getting the desired result and the bars in the chart still stays gray as can be seen below:

我尝试同时使用scale_fill_continuousscale_fill_gradient,但是没有成功.

I have tried with both scale_fill_continuous, and scale_fill_gradient, but without success.

我不确定我在这里到底缺少什么.

I am not sure exactly what I am missing here.

推荐答案

您尚未将填充美学指定给任何对象:aes(month, count, fill = count)应该可以解决问题.

You haven't assigned the fill aesthetic to anything: aes(month, count, fill = count) should do the trick.

完整代码:

ggplot(userData, aes(month, count, fill = count)) +
    geom_bar(stat = "identity") +
    scale_x_date() +
    scale_fill_continuous(low="blue", high="red") +
    labs(x= "Time", y="Count")

(限制现在可能已无用,但随时可以将其重新添加)

(limits are probably useless now, but feel free to add them back)

这篇关于为geom_bar分配连续的填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:43