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
geom_smooth() warns that weight aesthetic dropped during statistical transformation #5053
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?
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'
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
@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.
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?
@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.
@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
Activity
MattCowgill commentedon Nov 17, 2022
To add to the above, the
weightaesthetic does not actually seem to be dropped, despite what the warning says. The results differ ifgeom_smooth()is called without the weight aesthetic, as below:Created on 2022-11-17 with reprex v2.0.2
stamats commentedon Nov 19, 2022
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
clauswilke commentedon Nov 19, 2022
@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 likestat_smooth()should listweightas an aesthetic that can be dropped. (Note: If I read the code correctly,weightgets dropped andweightsgets added. This may have been a typo whenstat_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.stamats commentedon Nov 19, 2022
Dear Claus,
many thanks for the hint!
I added dropped_aes to the respective code of qqplotr and the warnings disappeared.
Best Matthias
MattCowgill commentedon Nov 20, 2022
Thank you @clauswilke!
Re:
geom_smooth(), the warning tells us thatweightis dropped, but you can see from my example above (the chart) that it is not dropped - thegeom_smooth()layer with theweightaesthetic is different from the layer without. So unless I am mistaken, it seems like this warning is misleading, in thatweightis not actually dropped, and unnecessary, in thatweightshould not be dropped in this case. Have I misunderstood?clauswilke commentedon Nov 20, 2022
@MattCowgill I think you're misunderstanding the word "dropped" here. It means that there is a
weightdata 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.MattCowgill commentedon Nov 20, 2022
Thanks for the clarification @clauswilke, apologies for misunderstanding.
I continue to not understand why a warning is being issued in this instance.
clauswilke commentedon Nov 21, 2022
@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
MattCowgill commentedon Nov 21, 2022
OK, thank you @clauswilke, sorry for not understanding earlier.
Fix tidyverse#5053
Drop `weight` aesthetic quietly in `stat_smooth()` (#5071)
MattCowgill commentedon Feb 20, 2023
Great to see this addressed in 3.4.1! https://github.com/tidyverse/ggplot2/releases/tag/v3.4.1
teunbrand commentedon Feb 20, 2023