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

Fix issue where grouped boxplots and violin plots were variable width… #173

Merged
merged 3 commits into from
Jan 28, 2023

Conversation

crew102
Copy link
Contributor

@crew102 crew102 commented Jan 26, 2023

This PR addresses the issue mentioned in #169. Namely, we get varying widths for boxplots and violins b/c we were providing a data-based y aesthetic in the mapping for those (as opposed to what we're doing now, which is providing a constant for y). Incidentally, I previously thought it was necessary to provide y for both boxplots and violins, now I see it's just for violins.

Here's what it looks like with the current version of ggExtra and then with the version in this PR.

Current version

library(ggplot2)
library(ggExtra)

p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(am))) + geom_point()

ggMarginal(p, type = "boxplot", groupFill = TRUE)

# Not related to this PR: Looks like it's another bug related to rendering 
# ggMarinals. Have to call this or it all gets overwritten on the same "page" 
# on the device.
grid::grid.newpage()

ggMarginal(p, type = "violin", groupFill = TRUE)
#> Warning: `position_dodge()` requires non-overlapping x intervals
#> `position_dodge()` requires non-overlapping x intervals

Created on 2023-01-25 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.2 (2021-11-01)
#>  os       macOS Big Sur 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2023-01-25
#>  pandoc   2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.0)
#>  backports     1.4.1   2021-12-13 [1] CRAN (R 4.1.0)
#>  cli           3.6.0   2023-01-09 [1] CRAN (R 4.1.2)
#>  colorspace    2.0-3   2022-02-21 [1] CRAN (R 4.1.2)
#>  curl          4.3.2   2021-06-23 [1] CRAN (R 4.1.0)
#>  DBI           1.1.2   2021-12-20 [1] CRAN (R 4.1.0)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.0)
#>  dplyr         1.0.8   2022-02-08 [1] CRAN (R 4.1.2)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.15    2022-02-18 [1] CRAN (R 4.1.2)
#>  fansi         1.0.4   2023-01-22 [1] CRAN (R 4.1.2)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.1.2)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.1.0)
#>  generics      0.1.2   2022-01-31 [1] CRAN (R 4.1.2)
#>  ggExtra     * 0.10.0  2022-03-23 [1] CRAN (R 4.1.2)
#>  ggplot2     * 3.4.0   2022-11-04 [1] CRAN (R 4.1.2)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.1.2)
#>  gtable        0.3.1   2022-09-01 [1] CRAN (R 4.1.2)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.0)
#>  httpuv        1.6.5   2022-01-05 [1] CRAN (R 4.1.2)
#>  httr          1.4.2   2020-07-20 [1] CRAN (R 4.1.0)
#>  knitr         1.37    2021-12-16 [1] CRAN (R 4.1.0)
#>  labeling      0.4.2   2020-10-20 [1] CRAN (R 4.1.0)
#>  later         1.3.0   2021-08-18 [1] CRAN (R 4.1.0)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.1.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.1.2)
#>  mime          0.12    2021-09-28 [1] CRAN (R 4.1.0)
#>  miniUI        0.1.1.1 2018-05-18 [1] CRAN (R 4.1.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.1.0)
#>  pillar        1.8.1   2022-08-19 [1] CRAN (R 4.1.2)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
#>  promises      1.2.0.1 2021-02-11 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
#>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1   2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0  2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0  2021-09-26 [1] CRAN (R 4.1.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.0)
#>  Rcpp          1.0.8   2022-01-13 [1] CRAN (R 4.1.2)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang         1.0.6   2022-09-24 [1] CRAN (R 4.1.2)
#>  rmarkdown     2.12    2022-03-02 [1] CRAN (R 4.1.2)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
#>  scales        1.2.1   2022-08-20 [1] CRAN (R 4.1.2)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.0)
#>  shiny         1.7.1   2021-10-02 [1] CRAN (R 4.1.0)
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.1.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
#>  styler        1.6.2   2021-09-23 [1] CRAN (R 4.1.0)
#>  tibble        3.1.8   2022-07-22 [1] CRAN (R 4.1.2)
#>  tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.1.2)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.5.1   2022-11-16 [1] CRAN (R 4.1.2)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.1.2)
#>  xfun          0.29    2021-12-14 [1] CRAN (R 4.1.0)
#>  xml2          1.3.3   2021-11-30 [1] CRAN (R 4.1.0)
#>  xtable        1.8-4   2019-04-21 [1] CRAN (R 4.1.0)
#>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.1.2)
#> 
#>  [1] /Users/cbaker/Library/R/x86_64/4.1/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

This PR

library(ggplot2)
library(ggExtra)

p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(am))) + geom_point()

ggMarginal(p, type = "boxplot", groupFill = TRUE)

# Not related to this PR: Looks like it's another bug related to rendering 
# ggMarinals. Have to call this or it all gets overwritten on the same "page" 
# on the device.
grid::grid.newpage()

ggMarginal(p, type = "violin", groupFill = TRUE)

