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
Using weights in geom_density() or geom_violin() give strange warning. #5374
I found a problem with geom_density() and geom_violin() when the weight aesthetic is used.
I expected ggplot to use the weights as an argument to density, which appears to happen; however, I get a warning message about dropping weight.
Warning message:
The following aesthetics were dropped during statistical transformation: weight
ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?
I'm not sure if this is harmless or not, as the density plots for the different species are different, which would not happen with unweighted data.
Here is the code to reproduce the bug:
library(tidyverse)
train<- sample(1:150, 75)
z<-MASS::lda(Species~Sepal.Length+Sepal.Width,data=iris,prior=c(1,1,1)/3,subset=train)
iris.pred<- cbind(iris[-train,],predict(z,iris[-train,])$posterior) %>%
pivot_longer(cols=all_of(c("setosa","versicolor","virginica")),
names_to="est_species",values_to="prob")
ggplot(iris.pred,aes(Petal.Width,weights=prob)) + geom_density() + facet_grid(rows=vars(est_species))
#> Warning in density.default(x, weights = w, bw = bw, adjust = adjust, kernel =#> kernel, : Selecting bandwidth *not* using 'weights'#> Warning: The following aesthetics were dropped during statistical transformation:#> weights#> ℹ This can happen when ggplot fails to infer the correct grouping structure in#> the data.#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical#> variable into a factor?
(Two repetitions of the same error messages deleted.)
ggplot(iris.pred,aes(x=est_species,y=Petal.Width,weight=prob)) + geom_violin()
#> Warning: The following aesthetics were dropped during statistical transformation: weight#> ℹ This can happen when ggplot fails to infer the correct grouping structure in#> the data.#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical#> variable into a factor?
weights is not a valid aesthetic. weight (without the -s), is a valid aesthetic.
There is partial matching of data$w to data$weights, which shouldn't happen.
Here is a more minimal reprex (the partial match warning turned on)
library(ggplot2)
options(warnPartialMatchDollar=TRUE)
p<- ggplot(iris, aes(Species, Sepal.Width))
p+ geom_violin(aes(weights=Sepal.Length))
#> Warning in geom_violin(aes(weights = Sepal.Length)): Ignoring unknown#> aesthetics: weights#> Warning in data$w: partial match of 'w' to 'weights'#> Warning in data$w: partial match of 'w' to 'weights'#> Warning in data$w: partial match of 'w' to 'weights'#> Warning: The following aesthetics were dropped during statistical transformation:#> weights#> ℹ This can happen when ggplot fails to infer the correct grouping structure in#> the data.#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical#> variable into a factor?
As a demonstration, you also get the dropped aesthetic warning if you provide a nonsense asethetic.
p+ geom_violin(aes(foobar=Sepal.Length))
#> Warning in geom_violin(aes(foobar = Sepal.Length)): Ignoring unknown#> aesthetics: foobar#> Warning: The following aesthetics were dropped during statistical transformation: foobar#> ℹ This can happen when ggplot fails to infer the correct grouping structure in#> the data.#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical#> variable into a factor?
Note that while weight is a valid aesthetic, the partial matching remains an issue.
p+ geom_violin(aes(weight=Sepal.Length))
#> Warning in data$w: partial match of 'w' to 'weight'#> Warning in data$w: partial match of 'w' to 'weight'#> Warning in data$w: partial match of 'w' to 'weight'
Activity
teunbrand commentedon Aug 1, 2023
I think you've stumbled on several issues here:
weightsis not a valid aesthetic.weight(without the -s), is a valid aesthetic.data$wtodata$weights, which shouldn't happen.Here is a more minimal reprex (the partial match warning turned on)
As a demonstration, you also get the dropped aesthetic warning if you provide a nonsense asethetic.
Note that while
weightis a valid aesthetic, the partial matching remains an issue.Created on 2023-08-01 with reprex v2.0.2