You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Possible bug in stage()/after_stat() with scale transformations. #4155
I think I might have found a bug with either the scales or the stage()/after_stat() functions in combination with dates.
I came across this when making a custom stat where I wanted to put default_aes = aes(xmin = after_stat(start), xmax = after_stat(end)) in the ggproto methods, where start and end are computed variables.
What happens is that when I try to apply a stage() or after_stat() to a date variable, it gives me an error that the date transformation is invalid. I don't think it is a problem with stats, as it also shows up with stat_identity().
The attempted plot below is not very interesting, but it is the most minimal example I could come up with.
library(ggplot2)
ggplot(economics) +
geom_point(aes(x= stage(date, after_stat=x),
y=unemploy))
#> Error: Invalid input: date_trans works with objects of class Date only
The error above doesn't happen for integer or numeric data.
And also the error can be rescued by formatting the output as a date again, but this seems overly verbose and seems difficult to anticipate in a custom stat.
The same is true for other geoms and aesthetics as well. Here are similar examples with after_stat() instead of stage().
ggplot(economics) +
geom_segment(aes(x=date, xend= after_stat(x+30),
y=unemploy, yend=unemploy))
#> Error: Invalid input: date_trans works with objects of class Date only
I'm yet to figure out what's happening, but probably we need to somehow bypass this transformation in the end of map_statistic() because the non-stat value should be already transformed.
Actually, abusing retransform param makes this work (this parameter seems meaningless as it is always TRUE in the current version, btw). I guess you are facing more complex case than this example. Could you try retransform <- FALSE works in your case?
Yes, the retransform trick totally works in my case, see example below. Note that the after_stat() occurs in the default_aes ggproto member and not in the layer mapping argument.
I never noticed the retransform setting, but it seems useful to my problem. Bit selfish/off-topic question maybe, but would there be any downsides to setting retransform = FALSE at the ggproto level?
I still have the gut feeling that the after_stat() + date class should work by default though, but I guess it is not a common use case. Another idea might also be to adjust the scales::date_trans()$transform() method to accept any numeric input, but that might cause more problems than it fixes.
library(ggh4x) # https://github.com/teunbrand/ggh4x#> Loading required package: ggplot2p<- ggplot(economics, aes(date)) +
stat_rle(
aes(label=date> as.Date("1980-01-01") &date< as.Date("1990-01-01")),
align="center"
) +
geom_line(aes(y=unemploy))
# Gives error
print(p)
#> Error: Invalid input: date_trans works with objects of class Date onlyp$layers[[1]]$stat$retransform<-FALSE# Output is fine
print(p)
I still have the gut feeling that the after_stat() + date class should work by default though
Yes, I totally agree with you! Sorry probably my comment was confusing. I meant
The staged values should be bypass the transformation as probably it is already transformed (I'm not 100% sure about this yet, though...).
Though we need to introduce a proper fix here, retransform, which seems an unused parameter, is a quick and dirty way to test if bypassing works. I didn't mean this is a workaround, but just wanted to see there's more thing to consider in more complex cases.
would there be any downsides to setting retransform = FALSE at the ggproto level?
I bet there's no downsides. It appears only the following two places:
Ah yes that result indeed doesn't make sense, so the bug applies to non-date transformations too. I should probably generalise the title of the issue for any transformation. As an aside, shouldn't the 0-break/label have been rendered too?
changed the title [-]Possible bug in `stage()`/`after_stat()` with dates.[/-][+]Possible bug in `stage()`/`after_stat()` with scale transformations.[/+]on Aug 15, 2020
Activity
yutannihilation commentedon Aug 14, 2020
I'm yet to figure out what's happening, but probably we need to somehow bypass this transformation in the end of
map_statistic()because the non-stat value should be already transformed.ggplot2/R/layer.r
Lines 326 to 330 in 3be0acc
Actually, abusing
retransformparam makes this work (this parameter seems meaningless as it is alwaysTRUEin the current version, btw). I guess you are facing more complex case than this example. Could you tryretransform <- FALSEworks in your case?Created on 2020-08-14 by the reprex package (v0.3.0)
teunbrand commentedon Aug 14, 2020
Yes, the retransform trick totally works in my case, see example below. Note that the
after_stat()occurs in thedefault_aesggproto member and not in the layer mapping argument.I never noticed the
retransformsetting, but it seems useful to my problem. Bit selfish/off-topic question maybe, but would there be any downsides to settingretransform = FALSEat the ggproto level?I still have the gut feeling that the
after_stat()+ date class should work by default though, but I guess it is not a common use case. Another idea might also be to adjust thescales::date_trans()$transform()method to accept any numeric input, but that might cause more problems than it fixes.Created on 2020-08-14 by the reprex package (v0.3.0)
yutannihilation commentedon Aug 14, 2020
Yes, I totally agree with you! Sorry probably my comment was confusing. I meant
retransform, which seems an unused parameter, is a quick and dirty way to test if bypassing works. I didn't mean this is a workaround, but just wanted to see there's more thing to consider in more complex cases.I bet there's no downsides. It appears only the following two places:
ggplot2/R/stat-.r
Lines 52 to 56 in 112f960
ggplot2/R/layer.r
Lines 328 to 330 in 3aa2937
yutannihilation commentedon Aug 15, 2020
Another example why I think this should be fixed. The
valuebecomes 4 because it gets sqrt-ed twice.teunbrand commentedon Aug 15, 2020
Ah yes that result indeed doesn't make sense, so the bug applies to non-date transformations too. I should probably generalise the title of the issue for any transformation. As an aside, shouldn't the 0-break/label have been rendered too?
[-]Possible bug in `stage()`/`after_stat()` with dates.[/-][+]Possible bug in `stage()`/`after_stat()` with scale transformations.[/+]yutannihilation commentedon Sep 6, 2020
Good catch, thanks! I've filed another issue here: #4193
teunbrand commentedon Jun 16, 2022
Great stuff @yutannihilation, thanks for solving the issue!
yutannihilation commentedon Jun 17, 2022
Thank you, too! Your exploration really helped. I'm glad that we solved this at last!