Skip to content
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

stat_density() ignores weight #2902

Closed
eliocamp opened this issue Sep 19, 2018 · 4 comments · Fixed by #3409
Closed

stat_density() ignores weight #2902

eliocamp opened this issue Sep 19, 2018 · 4 comments · Fixed by #3409
Labels
bug layers 📈 tidy-dev-day 🤓

Comments

@eliocamp
Copy link
Contributor

@eliocamp eliocamp commented Sep 19, 2018

While both geom_density() and stat_density() compute weighted densities correctly, the later results in an spurious warning (the first of the bunch).

library(ggplot2)

df <- data.frame(x = runif(100))

ggplot(df, aes(x)) +
   stat_density(aes(weight = abs(x - 0.5))) +
   geom_density(aes(weight = abs(x - 0.5)), color = "red")
#> Warning: Ignoring unknown aesthetics: weight
#> Warning in density.default(x, weights = w, bw = bw, adjust = adjust, kernel
#> = kernel, : sum(weights) != 1 -- will not get true density

#> Warning in density.default(x, weights = w, bw = bw, adjust = adjust, kernel
#> = kernel, : sum(weights) != 1 -- will not get true density

Created on 2018-09-19 by the reprex package (v0.2.0).

(I'm sorry if the multiple similar issues are not cool; you can tell that I'm working with weighted densities in my work and finding these small issues 😆 )

@clauswilke
Copy link
Member

@clauswilke clauswilke commented Sep 19, 2018

It's better to file separate issues for separate items, thanks!

@paleolimbot
Copy link
Member

@paleolimbot paleolimbot commented May 27, 2019

It looks like GeomDensity sets a default aesthetic for weight, which silences the warning (even though weight isn't in the data that gets passed to GeomDensity.

ggplot2/R/geom-density.r

Lines 75 to 80 in 1f6f0cb

GeomDensity <- ggproto("GeomDensity", GeomArea,
default_aes = defaults(
aes(fill = NA, weight = 1, colour = "black", alpha = NA),
GeomArea$default_aes
)
)

I very briefly tried adding weight = 1 to the default aesthetics of StatDensity, which does silence the warning (and doesn't cause any tests to fail).

ggplot2/R/stat-density.r

Lines 65 to 67 in 1f6f0cb

StatDensity <- ggproto("StatDensity", Stat,
required_aes = "x",
default_aes = aes(y = stat(density), fill = NA),

@paleolimbot paleolimbot added bug layers 📈 labels May 27, 2019
@hadley
Copy link
Member

@hadley hadley commented Jun 18, 2019

Two problems:

  • We need to do whatever is necessary to tell stat_density() that weight is a valid aesthetic.

  • In compute_density(), I think we should always do w <- w / sum(w) to silence the warning from density().

@hadley hadley added the tidy-dev-day 🤓 label Jun 18, 2019
annennenne added a commit to annennenne/ggplot2 that referenced this issue Jul 8, 2019
@lock
Copy link

@lock lock bot commented Jan 4, 2020

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/

@lock lock bot locked and limited conversation to collaborators Jan 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug layers 📈 tidy-dev-day 🤓
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants