Skip to content

geom_bin_2d: warning message when using weight without group #5216

Closed

Description

@szimmer

I found a problem with geom_bin_2d

I expected to be able to use a weight variable without a group variable but get a warning when I do this. The weight does seem to be used appropriately as the result is different when not using the weight.

Here is the code to reproduce the bug:

library(survey)
#> Loading required package: grid
#> Loading required package: Matrix
#> Loading required package: survival
#> 
#> Attaching package: 'survey'
#> The following object is masked from 'package:graphics':
#> 
#>     dotchart
library(ggplot2)

data(api)

ggplot(aes(x=api99, y=api00, weight=pw), data=apiclus2) +
  geom_bin_2d() 
#> 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?

ggplot(aes(x=api99, y=api00), data=apiclus2) +
  geom_bin_2d() 

Created on 2023-03-05 with reprex v2.0.2

Standard output and standard error
-- nothing to show --
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 (2022-10-31 ucrt)
#>  os       Windows 10 x64 (build 22621)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/New_York
#>  date     2023-03-05
#>  pandoc   2.19.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.2.1)
#>  cli           3.4.1   2022-09-23 [1] CRAN (R 4.2.2)
#>  colorspace    2.0-3   2022-02-21 [1] CRAN (R 4.2.1)
#>  curl          4.3.2   2021-06-23 [1] CRAN (R 4.2.1)
#>  DBI           1.1.3   2022-06-18 [1] CRAN (R 4.2.1)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.2.1)
#>  dplyr         1.0.10  2022-09-01 [1] CRAN (R 4.2.1)
#>  evaluate      0.16    2022-08-09 [1] CRAN (R 4.2.1)
#>  fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.1)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.2.1)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.1)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.1)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.1)
#>  ggplot2     * 3.4.0   2022-11-04 [1] CRAN (R 4.2.2)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.1)
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.2.1)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.2.1)
#>  htmltools     0.5.3   2022-07-18 [1] CRAN (R 4.2.1)
#>  httr          1.4.4   2022-08-17 [1] CRAN (R 4.2.1)
#>  knitr         1.40    2022-08-24 [1] CRAN (R 4.2.1)
#>  labeling      0.4.2   2020-10-20 [1] CRAN (R 4.2.0)
#>  lattice       0.20-45 2021-09-22 [2] CRAN (R 4.2.2)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.1)
#>  Matrix      * 1.5-1   2022-09-13 [1] CRAN (R 4.2.1)
#>  mime          0.12    2021-09-28 [1] CRAN (R 4.2.0)
#>  mitools       2.4     2019-04-26 [1] CRAN (R 4.2.1)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.2.1)
#>  pillar        1.8.1   2022-08-19 [1] CRAN (R 4.2.1)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.1)
#>  purrr         0.3.5   2022-10-06 [1] CRAN (R 4.2.1)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.2.1)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.0  2022-06-28 [1] CRAN (R 4.2.1)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.1)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.1)
#>  rlang         1.0.6   2022-09-24 [1] CRAN (R 4.2.1)
#>  rmarkdown     2.16    2022-08-24 [1] CRAN (R 4.2.1)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.2.1)
#>  scales        1.2.0   2022-04-13 [1] CRAN (R 4.2.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.1)
#>  stringi       1.7.8   2022-07-11 [1] CRAN (R 4.2.1)
#>  stringr       1.4.1   2022-08-20 [1] CRAN (R 4.2.1)
#>  styler        1.7.0   2022-03-13 [1] CRAN (R 4.2.1)
#>  survey      * 4.1-1   2021-07-19 [1] CRAN (R 4.2.1)
#>  survival    * 3.4-0   2022-08-09 [2] CRAN (R 4.2.2)
#>  tibble        3.1.8   2022-07-22 [1] CRAN (R 4.2.1)
#>  tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.2.1)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.1)
#>  vctrs         0.5.1   2022-11-16 [1] CRAN (R 4.2.2)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.1)
#>  xfun          0.32    2022-08-10 [1] CRAN (R 4.2.1)
#>  xml2          1.3.3   2021-11-30 [1] CRAN (R 4.2.1)
#>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.2.1)
#> 
#>  [1] C:/Users/steph/AppData/Local/R/win-library/4.2
#>  [2] C:/Program Files/R/R-4.2.2/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Activity

  1. teunbrand commented on Mar 5, 2023

    @teunbrand
    Collaborator

    Thanks for the report! Is this conceptually similar to #5053, where also an inappropriate warning was emitted?

  2. szimmer commented on Mar 5, 2023

    @szimmer
    Author

    Yes, I believe it is the same. Sorry I didn't see that when searching for issues before writing this one.

  3. teunbrand commented on Mar 6, 2023

    @teunbrand
  4. teunbrand commented on Mar 6, 2023

    @teunbrand
    Collaborator

    Reviewing when this occurs, I found that many more stats mention dropping weights. I think the weights are being dropped by design, so we don't need these warnings.

    library(ggplot2)
    
    ggplot(data.frame(x = 1:2)) +
      aes(x, y = x, weight = x) +
      geom_bin_2d()
    #> 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?

    ggplot(data.frame(x = c("A", "A", "B", "B"), y = 1:4)) +
      aes(x, y, weight = seq_along(y)) +
      geom_boxplot()
    #> 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?

    ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
      geom_contour(aes(weight = seq_along(density)))
    #> 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?

    ggplot(data.frame(x = 1:2)) +
      aes(x, y = x, weight = x) +
      geom_hex()
    #> 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?

    ggplot(data.frame(x = 1:2)) +
      aes(x, x, weight = x) +
      stat_quantile()
    #> Smoothing formula not specified. Using: 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?

    Created on 2023-03-06 with reprex v2.0.2

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions