Error in ggplot2 histogram: color based on a variable
<p>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:</p>

<pre><code>ggplot(aes(survey_grouped3$difference),data = survey_grouped3)+geom_histogram(binwidth = 10,fill = ifelse(survey_grouped3$difference &gt;= 0,"green","red"))
</code></pre>

<p>But I am getting the following error:</p>

<pre><code>Aesthetics must be either length 1 or the same as the data (26): fill
</code></pre>

<p>Can anyone help me on this? Thanks!</p>

<p>Edit: used the command</p>

<pre><code>ggplot(survey_grouped3, aes(difference)) +geom_histogram(aes(fill=ifelse(difference &gt; 0,"green","red")), binwidth = 10) +scale_color_identity()
</code></pre>

<p>but the coloring is still not working. i am attaching the output I am getting.</p>

<p><a href="http://i.stack.imgur.com/RWNRI.png" rel="nofollow">link to histogram</a></p>