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
stat_summary transforms first before applying summary function (rather than vice versa) #2804
I expected stat_summary to summarise data first then transform the summary for display.
What actually happens: the data is transformed and then the summary is calculated.
In the reprex below, the mean at x=0 is definitely 1, but when you use a transformed axis, the mean of the transformed values doesn't match that (because the mean of the transformed data is different from the transform of the mean of the data).
Is my expectation wrong here on the order of operations? Should I be doing something completely different to achieve my goal?
Is this is a ggplot2 bug? a feature? a limitation of the architecture/grammar-of-graphics? Or am I just doing the wrong thing?
library(tidyverse)
plot_df<-data.frame(
x= rep(1:3, each=7),
y= c(0, 0, 0, 0, 1, 1, 5, 1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8)
)
p<- ggplot(plot_df, aes(x, y)) +
geom_point(alpha=0.4) +
stat_summary(fun.y=mean, geom='line') +
theme_bw()
# The linear version correctly shows mean at x=0 is 1p+ scale_y_continuous(breaks=1:9) +
ggtitle("Using stat_summary to draw a mean line\nNote: at x=1 mean=1 (correct)")
# Transformed axis version has incorrect mean at x=0p+ scale_y_sqrt(breaks=1:9) +
ggtitle("Using stat_summary to draw a mean line\nNow with transformed y axis\nNote: at x=1 mean != 1 (incorrect)")
Mh, that's a tough one.
I think the curved line is technically a more correct representation of the data, but I can see why you might want to have it straight. In that case, you might have to compute the summary yourself first an then do it by hand? If you have enough ggplot2 mad skillz then a custom geom/stat might do the trick, yes.
The idea that statistics are computed on the scaled data seems to be part of the foundations of the Grammar of Graphics, so I think it's unlikely that this would change.
This is from Wilkinson's book:
Of course, I can't speak on behalf of the ggteam so I'll let them answer.
@eliocamp I thought that might be the case - i.e. the order of ops is set by the grammar of graphics which means that the thing I want to do is "disallowed"
In my custom stat/geom I've worked around it by using the scales$y$trans$inverse to apply the inverse transform to the data and then work on that. This won't work when a transform isn't completely invertible.
The new idea i'm looking at is asking ggplot2::transform_df() function to keep the original values as well as the transformed ones. Then I use a flag to ask stat_summary to calculate on the original values rather than the transformed ones.
This probably wrecks the purity of the grammar workflow
You could always use a summary function that undoes the coordinate transformation. Though I would probably just summarize beforehand using dplyr. In any case, I don't see the need for changes in the ggplot2 code base.
Summarising beforehand in dplyr isn't convenient when you may be facetting/grouping and drawing multiple lines. It then means you have to keep your dplyr group_by/summarisation code in sync with your ggplot facetting/grouping code - which will be error prone.
And the idea of a function that undoes the coordinate transform is what I (sort of) currently do, but this fails when the transform creates infinities (which are thrown away by default in compute_layer).
e.g. if we do a log transform on the y axis, then all the zeros become infinities and are removed from the stat calculation, and we get the incorrect mean at x=1 is (5+1+1)/3 = 2.3
You’re asking for a fundamental change to the way that ggplot2 works for a fairly small enhancement that can be achieved with dplyr. The cost-benefit ratio of this proposed change means that it’s not a good fit for ggplot2.
Would you be open to considering a small change in an internal data structure which would allow 3rd parties to access the original untransformed data in custom Stats/Geoms?
Something like this change in scales_transform_df which would keep the original data during the scaling process?
Activity
eliocamp commentedon Aug 5, 2018
I think that for that case you need to apply the transform in the coordinates, not on the scales. This seems to work.
Created on 2018-08-05 by the reprex package (v0.2.0).
coolbutuseless commentedon Aug 5, 2018
I was just about to update this issue with that plot :)
I don't want the curved lines that this gives. Which makes me think I should stick with my current custom geom/stat.
But I'm interested to see if there's a way to switch the transform/summary ordering, or if the maintainers are interested in such a PR.
eliocamp commentedon Aug 5, 2018
Mh, that's a tough one.
I think the curved line is technically a more correct representation of the data, but I can see why you might want to have it straight. In that case, you might have to compute the summary yourself first an then do it by hand? If you have enough ggplot2 mad skillz then a custom geom/stat might do the trick, yes.
The idea that statistics are computed on the scaled data seems to be part of the foundations of the Grammar of Graphics, so I think it's unlikely that this would change.

This is from Wilkinson's book:
Of course, I can't speak on behalf of the ggteam so I'll let them answer.
coolbutuseless commentedon Aug 5, 2018
@eliocamp I thought that might be the case - i.e. the order of ops is set by the grammar of graphics which means that the thing I want to do is "disallowed"
In my custom stat/geom I've worked around it by using the
scales$y$trans$inverseto apply the inverse transform to the data and then work on that. This won't work when a transform isn't completely invertible.The new idea i'm looking at is asking
ggplot2::transform_df()function to keep the original values as well as the transformed ones. Then I use a flag to ask stat_summary to calculate on the original values rather than the transformed ones.This probably wrecks the purity of the grammar workflow
clauswilke commentedon Aug 5, 2018
You could always use a summary function that undoes the coordinate transformation. Though I would probably just summarize beforehand using dplyr. In any case, I don't see the need for changes in the ggplot2 code base.
Created on 2018-08-05 by the reprex package (v0.2.0).
coolbutuseless commentedon Aug 5, 2018
Hi @clauswilke
Summarising beforehand in dplyr isn't convenient when you may be facetting/grouping and drawing multiple lines. It then means you have to keep your dplyr group_by/summarisation code in sync with your ggplot facetting/grouping code - which will be error prone.
And the idea of a function that undoes the coordinate transform is what I (sort of) currently do, but this fails when the transform creates infinities (which are thrown away by default in compute_layer).
e.g. if we do a log transform on the y axis, then all the zeros become infinities and are removed from the stat calculation, and we get the incorrect mean at x=1 is (5+1+1)/3 = 2.3
Created on 2018-08-06 by the reprex
package (v0.2.0).
hadley commentedon Aug 6, 2018
You’re asking for a fundamental change to the way that ggplot2 works for a fairly small enhancement that can be achieved with dplyr. The cost-benefit ratio of this proposed change means that it’s not a good fit for ggplot2.
coolbutuseless commentedon Aug 6, 2018
Hi @clauswilke, @hadley
Fair enough.
Would you be open to considering a small change in an internal data structure which would allow 3rd parties to access the original untransformed data in custom Stats/Geoms?
Something like this change in
scales_transform_dfwhich would keep the original data during the scaling process?lock commentedon Feb 2, 2019
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/