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

stat_density2d doesn't work nicely with geom_polygon #3846

Closed
JoFAM opened this issue Mar 1, 2020 · 6 comments · Fixed by sthagen/tidyverse-ggplot2#10
Closed

stat_density2d doesn't work nicely with geom_polygon #3846

JoFAM opened this issue Mar 1, 2020 · 6 comments · Fixed by sthagen/tidyverse-ggplot2#10

Comments

@JoFAM
Copy link

JoFAM commented Mar 1, 2020

As per the title. If I use stat_density2d(aes(fill = stat(level)), geom = "polygon") the outer regions are completely warbled.

data(airquality)
airQ <- filter_all(airquality, ~!is.na(.))
ggplot(airQ, aes(x = Solar.R, y = Temp)) +
  geom_point() +
  stat_density2d(aes(fill = stat(level)),
                 geom = "polygon")

gives
Wrongplot

@thomasp85
Copy link
Member

thomasp85 commented Mar 1, 2020

The coming release will make you happy 🙂

@JoFAM
Copy link
Author

JoFAM commented Mar 1, 2020

@thomasp85 I hoped to be able to use this for the practical exercises tomorrow, but that's pushing it a bit probably? 😃

Definitely looking forward to it.

@thomasp85
Copy link
Member

thomasp85 commented Mar 1, 2020

You can install the v3.3.0-rc branch and run with it

@clauswilke
Copy link
Member

clauswilke commented Mar 1, 2020

Looking into this now, I'm not sure this is fixed yet. We can draw filled contours from a raster grid of data, but we can't run the density estimation and then draw the filled contour, as far as I can tell.

library(tidyverse)

data(airquality)

airQ <- filter_all(airquality, ~!is.na(.))

ggplot(airQ, aes(x = Solar.R, y = Temp)) +
  stat_density_2d(aes(fill = after_stat(level)), geom = "polygon")

