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
Documentation for plotting bin proportion in geom_histogram #3522
Showing proportions (instead of counts or density) of a bin in a histogram is a common exercise, but this is not documented. Most stackoverflow suggestions are either conflicting or outdated.
The solution below by @clauswilke in a now-locked thread seems like the best practice. Could this trick as well as the width stat be documented in the geom_histogram manual and the ggplot website?
library(ggplot2)
# instead of plotting the count statistic in geom_histogram, # plot the proportion of a bin, which is equivalent to the product of the # density and the width statistic
ggplot(diamonds, aes(x=carat, y= stat(density*width))) +
geom_histogram(binwidth=0.01)
Hmm... I'm wondering if using stat(width) is really a part of ggplot2's API; it's not a calculated variable of StatBin. I didn't notice stat() can access not only to official calculated variables, but also to other internal variables. For other example:
But, I don't check this in detail yet. Maybe I'm just confused with the case of width as an aesthetic, where we want to hide width from users to avoid confusion... (c.f. #3194)
Apologies for not following all the nuances, but does this imply y = stat(density*width) is a safe way to generate proportion histograms moving forward?
Activity
yutannihilation commentedon Sep 11, 2019
Hmm... I'm wondering if using
stat(width)is really a part of ggplot2's API; it's not a calculated variable ofStatBin. I didn't noticestat()can access not only to official calculated variables, but also to other internal variables. For other example:Created on 2019-09-11 by the reprex package (v0.3.0)
In my opinion,
widthis just an internal variable and should not be recommended to use because it can be renamed in future.@clauswilke
What do you think?
clauswilke commentedon Sep 11, 2019
I'm not sure I understand what "it's not a calculated variable of
StatBin" means.These are all the variables that
StatBincalculates:ggplot2/R/bin.R
Lines 168 to 175 in 7f317d4
I don't see how
widthis in any way different than, say,density.yutannihilation commentedon Sep 11, 2019
Yes,
widthis a calculated variable in that sense. The reason I thought it's not exposed officially wasit's not documented in the Computed Variables section, and
ggplot2/R/stat-bin.r
Lines 30 to 36 in 23e3241
it's not included in the data
layer_data()returns.Created on 2019-09-12 by the reprex package (v0.3.0)
But, I don't check this in detail yet. Maybe I'm just confused with the case of
widthas an aesthetic, where we want to hidewidthfrom users to avoid confusion... (c.f. #3194)clauswilke commentedon Sep 12, 2019
Weird, I have no idea why
widthdoesn't make it into the layer data.yutannihilation commentedon Sep 12, 2019
IIRC, this is somewhat related to the effort we are trying to treat
widthas a param.thomasp85 commentedon Oct 1, 2019
widthis not present because it gets dropped between the stat and geom AFAIK.kuriwaki commentedon Oct 28, 2019
Apologies for not following all the nuances, but does this imply
y = stat(density*width)is a safe way to generate proportion histograms moving forward?thomasp85 commentedon Jan 21, 2020
It should be safe, but I think it may make sense to provide it explicitly by
stat_bin()... @clauswilke do you have any input to thisclauswilke commentedon Jan 21, 2020
I think we should document
widthand also add a unit test that uses it to make sure it doesn't randomly disappear at some point in the future.yutannihilation commentedon Jan 21, 2020
Sorry, now I think I see the rule behind this a bit clearer now. Let me confirm.
widthand an aestheticwidthare two different things.widthis whatstat_bin()officially provideswidth(or any other variable) to an aestheticwidthof suchGeomasgeom_bar()(c.f. geom_bar()/geom_col() erroneously warn that they ignore width aesthetic #3142 (comment))Is my understanding correct?
clauswilke commentedon Jan 21, 2020
Yes, I think that's correct.
yutannihilation commentedon Jan 22, 2020
Thanks, then I agree with documenting
widthand adding tests.