GGPlot - issue with fill=..count.. for using gradient scales

Hi all

Having an issue in ggplot.
I am trying to put a gradient scale on my histogram, however I am encountering an error which states:

Error: Aesthetics must be valid computed stats. Problematic aesthetic(s): fill = ..count...
Did you map your stat in the wrong layer?

The data in the dataframe is just a column with continuous numbers from 0 to 204

Here is my ggplot code
df2 %>% ggplot(aes(data,fill=..count..)) + geom_histogram(binwidth = 1)+ scale_fill_gradient(low="lightblue",high="blue")+ geom_text(label="binwidth = 1",x = 50,y = 3000)+ theme_bw()

The weird thing is when I use qplot, it comes out perfectly:

qpplot <- qplot(df2$data, fill=..count.., geom="histogram",xlim=c(0,204)) qpplot + scale_fill_gradient(low="lightblue",high="darkblue")

What am i doing wrong with my ggplot coding?



I think he problem is with the geom_text() remove it and see that your code works.
I fixed it with

 geom_text(aes(fill=NULL),label = "binwidth = 1", 

i.e. set fill to null for that layer

Thank you very much, that did the trick!

Why would that cause the error? Is it something to do with it trying to do a count for that layer?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.