title: How to color a ggplot histogram differently based on precise cut off points?
body: <p>I'm trying to color a ggplot histogram differently based on precise boundaries along the x axis. However, the colors are not accurate because a bin that contains values from both colors will show up as a mixed color bin split up horizontally. Example minimal code and problem chart below.</p>
  
  <p>I would like to split the bin by color vertically. So that all values to the left of the cutoff line are one color and all values to the right of the cutoff line are the other color.</p>
  
  <p>How can I accomplish this?</p>
  
  <p>I think geom_density would not have this problem, but I would prefer to use geom_histogram instead of geom_density because the histogram shows actual counts on the y axis.</p>
  
  <pre><code>cutoff_point &lt;- 3.9
  mtcars %&gt;% 
    mutate(wt_color = ifelse(wt &lt; cutoff_point, "red", "blue")) %&gt;% 
    select(wt, wt_color) %&gt;% 
    ggplot(aes(x=wt, fill = wt_color)) +
    geom_histogram(bins = 5) +
    geom_vline(xintercept=cutoff_point, colour="black")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/H8sXZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H8sXZ.png" alt="enter image description here"></a></p>
  
  <p>The <code>boundary</code> argument works well when I have just one cutoff point, but it doesn't work when I have two cutoff points like below</p>
  
  <pre><code>cutoff_point1 &lt;- 2.5
  cutoff_point2 &lt;- 5.4
  
  mtcars %&gt;% 
    mutate(wt_color = case_when(
      wt &lt; cutoff_point1 ~ "blue",
      wt &gt; cutoff_point1 &amp; wt &lt; cutoff_point2 ~ "red",
      TRUE ~ "green"
      )) %&gt;% 
    select(wt, wt_color) %&gt;% 
    ggplot(aes(x=wt, fill = wt_color)) +
    geom_histogram(bins = 5, boundary=cutoff_point) +
    geom_vline(xintercept=cutoff_point, colour="black")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/gRtzu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gRtzu.png" alt="enter image description here"></a></p>