Created on 2020-03-01 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.0 (2019-04-26)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2020-03-01                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                            
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                    
#>  backports     1.1.5      2019-10-02 [1] CRAN (R 3.6.0)                    
#>  broom         0.5.2      2019-04-07 [1] CRAN (R 3.6.0)                    
#>  callr         3.4.2      2020-02-12 [1] CRAN (R 3.6.0)                    
#>  cellranger    1.1.0      2016-07-27 [1] CRAN (R 3.6.0)                    
#>  cli           2.0.1      2020-01-08 [1] CRAN (R 3.6.0)                    
#>  colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.6.0)                    
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                    
#>  curl          4.3        2019-12-02 [1] CRAN (R 3.6.0)                    
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.6.0)                    
#>  devtools      2.2.1      2019-09-24 [1] CRAN (R 3.6.0)                    
#>  digest        0.6.25     2020-02-23 [1] CRAN (R 3.6.0)                    
#>  dplyr       * 0.8.3      2019-07-04 [1] CRAN (R 3.6.0)                    
#>  ellipsis      0.3.0      2019-09-20 [1] CRAN (R 3.6.0)                    
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)                    
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.0)                    
#>  farver        2.0.3      2020-01-16 [1] CRAN (R 3.6.0)                    
#>  forcats     * 0.4.0      2019-02-17 [1] CRAN (R 3.6.0)                    
#>  fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)                    
#>  generics      0.0.2      2018-11-29 [1] CRAN (R 3.6.0)                    
#>  ggplot2     * 3.3.0.9000 2020-02-25 [1] Github (tidyverse/ggplot2@53edc51)
#>  glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)                    
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 3.6.0)                    
#>  haven         2.1.1      2019-07-04 [1] CRAN (R 3.6.0)                    
#>  highr         0.8        2019-03-20 [1] CRAN (R 3.6.0)                    
#>  hms           0.5.3      2020-01-08 [1] CRAN (R 3.6.0)                    
#>  htmltools     0.3.6      2017-04-28 [1] CRAN (R 3.6.0)                    
#>  httr          1.4.1      2019-08-05 [1] CRAN (R 3.6.0)                    
#>  isoband       0.2.0.9000 2019-06-15 [1] local                             
#>  jsonlite      1.6        2018-12-07 [1] CRAN (R 3.6.0)                    
#>  knitr         1.26       2019-11-12 [1] CRAN (R 3.6.0)                    
#>  labeling      0.3        2014-08-23 [1] CRAN (R 3.6.0)                    
#>  lattice       0.20-38    2018-11-04 [1] CRAN (R 3.6.0)                    
#>  lifecycle     0.1.0      2019-08-01 [1] CRAN (R 3.6.0)                    
#>  lubridate     1.7.4      2018-04-11 [1] CRAN (R 3.6.0)                    
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)                    
#>  MASS          7.3-51.4   2019-03-31 [1] CRAN (R 3.6.0)                    
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.6.0)                    
#>  mime          0.8        2019-12-19 [1] CRAN (R 3.6.0)                    
#>  modelr        0.1.5      2019-08-08 [1] CRAN (R 3.6.0)                    
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.6.0)                    
#>  nlme          3.1-140    2019-05-12 [1] CRAN (R 3.6.0)                    
#>  pillar        1.4.3      2019-12-20 [1] CRAN (R 3.6.0)                    
#>  pkgbuild      1.0.6      2019-10-09 [1] CRAN (R 3.6.0)                    
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 3.6.0)                    
#>  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.6.0)                    
#>  prettyunits   1.1.1      2020-01-24 [1] CRAN (R 3.6.0)                    
#>  processx      3.4.2      2020-02-09 [1] CRAN (R 3.6.0)                    
#>  ps            1.3.2      2020-02-13 [1] CRAN (R 3.6.0)                    
#>  purrr       * 0.3.3      2019-10-18 [1] CRAN (R 3.6.0)                    
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.0)                    
#>  Rcpp          1.0.3      2019-11-08 [1] CRAN (R 3.6.0)                    
#>  readr       * 1.3.1      2018-12-21 [1] CRAN (R 3.6.0)                    
#>  readxl        1.3.1      2019-03-13 [1] CRAN (R 3.6.0)                    
#>  remotes       2.1.0      2019-06-24 [1] CRAN (R 3.6.0)                    
#>  rlang         0.4.4      2020-01-28 [1] CRAN (R 3.6.0)                    
#>  rmarkdown     1.15       2019-08-21 [1] CRAN (R 3.6.0)                    
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)                    
#>  rvest         0.3.4      2019-05-15 [1] CRAN (R 3.6.0)                    
#>  scales        1.1.0      2019-11-18 [1] CRAN (R 3.6.0)                    
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                    
#>  stringi       1.4.5      2020-01-11 [1] CRAN (R 3.6.0)                    
#>  stringr     * 1.4.0      2019-02-10 [1] CRAN (R 3.6.0)                    
#>  testthat      2.3.1      2019-12-01 [1] CRAN (R 3.6.0)                    
#>  tibble      * 2.1.3      2019-06-06 [1] CRAN (R 3.6.0)                    
#>  tidyr       * 1.0.0      2019-09-11 [1] CRAN (R 3.6.0)                    
#>  tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.6.0)                    
#>  tidyverse   * 1.2.1      2017-11-14 [1] CRAN (R 3.6.0)                    
#>  usethis       1.5.1      2019-07-04 [1] CRAN (R 3.6.0)                    
#>  vctrs         0.2.3      2020-02-20 [1] CRAN (R 3.6.0)                    
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                    
#>  xfun          0.12       2020-01-13 [1] CRAN (R 3.6.0)                    
#>  xml2          1.2.2      2019-08-09 [1] CRAN (R 3.6.0)                    
#>  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.6.0)                    
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

The reason is that StatDensity2d unconditionally calls StatContour. There is no way to make it call StatContourFilled.

if (contour) {
StatContour$compute_panel(df, scales, bins, binwidth)
} else {

@clauswilke
Copy link
Member

clauswilke commented Mar 1, 2020

This is how we'd have to do it if we calculate the density grid manually.

library(tidyverse)

data(airquality)

airQ <- filter_all(airquality, ~!is.na(.))

h <- c(MASS::bandwidth.nrd(airQ$Solar.R), MASS::bandwidth.nrd(airQ$Temp))
dens <- MASS::kde2d(
  airQ$Solar.R, airQ$Temp, h = h, n = 100,
  lims = c(range(airQ$Solar.R), range(airQ$Temp))
)
df <- expand.grid(x = dens$x, y = dens$y)
df$z <- as.vector(dens$z)

ggplot(df, aes(x, y, z = z, fill = after_stat(level))) +
  geom_contour_filled() +
  scale_fill_viridis_d()

Created on 2020-03-01 by the reprex package (v0.3.0)

@clauswilke
Copy link
Member

clauswilke commented Mar 7, 2020

I just opened a PR (#3864):

library(tidyverse)

data(airquality)

airQ <- filter_all(airquality, ~!is.na(.))
ggplot(airQ, aes(x = Solar.R, y = Temp)) +
  geom_point() +
  stat_density2d(
    aes(fill = stat(level)),
    geom = "polygon",
    contour_type = "bands",
    alpha = 0.7
  )

Created on 2020-03-06 by the reprex package (v0.3.0)

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 a pull request may close this issue.

3 participants