79203685

Date: 2024-11-19 13:58:03
Score: 1
Natty:
Report link

Thanks for the comments on the post, and the answer provided above. I used a combination of the two to come to a solution which works great. For geom_label_repel() and geom_label(), I used a label = ifelse() statement rather than filtering the data itself. Thanks again to the commenter and the proposed answer for helping me solidify this result.

# Set repel threshold
threshold <- 0.055

# Plot
survey %>% 
  ggplot(aes(x = pct, y = 1, fill = fct_rev(answer))) +
  geom_col(color = "black") +
  theme_minimal() +
  scale_x_continuous(labels = label_percent(),
                     # Expand so the labels aren't off-plot
                     expand = expansion(mult = c(0.025, 0.025))) +
  scale_y_discrete(labels = NULL) +
  geom_label_repel(aes(label = ifelse(pct < threshold, percent_format(accuracy = 1)(pct), NA),
                       color = fct_rev(answer)),
                   fill = "white",
                   size = 3.25,
                   fontface = "bold",
                   label.size = 1,
                   label.r = unit(2.5, "pt"),
                   show.legend = FALSE,
                   na.rm = TRUE,
                   position = position_stack(vjust = 0.5, reverse = FALSE),
                   # Set direction so that repel is only "up" or "down" on plot
                   direction = "y",
                   # Set ylim to prevent labels going off the bar
                   ylim = c(.6, 1.3),
                   # Set seed so they always place in same position
                   seed = 12345
  ) +
  geom_label(aes(label = ifelse(pct >= threshold, percent_format(accuracy = 1)(pct), NA),
                 color = fct_rev(answer)),
             fill = "white",
             size = 3.25,
             fontface = "bold",
             label.size = 1,
             label.r = unit(2.5, "pt"),
             show.legend = FALSE,
             na.rm = TRUE,
             position = position_stack(vjust = 0.5, reverse = FALSE),) +
  scale_fill_manual(values = c("tomato4", "tomato", "royalblue", "royalblue4")) +
  scale_color_manual(values = c("tomato4", "tomato", "royalblue", "royalblue4"), guide = "none") +
  guides(fill = guide_legend(position = "bottom", nrow = 2, reverse = TRUE)) +
  labs(
    title = NULL,
    subtitle = NULL,
    caption = paste("Respondents N =", survey[1,]$respondents),
    fill = NULL,
    color = NULL,
    x = NULL,
    y = NULL
  )

fixed plot with correctly repelled labels

It also ended up working for all 20-ish plots in my .Rmd file, with some modifications here and there to the seed = argument to get them to place where I wanted.

final plot from my document

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Morrigan