Skip to content

geom_smooth() warns that weight aesthetic dropped during statistical transformation #5053

Closed

Description

@MattCowgill

When using the weight aesthetic with geom_smooth(), I now get the following warning that did not occur before version 3.4.0:

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?

Here is a reprex:

library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(size = cyl)) +
  geom_smooth(aes(weight = cyl),
              method = "loess")

I am unclear about why this warning arises. I believe it is related to #4917.

Activity

  1. MattCowgill commented on Nov 17, 2022

    @MattCowgill
    ContributorAuthor

    To add to the above, the weight aesthetic does not actually seem to be dropped, despite what the warning says. The results differ if geom_smooth() is called without the weight aesthetic, as below:

    library(ggplot2)
    
    ggplot(mtcars, aes(wt, mpg)) +
      geom_point(aes(size = cyl)) +
      geom_smooth(aes(weight = cyl),
                  method = "loess",
                  colour = "blue",
                  se = FALSE) +
      geom_smooth(method = "loess",
                  colour = "red",
                  se = FALSE)
    #> `geom_smooth()` using formula = 'y ~ x'
    #> 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?
    #> `geom_smooth()` using formula = 'y ~ x'

    Created on 2022-11-17 with reprex v2.0.2

  2. stamats commented on Nov 19, 2022

    @stamats

    Maybe the following is related.

    I use R 4.2.2 Patched (2022-11-12 r83339), ggplot2 version 3.4.0 (2022-11-04) and qqplotr 0.0.5 (2021-04-23).

    Running example from qqplotr:
    smp <- data.frame(norm = rnorm(100))
    gg <- ggplot(data = smp, mapping = aes(sample = norm)) +
    stat_qq_band() +
    stat_qq_line() +
    stat_qq_point() +
    labs(x = "Theoretical Quantiles", y = "Sample Quantiles")
    gg

    I now get two warnings:

    1: The following aesthetics were dropped during statistical transformation: sample
    information_source This can happen when ggplot fails to infer the correct grouping structure in the data.
    information_source Did you forget to specify a group aesthetic or to convert a numerical variable into a
    factor?
    2: The following aesthetics were dropped during statistical transformation: sample
    information_source This can happen when ggplot fails to infer the correct grouping structure in the data.
    information_source Did you forget to specify a group aesthetic or to convert a numerical variable into a
    factor?

    Which are from stat_qq_band and stat_qq_line. That is

    ggplot(data = smp, mapping = aes(sample = norm)) + stat_qq_band()

    and

    ggplot(data = smp, mapping = aes(sample = norm)) + stat_qq_line()

    give a warning, but not

    ggplot(data = smp, mapping = aes(sample = norm)) + stat_qq_point()

    I have to admit that I'm not sure whether it is an issue with ggplot2 or qqplotr.

    Best wishes
    Matthias

  3. clauswilke commented on Nov 19, 2022

    @clauswilke
    Member

    @MattCowgill @stamats Since ggplot2 3.4.0, stats need to declare any aesthetics that will get dropped during the statistical transformation. We added the relevant declarations to the stats that are part of the ggplot2 package, but may have overlooked some. For the problem with geom_smooth(), it looks like stat_smooth() should list weight as an aesthetic that can be dropped. (Note: If I read the code correctly,weight gets dropped and weights gets added. This may have been a typo when stat_smooth() was written.)

    For the stat_qq_* examples, whether this is a ggplot2 issue or a qqplotr issue depends on where these stats are defined. If they are defined in qqplotr then qqplotr would have to make sure they declare dropped aesthetics.

  4. stamats commented on Nov 19, 2022

    @stamats

    Dear Claus,
    many thanks for the hint!
    I added dropped_aes to the respective code of qqplotr and the warnings disappeared.
    Best Matthias

  5. MattCowgill commented on Nov 20, 2022

    @MattCowgill
    ContributorAuthor

    Thank you @clauswilke!

    Re: geom_smooth(), the warning tells us that weight is dropped, but you can see from my example above (the chart) that it is not dropped - the geom_smooth() layer with the weight aesthetic is different from the layer without. So unless I am mistaken, it seems like this warning is misleading, in that weight is not actually dropped, and unnecessary, in that weight should not be dropped in this case. Have I misunderstood?

  6. clauswilke commented on Nov 20, 2022

    @clauswilke
    Member

    @MattCowgill I think you're misunderstanding the word "dropped" here. It means that there is a weight data column before the stat does its job but it's no longer there after. In that sense it's been dropped. Whether in the process of dropping it has been used or not is a separate question. For stats that intentionally drop columns, they generally make use of them in the process.

  7. MattCowgill commented on Nov 20, 2022

    @MattCowgill
    ContributorAuthor

    Thanks for the clarification @clauswilke, apologies for misunderstanding.

    I continue to not understand why a warning is being issued in this instance.

  8. clauswilke commented on Nov 21, 2022

    @clauswilke
    Member

    @MattCowgill Well, there shouldn't be a warning if the drop is intentional. The case you identified for geom_smooth() needs to be fixed. That's why I didn't close this issue.

    The warning is meant for cases such as these: #3250

  9. added
    bug
    on Nov 21, 2022
  10. MattCowgill commented on Nov 21, 2022

    @MattCowgill
    ContributorAuthor

    OK, thank you @clauswilke, sorry for not understanding earlier.

  11. added a commit that references this issue on Nov 30, 2022
    9c94550
  12. added a commit that references this issue on Dec 3, 2022
    a3e79d7
  13. MattCowgill commented on Feb 20, 2023

    @MattCowgill
    ContributorAuthor
  14. teunbrand commented on Feb 20, 2023

    @teunbrand
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions