Skip to content

Silence new density() warning #5281

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

Merged
merged 3 commits into from
Apr 24, 2023
Merged

Conversation

teunbrand
Copy link
Collaborator

This PR aims to prevent a newly introduced warning.

From the R4.3.0 NEWS bullets:

density(x, weights = *) now warns if automatic bandwidth selection happens without using weights; new optional warnWbw may suppress the warning. Prompted by Christoph Dalitz' PR#18490 and its discussants.

This affects ggplot2 when it uses weights:

library(ggplot2)

cutoff <- min(mtcars$mpg)

p <- ggplot(mtcars, aes(mpg, weight = cyl)) +
  stat_density()
invisible(ggplotGrob(p))
#> Warning in density.default(x, weights = w, bw = bw, adjust = adjust, kernel =
#> kernel, : Selecting bandwidth *not* using 'weights'

Created on 2023-04-22 with reprex v2.0.2

Because of backwards compatibility with older R versions, we cannot use the warnWbw argument. This PR prevents this warning by precomputing the bw argument of density() and noting in the docs that weights are ignored for bandwidth calculation (as they always have been, it is just stated now).

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit 4ef0284 into tidyverse:main Apr 24, 2023
@teunbrand teunbrand deleted the R43_KDE_warning branch April 24, 2023 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants