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_bin strange behavior when combined with scale_y_* #2880
Attempting to do a binned summation of data via fun.y with stat_summary_bin and represent as a line. I get strange y-axis labels when combining with a scale_y_* aesthetic. It looks like a bug to me, but perhaps I misunderstand how to use stat_summary_bin.
This issue seems the same as #2804 (comment). stat_summary_bin() summarises the values after transformation, which is a kind of implementational limitation so is unlikely to change.
You can restore the transformation (e.g. 10^x), then summarise (e.g. sum()), and finally transform (e.g. log10()) again. But, it would be easier to summarise data before passing it to ggplot2...
Thanks for the explanation @yutannihilation. The workaround makes sense and after reading that issue I understand that changing this behavior would be a big undertaking. I think a warning or explanation in the documentation for stat_summary-bin would be helpful for future reference.
It's nice to be able to do this operation with stat_summary_bin rather than summarizing beforehand because defining the bins and axis labels for passing to ggplot can get pretty annoying.
Ah, sorry, I was wrong! During search for the document about this, I found we can use coord_trans(). ?stat_summary says:
# Transforming the scale means the data are transformed# first, after which statistics are computed:p1<-m2+ scale_y_log10()
# Transforming the coordinate system occurs after the# statistic has been computed. This means we're calculating the summary on the raw data# and stretching the geoms onto the log scale. Compare the widths of the# standard errors.p2<-m2+ coord_trans(y="log10")
So, in this case you could write like p2 in the following code:
Oh wow, it is in the docs after all... but hidden in an example. I do think this nuance regarding the transform --> stat vs. stat-->transform order of operations should be described in the Summary functions section.
Thanks for pointing out these two options for resolving the issue.
I think just a note about using stat_summary* with scale/coordinate transformations should suffice. The existing comments in the example explain it in necessary depth. Slightly reworded:
Note that scale transformations occur before statistics are computed, while coordinate system transformations occur after statistics are computed.
Activity
yutannihilation commentedon Sep 5, 2018
This issue seems the same as #2804 (comment).
stat_summary_bin()summarises the values after transformation, which is a kind of implementational limitation so is unlikely to change.You can restore the transformation (e.g.
10^x), then summarise (e.g.sum()), and finally transform (e.g.log10()) again. But, it would be easier to summarise data before passing it to ggplot2...Created on 2018-09-05 by the reprex package (v0.2.0).
mkoohafkan commentedon Sep 5, 2018
Thanks for the explanation @yutannihilation. The workaround makes sense and after reading that issue I understand that changing this behavior would be a big undertaking. I think a warning or explanation in the documentation for
stat_summary-binwould be helpful for future reference.It's nice to be able to do this operation with
stat_summary_binrather than summarizing beforehand because defining the bins and axis labels for passing toggplotcan get pretty annoying.yutannihilation commentedon Sep 5, 2018
Ah, sorry, I was wrong! During search for the document about this, I found we can use
coord_trans().?stat_summarysays:So, in this case you could write like
p2in the following code:Created on 2018-09-05 by the reprex package (v0.2.0).
yutannihilation commentedon Sep 5, 2018
IIUC, the summary of #2804 is:
coord_trans().mkoohafkan commentedon Sep 5, 2018
Oh wow, it is in the docs after all... but hidden in an example. I do think this nuance regarding the
transform --> statvs.stat-->transformorder of operations should be described in the Summary functions section.Thanks for pointing out these two options for resolving the issue.
yutannihilation commentedon Sep 5, 2018
Agreed that there should be a section for this in the doc of
stat_summary()andstat_summary_bin().But...,
transform --> statvs.stat-->transformseems more general topic. I have no idea where this fits...mkoohafkan commentedon Sep 5, 2018
I think just a note about using
stat_summary*with scale/coordinate transformations should suffice. The existing comments in the example explain it in necessary depth. Slightly reworded:teunbrand commentedon Feb 2, 2024
Closing this issue for the reason above.