Assigning continuous fill color to geom_bar

2019-08-19 15:55发布

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.

I am trying this:

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:

rplot

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.

1条回答
Root(大扎)
2楼-- · 2019-08-19 16:26

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

Complete code:

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)

查看更多
登录 后发表回答