Skip to content

geom_label does not work as expected with a stat of "summary" and a label aesthetic variable #6465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidhodge931 opened this issue May 20, 2025 · 2 comments

Comments

@davidhodge931
Copy link

geom_label does not work as expected with a stat of "summary".

Instead, you have to use after_stat, which seems inconsistent with the grammar in how the x and y aesthetics are treated where you don't do this.

Could geom_label with a stat of "summary" work with label aesthetic variable?

library(tidyverse)
library(tidyplots)

#doesn't
spendings |>
  ggplot(aes(y = category, x = amount, label = amount)) +
  geom_bar(stat = "summary", fun = \(x) sum(x)) +
  geom_label(stat = "summary", fun = \(x) sum(x)) 
#> Warning: Removed 5 rows containing missing values or values outside the scale range
#> (`geom_label()`).

  
#works
spendings |>
  ggplot(aes(y = category, x = amount, label = after_stat(x))) +
  geom_bar(stat = "summary", fun = \(x) sum(x)) +
  geom_label(stat = "summary", fun = \(x) sum(x))

Created on 2025-05-21 with reprex v2.1.1

@teunbrand
Copy link
Collaborator

Hi David, thanks for raising the issue!

seems inconsistent with the grammar

Can you elaborate a little bit on how this is inconsistent?

Could geom_label with a stat of "summary" work with label aesthetic variable?

Generally, we avoid specific interactions between stats and geoms. Especially with stats that are as generic as the summary stat.

@davidhodge931
Copy link
Author

Actually rereading the help for stat_summary, and this makes sense - as it doesn't operate on aesthetics other than x or y.

"stat_summary() operates on unique x or y;"

Whoops, I should read the help first!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants