Search code examples
rggplot2geom-bar

Using expand_limits in ggplot when counts are not a column in the dataset (R)


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.

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.

mtcars_cyl_counts <- as.data.frame(table(mtcars$cyl))
colnames(mtcars_cyl_counts)[1:2] <- c("cyl", "counts")

mtcars_cyl_counts %>% 
  arrange(desc(counts)) %>%
  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))

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:

mtcars %>%
  group_by(cyl) %>%
  summarize(counts = n()) %>%
  arrange(-counts) %>%
  mutate(cyl = factor(cyl, cyl)) %>%
  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))

However, this returns the following error:

Error in data.frame(..., stringsAsFactors = FALSE) : object 'counts' not found

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.

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?

(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...)


Solution

  • If this helps Megan,

    mtcars %>%
        count(cyl) %>% 
        arrange(-n) %>% 
        mutate(cyl = factor(cyl, cyl)) %>% 
        ggplot(aes(cyl, n)) +
        geom_text(vjust = -0.5, aes(label = n)) +
        geom_bar(stat = "identity") +
        expand_limits(y = max(table(mtcars$cyl) * 1.05))