I am trying to create a histogram for a variable which has both negative and positive values and I want to color the bars based on the sign of the variable, i.e., red if the number is negative and green if the number is positive. I am using the following code in ggplot2:
ggplot(aes(survey_grouped3$difference),data = survey_grouped3)+geom_histogram(binwidth = 10,fill = ifelse(survey_grouped3$difference >= 0,"green","red"))
But I am getting the following error:
Aesthetics must be either length 1 or the same as the data (26): fill
Can anyone help me on this? Thanks!
Edit: used the command
ggplot(survey_grouped3, aes(difference)) +geom_histogram(aes(fill=ifelse(difference > 0,"green","red")), binwidth = 10) +scale_color_identity()
but the coloring is still not working. i am attaching the output I am getting.