title: Using expand_limits in ggplot when counts are not a column in the dataset (R)
body: <p>I have a dataset and I need to plot a bar chart of the counts of the different outcomes of a certain column. For this example I am using the mtcars dataset. </p>
  
  <p>When I first attempted this, I found that the labels on the bars were getting cut off at the top, so I used the expand_limits argument to give them more space. As I want to be able to use this code for refreshed data, the limits might change, which is why I've used the max() function. </p>
  
  <pre><code>mtcars_cyl_counts &lt;- as.data.frame(table(mtcars$cyl))
  colnames(mtcars_cyl_counts)[1:2] &lt;- c("cyl", "counts")
  
  mtcars_cyl_counts %&gt;% 
    arrange(desc(counts)) %&gt;%
    ggplot(aes(x = reorder(cyl, -counts), y = counts)) +
    geom_bar(stat = "identity") +
    geom_text(aes(label = comma(counts), vjust = -0.5), size = 3) +
    expand_limits(y = max((mtcars_cyl_counts$counts) * 1.05))
  </code></pre>
  
  <p>This works fine, but it seemed unnecessarily cumbersome to create a separate table of counts, and made some of the future code more complicated, so I redid this:</p>
  
  <pre><code>mtcars %&gt;%
    group_by(cyl) %&gt;%
    summarize(counts = n()) %&gt;%
    arrange(-counts) %&gt;%
    mutate(cyl = factor(cyl, cyl)) %&gt;%
    ggplot() +
    geom_bar(aes(x = cyl, y = counts), stat = "identity") +
    geom_text(aes(x = cyl, y = counts, label = comma(counts), vjust = -0.5), size = 3) +
    expand_limits(y = max((counts) * 1.05))
  </code></pre>
  
  <p>However, this returns the following error: </p>
  
  <blockquote>
    <p>Error in data.frame(..., stringsAsFactors = FALSE) : 
      object 'counts' not found</p>
  </blockquote>
  
  <p>I get that 'counts' is not technically in the mtcars dataset (which is why it also doesn't work if I use mtcars$counts), but it's what I've used elsewhere in the code for y definitions. </p>
  
  <p>So, is there a way to write this so that it works, or an alternative way to expand the vertical limits in a way that will adapt to different datasets?</p>
  
  <p>(NB: with these examples, the bar labels don't get cut off because they aren't very big, but for the purpose of this I just need the limits expanded so that detail is not critically important to the working...)</p>