Skip to content
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

Warning message with stat_function() when using purrr::map2 #5498

Closed
yuw444 opened this issue Oct 26, 2023 · 4 comments
Closed

Warning message with stat_function() when using purrr::map2 #5498

yuw444 opened this issue Oct 26, 2023 · 4 comments

Comments

@yuw444
Copy link

yuw444 commented Oct 26, 2023

I found a problem with stat_function(), it produces some warning messages although the result is as expected.

Here is the code to reproduce the bug:

library(ggplot2)
library(purrr)
data(mtcars)
temp <- function(y) {
    function(x) y / x
}

fun_list <- map(1:3 * 10^4, ~ temp(.x))

p <- ggplot(
    mtcars,
    aes(disp, hp, fill = wt)
) +
    geom_point(shape = 21, size = 3, aes(fill= factor(vs))) +
    purrr::map2(
        fun_list,
        c("4", "6", "8"),
        ~ stat_function(
            fun = .x,
            aes(color = .y)
        )
    )

p

Below is the warning message

Warning messages:
1: The following aesthetics were dropped during statistical transformation: fill
ℹ 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? 
2: The following aesthetics were dropped during statistical transformation: fill
ℹ 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? 
3: The following aesthetics were dropped during statistical transformation: fill
ℹ 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?
@teunbrand
Copy link
Collaborator

More minimal reprex to reproduce warning:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.1

ggplot(mtcars, aes(disp, hp, fill = wt)) +
  geom_point() +
  stat_function(fun = ~ .x)
#> Warning: The following aesthetics were dropped during statistical transformation: fill
#> ℹ 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-10-26 with reprex v2.0.2

In any case, I think the warnings are appropriate, as you've specified fill =wt at the top-level ggplot() call, which will result in non-constant columns within a group that the stat applies to.

@clauswilke
Copy link
Member

I tend to agree that the warning is appropriate even if it is a bit confusing here. The code is asking stat_function() to do something that it cannot do (vary the fill color along the data points), and so this triggers a warning. It's difficult to write these warnings in such a way that they correctly describe the true underlying issue in all cases.

@teunbrand
Copy link
Collaborator

Yeah agreed. Because it is working as intended, I'll close this as 'not a bug'.

@teunbrand teunbrand closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2023
@yuw444
Copy link
Author

yuw444 commented Oct 27, 2023

Thanks for the insight. Seems like I should put fill=wt inside of geom_point().

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

No branches or pull requests

3 participants