Skip to content
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

Using position_stack() with text geom in stat_summary() #2709

Closed
aosmith16 opened this issue Jun 21, 2018 · 2 comments · Fixed by #3416
Closed

Using position_stack() with text geom in stat_summary() #2709

aosmith16 opened this issue Jun 21, 2018 · 2 comments · Fixed by #3416
Labels
bug positions 🥇 tidy-dev-day 🤓

Comments

@aosmith16
Copy link

aosmith16 commented Jun 21, 2018

Should it be possible to "stack" text when calculating the labels and the y axis with stat_summary()? I'm getting an error using either position = "stack" or position = position_stack().

library(ggplot2)

# Using stat_summary for aggregation
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(am))) + 
    stat_summary_bin(fun.y = sum, geom="bar", position = "stack")+
    stat_summary(aes(label = stat(y)), fun.y = sum,  geom = "text", 
                 position = position_stack(vjust = .5))
#> Error in if (any(negative)) {: missing value where TRUE/FALSE needed

Things work as I expect when doing the calculation outside of ggplot2.

# Aggregation outside ggplot2
suppressPackageStartupMessages( library(dplyr) )
mtsum = mtcars %>%
    group_by(cyl, am) %>%
    summarise(mpg = sum(mpg) )

ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(am))) + 
    stat_summary_bin(fun.y = sum, geom="bar", position = "stack")+
    geom_text(data = mtsum, aes(label = mpg), position = position_stack(vjust = .5))

Created on 2018-06-21 by the reprex package (v0.2.0).

@paleolimbot
Copy link
Member

paleolimbot commented Jun 25, 2019

I think the problem is that PositionStack$compute_panel() assumes that there is a ymax column and that all of the values are non-missing:

negative <- data$ymax < 0

Not all geometries have a ymax and/or ymin (although most of the ones that are usually stacked do, like geom_bar(), which is why this isn't usually a problem). Changing this to something more like:

ymax_position <- ifelse(is.na(data$ymax), data$y, data$ymax)
negative <- ymax_position < 0

Would probably fix the problem. Given that collide() handles NA values in ymin and ymax, I think this is a good solution.

@paleolimbot paleolimbot added bug positions 🥇 tidy-dev-day 🤓 labels Jun 25, 2019
@lock
Copy link

lock bot commented Jan 13, 2020

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jan 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug positions 🥇 tidy-dev-day 🤓
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants