Help with ggplot: stacked bar plot has squished labels

Hi RStudio community.

I'm stumped on a labeling issue with my stacked bar plot.

The labels in my stacked bar plot are squished for categories that are very small.
I would like to omit the labels for categories that are too small.

Important note:

My labels are based on percentages, and those percentages are grouped calculations, whereas the counts on the y-axis are the sum of all state counts combined.

Silly Reprex Example:

library(tidyverse)
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor

tribble(
  ~state, ~surfboard_ownership, ~count,
  "Arizona","surfboard", 25,
  "Arizona", "no_surfboard", 5,
  "Idaho", "surfboard", 0,
  "Idaho", "no_surfboard", 2,
  "Washington", "surfboard", 180,
  "Washington", "no_surfboard", 250,
  "Oregon","surfboard", 100,
  "Oregon", "no_surfboard", 200,
  "California","surfboard", 275,
  "California", "no_surfboard", 1800,
  "Nevada","surfboard", 30,
  "Nevada", "no_surfboard", 250
)%>%
  group_by(state) %>%
  mutate(percent = count/sum(count)) %>%
  mutate(surfboard_ownership = factor(surfboard_ownership,
         levels = c("no_surfboard", "surfboard"))) %>%
  ggplot(aes(x = state,
             y = count,
             fill = surfboard_ownership)) +
  geom_bar(stat = "identity") +
  theme_bw() +
  geom_text(aes(label = scales::percent(percent, accuracy = 1)),
            position = position_stack(vjust = .5)) +
  labs(title = "Surfboard Ownership")

Created on 2022-06-05 by the reprex package (v2.0.1)

Additional Remarks:

As you can see, the percentage labels for Arizona, Idaho and Nevada are squished together because the bars aren't big enough. It does not make sense to have labels for these states.

Goal:

I'd like to preserve the labels for bars that are big enough to accommodate them, and omit them when the bars are too small.

I've spent a ton of time trying to fix this, but my numerous efforts thus far have been fruitless.

I'm very grateful for any help or guidance!

How's this?

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
tribble(
  ~state, ~surfboard_ownership, ~count,
  "Arizona","surfboard", 25,
  "Arizona", "no_surfboard", 5,
  "Idaho", "surfboard", 0,
  "Idaho", "no_surfboard", 2,
  "Washington", "surfboard", 180,
  "Washington", "no_surfboard", 250,
  "Oregon","surfboard", 100,
  "Oregon", "no_surfboard", 200,
  "California","surfboard", 275,
  "California", "no_surfboard", 1800,
  "Nevada","surfboard", 30,
  "Nevada", "no_surfboard", 250
)%>%
  group_by(state) %>%
  mutate(percent = count/sum(count)) %>%
  mutate(surfboard_ownership = factor(surfboard_ownership,
                                      levels = c("no_surfboard", "surfboard")),
         percent=ifelse(count < 100, NA, percent)) %>%
  ggplot(aes(x = state,
             y = count,
             fill = surfboard_ownership)) +
  geom_bar(stat = "identity") +
  theme_bw() +
  geom_text(aes(label = scales::percent(percent, accuracy = 1)),
            position = position_stack(vjust = .5)) +
  labs(title = "Surfboard Ownership")
#> Warning: Removed 5 rows containing missing values (geom_text).

Created on 2022-06-05 by the reprex package (v2.0.1)

1 Like

This is fantastic, you did it! THANK YOU! :slight_smile:

In my attempts, I was really wrapped around the axle trying to address the problem in the geom_text(), rather than upstream in the data prep! Also, I had no idea NA's would render that way.

This is a solution that will undoubtedly carry forward with me in future projects. Again, thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.