Created on 2023-01-25 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.2 (2021-11-01)
#>  os       macOS Big Sur 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2023-01-25
#>  pandoc   2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.0)
#>  backports     1.4.1   2021-12-13 [1] CRAN (R 4.1.0)
#>  cli           3.6.0   2023-01-09 [1] CRAN (R 4.1.2)
#>  colorspace    2.0-3   2022-02-21 [1] CRAN (R 4.1.2)
#>  curl          4.3.2   2021-06-23 [1] CRAN (R 4.1.0)
#>  DBI           1.1.2   2021-12-20 [1] CRAN (R 4.1.0)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.0)
#>  dplyr         1.0.8   2022-02-08 [1] CRAN (R 4.1.2)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.15    2022-02-18 [1] CRAN (R 4.1.2)
#>  fansi         1.0.4   2023-01-22 [1] CRAN (R 4.1.2)
#>  farver        2.1.1   2022-07-06 [1] CRAN (R 4.1.2)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.1.0)
#>  generics      0.1.2   2022-01-31 [1] CRAN (R 4.1.2)
#>  ggExtra     * 0.10.0  2023-01-26 [1] local
#>  ggplot2     * 3.4.0   2022-11-04 [1] CRAN (R 4.1.2)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.1.2)
#>  gtable        0.3.1   2022-09-01 [1] CRAN (R 4.1.2)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.0)
#>  httpuv        1.6.5   2022-01-05 [1] CRAN (R 4.1.2)
#>  httr          1.4.2   2020-07-20 [1] CRAN (R 4.1.0)
#>  knitr         1.37    2021-12-16 [1] CRAN (R 4.1.0)
#>  labeling      0.4.2   2020-10-20 [1] CRAN (R 4.1.0)
#>  later         1.3.0   2021-08-18 [1] CRAN (R 4.1.0)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.1.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.1.2)
#>  mime          0.12    2021-09-28 [1] CRAN (R 4.1.0)
#>  miniUI        0.1.1.1 2018-05-18 [1] CRAN (R 4.1.0)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.1.0)
#>  pillar        1.8.1   2022-08-19 [1] CRAN (R 4.1.2)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
#>  promises      1.2.0.1 2021-02-11 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
#>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1   2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0  2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0  2021-09-26 [1] CRAN (R 4.1.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.0)
#>  Rcpp          1.0.8   2022-01-13 [1] CRAN (R 4.1.2)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang         1.0.6   2022-09-24 [1] CRAN (R 4.1.2)
#>  rmarkdown     2.12    2022-03-02 [1] CRAN (R 4.1.2)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
#>  scales        1.2.1   2022-08-20 [1] CRAN (R 4.1.2)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.0)
#>  shiny         1.7.1   2021-10-02 [1] CRAN (R 4.1.0)
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.1.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
#>  styler        1.6.2   2021-09-23 [1] CRAN (R 4.1.0)
#>  tibble        3.1.8   2022-07-22 [1] CRAN (R 4.1.2)
#>  tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.1.2)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.5.1   2022-11-16 [1] CRAN (R 4.1.2)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.1.2)
#>  xfun          0.29    2021-12-14 [1] CRAN (R 4.1.0)
#>  xml2          1.3.3   2021-11-30 [1] CRAN (R 4.1.0)
#>  xtable        1.8-4   2019-04-21 [1] CRAN (R 4.1.0)
#>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.1.2)
#> 
#>  [1] /Users/cbaker/Library/R/x86_64/4.1/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Not a great example I know; It's kinda hard to tell with the boxplots what the difference is, and with the violins, it almost looks like the issue is still persisting. I believe the violins are still correct, though, as, as the widths of a given violin will be a function of the underlying distribution.

@crew102
Copy link
Contributor Author

crew102 commented Jan 26, 2023

Shoulda just used the example in the issue. Here's what it looks like with the fix:

Rplot

@daattali
Copy link
Owner

With the violins, I can see they don't overlap, which is perhaps an indication that each one gets its own "bounding box".

I wasn't able to reproduce the issue you mention that requires grid.newpage() , do you literally see two different plots laid on top of each other?

@daattali
Copy link
Owner

daattali commented Jan 27, 2023

Can you bump the version to .9000 and add a NEWS entry?

@crew102
Copy link
Contributor Author

crew102 commented Jan 28, 2023

Sure I'll add those items. And yeah, when you run the code through reprex you may get something that looks like this:

library(ggplot2)
library(ggExtra)

p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(am))) + geom_point()

ggMarginal(p, type = "boxplot", groupFill = TRUE)

ggMarginal(p, type = "violin", groupFill = TRUE)

