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

Actual result of ..ndensity.. does not match the documentation #2324

timgoodman opened this Issue Nov 3, 2017 · 1 comment


None yet
2 participants

timgoodman commented Nov 3, 2017

In help(stat_bin), it defines ..ncount.. as "count, scaled to maximum of 1" and ..ndensity.. as "density, scaled to maximum of 1". From this I would expect that:

..ncount.. is ..count.. / max(..count..)

and that

..ndensity.. is ..density.. / max(..density..)

However, the latter is not the case. Instead, ..ndensity.. appears to be ..count.. / max(..density..)

If this is a bug, it looks like it could be fixed with a one line change to R/bin.R (which I'd be happy to submit as a pull request if you want). But if this is the intended behavior, perhaps the documentation could be made more clear.


standard_normal <- data.frame(X = rnorm(1E5, 0, 1))

# ..ndensity.. is ..count.. / max(..density..)
# which is not "density, scaled to a maximum of 1"
ggplot(standard_normal, aes(x = X)) +
  geom_freqpoly(aes(y = ..ndensity..), binwidth = 0.2, center = 0, size = 1.5, color = 'black') +
  geom_freqpoly(aes(y = ..count../max(..density..)), binwidth = 0.2, center = 0, size = 0, color = 'cyan')


This comment has been minimized.


timgoodman commented Nov 3, 2017

In particular, the changes to R/stat-bin.r in this commit included removing the line:

results$ndensity <- results$density / max(abs(results$density), na.rm = TRUE) 

while adding the line:

ndensity = count / max(abs(density)),

I assume the change from density to count in the numerator was unintentional. The new code was later moved from R/stat-bin.r to R/bin.r in this commit.

@hadley hadley closed this Nov 7, 2017

@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.