title: R: Creating histogram bins to represent gaps in data
body: <p>Is there a way to create bins that represent &quot;artificial&quot; gaps in a range of data using &quot;case_when&quot;? The script I wrote works somewhat, but I can't get it to plot my specified gaps in the data. My attempts are getting ignored:</p>
  <p>My data:</p>
  <pre><code>&gt; table(df$den)
  
                  0 0.333333333333333 0.666666666666667                 1  1.33333333333333 
                507               155                77                43                18 
   1.66666666666667                 2  2.33333333333333                 3  3.33333333333333 
                  7                 5                 8                 3                 1 
                  4  4.33333333333333  5.66666666666667  8.33333333333333 
                  1                 1                 2                 1 
  </code></pre>
  <p>Generate bins:</p>
  <pre><code>df = df %&gt;% mutate(
    bins = case_when(
      den == 0 ~ &quot;0&quot;,
      den &lt;= 1 ~ &quot;0 - 1&quot;,
      den &lt;= 2 ~ &quot;1 - 2&quot;,
      den &lt;= 3 ~ &quot;2 - 3&quot;,
      den &lt;= 4 ~ &quot;3 - 4&quot;,
      den &lt;= 5 ~ &quot;4 - 5&quot;,
      den &lt;= 6 ~ &quot;5 - 6&quot;,
      den &lt;= 8 ~ &quot;...&quot;,
      TRUE ~ &quot;8.0+&quot; 
    )
  )
  </code></pre>
  <p>But the gaps get ignored:</p>
  <pre><code>&gt; bins &lt;- as.data.frame(table(df$bins))
  &gt; bins
     Var1 Freq
  1     0  507
  2 0 - 1  275
  3 1 - 2   30
  4 2 - 3   11
  5 3 - 4    2
  6 4 - 5    1
  7 5 - 6    2
  8  8.0+    1
  </code></pre>
  <p>I had hoped to achieve something like this, but the &quot;case_when&quot; function won't create it:</p>
  <pre><code>&gt;bins
         Var1 Freq
      1     0  507
      2 0 - 1  275
      3 1 - 2   30
      4 2 - 3   11
      5 3 - 4    2
      6 4 - 5    1
      7 5 - 6    2
      8 &quot;...&quot;    NA
      9  8.0+    1
  </code></pre>
  <p>other methods don't work either and are a pain to create:</p>
  <pre><code>df$bins &lt;- cut(df$den, c(0, seq(0.001, 9, 1)), right = FALSE,
                  labels=c(&quot;0&quot;, &quot;0.001 - 1.001&quot;, &quot;1.002 - 2.001&quot;, &quot;2.002 - 3.001&quot;, 
                           &quot;3.002 - 4.001&quot;, &quot;4.002 - 5.001&quot;, &quot;5.002 - 6.001&quot;, 
                           &quot;...&quot;, &quot;8.0+&quot;)) 
      
  </code></pre>
  <p>ggplot:</p>
  <pre><code>  ggplot(bins, aes(x = Var1, y = Freq, fill = Var1)) + 
        geom_bar(stat = &quot;identity&quot;, colour=&quot;black&quot;) + 
        geom_text(aes(label = Freq), vjust = -0.5) +
        scale_fill_brewer(breaks = c(&quot;0&quot;, &quot;0-1&quot;,&quot;1-2&quot;, &quot;2-3&quot;,&quot;3-4&quot;, &quot;4-5&quot;, &quot;5-6&quot;, &quot;...&quot;, &quot;8+&quot;))
    
  </code></pre>