Created on 2023-01-27 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.2 (2021-11-01)
#>  os       macOS Big Sur 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2023-01-27
#>  pandoc   2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version   date (UTC) lib source
#>  assertthat    0.2.1     2019-03-21 [1] CRAN (R 4.1.0)
#>  backports     1.4.1     2021-12-13 [1] CRAN (R 4.1.0)
#>  cli           3.6.0     2023-01-09 [1] CRAN (R 4.1.2)
#>  colorspace    2.0-3     2022-02-21 [1] CRAN (R 4.1.2)
#>  curl          4.3.2     2021-06-23 [1] CRAN (R 4.1.0)
#>  DBI           1.1.2     2021-12-20 [1] CRAN (R 4.1.0)
#>  digest        0.6.29    2021-12-01 [1] CRAN (R 4.1.0)
#>  dplyr         1.0.8     2022-02-08 [1] CRAN (R 4.1.2)
#>  ellipsis      0.3.2     2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.15      2022-02-18 [1] CRAN (R 4.1.2)
#>  fansi         1.0.4     2023-01-22 [1] CRAN (R 4.1.2)
#>  farver        2.1.1     2022-07-06 [1] CRAN (R 4.1.2)
#>  fastmap       1.1.0     2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.2     2021-12-08 [1] CRAN (R 4.1.0)
#>  generics      0.1.2     2022-01-31 [1] CRAN (R 4.1.2)
#>  ggExtra     * 0.10.9000 2023-01-28 [1] local
#>  ggplot2     * 3.4.0     2022-11-04 [1] CRAN (R 4.1.2)
#>  glue          1.6.2     2022-02-24 [1] CRAN (R 4.1.2)
#>  gtable        0.3.1     2022-09-01 [1] CRAN (R 4.1.2)
#>  highr         0.9       2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2     2021-08-25 [1] CRAN (R 4.1.0)
#>  httpuv        1.6.5     2022-01-05 [1] CRAN (R 4.1.2)
#>  httr          1.4.2     2020-07-20 [1] CRAN (R 4.1.0)
#>  knitr         1.37      2021-12-16 [1] CRAN (R 4.1.0)
#>  labeling      0.4.2     2020-10-20 [1] CRAN (R 4.1.0)
#>  later         1.3.0     2021-08-18 [1] CRAN (R 4.1.0)
#>  lifecycle     1.0.3     2022-10-07 [1] CRAN (R 4.1.2)
#>  magrittr      2.0.3     2022-03-30 [1] CRAN (R 4.1.2)
#>  mime          0.12      2021-09-28 [1] CRAN (R 4.1.0)
#>  miniUI        0.1.1.1   2018-05-18 [1] CRAN (R 4.1.0)
#>  munsell       0.5.0     2018-06-12 [1] CRAN (R 4.1.0)
#>  pillar        1.8.1     2022-08-19 [1] CRAN (R 4.1.2)
#>  pkgconfig     2.0.3     2019-09-22 [1] CRAN (R 4.1.0)
#>  promises      1.2.0.1   2021-02-11 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4     2020-04-17 [1] CRAN (R 4.1.0)
#>  R.cache       0.15.0    2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1     2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0    2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0    2021-09-26 [1] CRAN (R 4.1.0)
#>  R6            2.5.1     2021-08-19 [1] CRAN (R 4.1.0)
#>  Rcpp          1.0.8     2022-01-13 [1] CRAN (R 4.1.2)
#>  reprex        2.0.1     2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang         1.0.6     2022-09-24 [1] CRAN (R 4.1.2)
#>  rmarkdown     2.12      2022-03-02 [1] CRAN (R 4.1.2)
#>  rstudioapi    0.13      2020-11-12 [1] CRAN (R 4.1.0)
#>  scales        1.2.1     2022-08-20 [1] CRAN (R 4.1.2)
#>  sessioninfo   1.2.2     2021-12-06 [1] CRAN (R 4.1.0)
#>  shiny         1.7.1     2021-10-02 [1] CRAN (R 4.1.0)
#>  stringi       1.7.6     2021-11-29 [1] CRAN (R 4.1.0)
#>  stringr       1.4.0     2019-02-10 [1] CRAN (R 4.1.0)
#>  styler        1.6.2     2021-09-23 [1] CRAN (R 4.1.0)
#>  tibble        3.1.8     2022-07-22 [1] CRAN (R 4.1.2)
#>  tidyselect    1.1.2     2022-02-21 [1] CRAN (R 4.1.2)
#>  utf8          1.2.2     2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.5.1     2022-11-16 [1] CRAN (R 4.1.2)
#>  withr         2.5.0     2022-03-03 [1] CRAN (R 4.1.2)
#>  xfun          0.29      2021-12-14 [1] CRAN (R 4.1.0)
#>  xml2          1.3.3     2021-11-30 [1] CRAN (R 4.1.0)
#>  xtable        1.8-4     2019-04-21 [1] CRAN (R 4.1.0)
#>  yaml          2.3.5     2022-02-21 [1] CRAN (R 4.1.2)
#> 
#>  [1] /Users/cbaker/Library/R/x86_64/4.1/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Probably b/c reprex sends code off to Rmarkdown for execution, which we've had issues with in the past (e.g., #147, #133, #89).

@daattali
Copy link
Owner

Ah, yes, in a reprex (or rmd) I see what you mean. It is indeed a known issue described in the README!

@daattali daattali merged commit 2fc29c1 into daattali:master Jan 28, 2023
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

Successfully merging this pull request may close these issues.

2 participants