Skip to content

Using weights in geom_density() or geom_violin() give strange warning. #5374

Closed

Description

@ralmond

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?

Created on 2023-07-29 with reprex v2.0.2

This may be harmless, but it is confusing.

Activity

  1. teunbrand commented on Aug 1, 2023

    @teunbrand
    Collaborator

    I think you've stumbled on several issues here:

    • 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'

    Created on 2023-08-01 with reprex v2.0.2

  2. added
    bug
    on Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buglayers 📈

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions