mjskay / ggdist

Visualizations of distributions and uncertainty
https://mjskay.github.io/ggdist/
GNU General Public License v3.0
848 stars 26 forks source link

facet_wrap() and stat_halfeye() !!! #195

Closed MaryamEO closed 1 year ago

MaryamEO commented 1 year ago

I have been trying to combine facet_wrap() with stat_halfeye() in my plot. While the box plots are correctly organized in the facets, the density plots are not shown.

The line of codes that I have been using is mentioned below!

fig <- ggplot(Distribution, aes( y = sum, x = AF)) + ggdist::stat_halfeye( aes(color = Phenotype, fill = after_scale(alpha(color, 0.4))), adjust = .5, width = .6, .width = 0, justification = -.4, point_color = NA) + geom_boxplot( aes(color = Phenotype, color = after_scale(alpha(color, 0.4)), fill = after_scale(desaturate(lighten(color, .8), .4))), width = .3, )+ facet_wrap(~as.factor(category), nrow = 3)

mjskay commented 1 year ago

Hi --- without knowing the underlying data, it's hard to tell what might be wrong. If you prepare a reprex that generates a chart with the problem you're having I'd be happy to help!

mjskay commented 1 year ago

closing due to lack of reprex --- feel free to re-open if you provide a reprex

henningte commented 9 months ago

I think I have the same or a similar problem as @MaryamEO, but I'm not sure whether 'ggdist' is expected to work like that or whether I have missed an already existing solution.

The problem is that plotting densities or histograms of variables with very different ranges results in very different scaling of the resulting density/histograms. This is of course no surprise because density is no probability.

However when creating a facetted plot with scales = "free", I had expected that the densities would all get scaled to the maximum density in each panel, but they do not; they are rather scaled to the maximum overall density (just as when setting scales = "fixed"):

library(ggplot2)
library(posterior)
#> This is posterior version 1.2.2
#> 
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#> 
#>     mad, sd, var

set.seed(4545)

x <- 
  tibble::tibble(
    x = 
      posterior::rvar_rng(rnorm, n = 4, mean = 0, sd = c(0.0001, 0.5, 2, 40), ndraws = 1000),
    id = seq_along(x)
  )

# facetted plot with four sets of samples from normal distributions with very different x axis scaling
x |>
  ggplot() +
  ggdist::stat_slab(mapping = aes(xdist = x)) +
  facet_wrap( ~ id, scales = "free")

# same excluding the first group
x |>
  dplyr::filter(id != 1) |>
  ggplot() +
  ggdist::stat_slab(mapping = aes(xdist = x)) +
  facet_wrap( ~ id, scales = "free")

Created on 2024-01-04 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.0 (2022-04-22) #> os Ubuntu 20.04.6 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Etc/UTC #> date 2024-01-04 #> pandoc 2.17.1.1 @ /usr/lib/rstudio-server/bin/quarto/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> ! package * version date (UTC) lib source #> P abind 1.4-5 2016-07-21 [?] RSPM (R 4.2.0) #> P assertthat 0.2.1 2019-03-21 [?] RSPM (R 4.2.0) #> P backports 1.4.1 2021-12-13 [?] RSPM (R 4.2.0) #> P checkmate 2.1.0 2022-04-21 [?] RSPM (R 4.2.0) #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.2.0) #> P colorspace 2.0-3 2022-02-21 [?] RSPM (R 4.2.0) #> P crayon 1.5.1 2022-03-26 [?] RSPM (R 4.2.0) #> P curl 4.3.2 2021-06-23 [?] RSPM (R 4.2.0) #> P DBI 1.1.3 2022-06-18 [?] RSPM (R 4.2.0) #> P digest 0.6.29 2021-12-01 [?] RSPM (R 4.2.0) #> P distributional 0.3.0 2022-01-05 [?] RSPM (R 4.2.0) #> P dplyr 1.0.9 2022-04-28 [?] RSPM (R 4.2.0) #> P ellipsis 0.3.2 2021-04-29 [?] RSPM (R 4.2.0) #> P evaluate 0.15 2022-02-18 [?] RSPM (R 4.2.0) #> P fansi 1.0.3 2022-03-24 [?] RSPM (R 4.2.0) #> P farver 2.1.0 2021-02-28 [?] RSPM (R 4.2.0) #> P fastmap 1.1.0 2021-01-25 [?] RSPM (R 4.2.0) #> P fs 1.5.2 2021-12-08 [?] RSPM (R 4.2.0) #> P generics 0.1.2 2022-01-31 [?] RSPM (R 4.2.0) #> P ggdist 3.1.1 2022-02-27 [?] RSPM (R 4.2.0) #> ggplot2 * 3.4.4 2023-10-12 [1] CRAN (R 4.2.0) #> P glue 1.6.2 2022-02-24 [?] RSPM (R 4.2.0) #> P gtable 0.3.0 2019-03-25 [?] RSPM (R 4.2.0) #> P highr 0.9 2021-04-16 [?] RSPM (R 4.2.0) #> P htmltools 0.5.2 2021-08-25 [?] RSPM (R 4.2.0) #> P httr 1.4.3 2022-05-04 [?] RSPM (R 4.2.0) #> P knitr 1.39 2022-04-26 [?] RSPM (R 4.2.0) #> P labeling 0.4.2 2020-10-20 [?] RSPM (R 4.2.0) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.2.0) #> P magrittr 2.0.3 2022-03-30 [?] RSPM (R 4.2.0) #> P mime 0.12 2021-09-28 [?] RSPM (R 4.2.0) #> P munsell 0.5.0 2018-06-12 [?] RSPM (R 4.2.0) #> P pillar 1.7.0 2022-02-01 [?] RSPM (R 4.2.0) #> P pkgconfig 2.0.3 2019-09-22 [?] RSPM (R 4.2.0) #> P posterior * 1.2.2 2022-06-09 [?] RSPM (R 4.2.0) #> P purrr 0.3.4 2020-04-17 [?] RSPM (R 4.2.0) #> P R6 2.5.1 2021-08-19 [?] RSPM (R 4.2.0) #> P reprex 2.0.1 2021-08-05 [?] RSPM (R 4.2.0) #> rlang 1.1.2 2023-11-04 [1] CRAN (R 4.2.0) #> P rmarkdown 2.14 2022-04-25 [?] RSPM (R 4.2.0) #> P rstudioapi 0.13 2020-11-12 [?] RSPM (R 4.2.0) #> P scales 1.2.0 2022-04-13 [?] RSPM (R 4.2.0) #> P sessioninfo 1.2.2 2021-12-06 [?] RSPM (R 4.2.0) #> P stringi 1.7.6 2021-11-29 [?] RSPM (R 4.2.0) #> P stringr 1.4.0 2019-02-10 [?] RSPM (R 4.2.0) #> P tensorA 0.36.2 2020-11-19 [?] RSPM (R 4.2.0) #> P tibble 3.1.7 2022-05-03 [?] RSPM (R 4.2.0) #> P tidyselect 1.1.2 2022-02-21 [?] RSPM (R 4.2.0) #> P utf8 1.2.2 2021-07-24 [?] RSPM (R 4.2.0) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.2.0) #> P withr 2.5.0 2022-03-03 [?] RSPM (R 4.2.0) #> P xfun 0.31 2022-05-10 [?] RSPM (R 4.2.0) #> P xml2 1.3.3 2021-11-30 [?] RSPM (R 4.2.0) #> P yaml 2.3.5 2022-02-21 [?] RSPM (R 4.2.0) #> #> [1] /home/rstudio/.cache/R/renv/library/pdpm-c04c2f15/R-4.2/x86_64-pc-linux-gnu #> [2] /home/rstudio/pdpm/pdpm/renv/sandbox/R-4.2/x86_64-pc-linux-gnu/25ebdc09 #> [3] /usr/local/lib/R/library #> #> P ── Loaded and on-disk path mismatch. #> #> ────────────────────────────────────────────────────────────────────────────── ```

A possible workaround is to manually compute the maximum density and pass as scale to stat_slab():

x |>
  dplyr::mutate(
    scale = 
      purrr::map_dbl(seq_along(x), function(i) {
        max(stats::density(attr(x[[i]], "draws")[, 1])$y)
      }),
    scale = max(scale)/scale
  ) |>
  ggplot() +
  ggdist::stat_slab(mapping = aes(xdist = x, scale = scale)) +
  facet_wrap( ~ id, scales = "free")

But as mentioned, I'm not sure whether this is how stat_slab() should behave when using facet_wrap() with scales = "free"

henningte commented 9 months ago

Well, I should've read the documentation better first. I'm sorry for that!

library(ggplot2)
library(posterior)
#> This is posterior version 1.2.2
#> 
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#> 
#>     mad, sd, var

set.seed(4545)

x <- 
  tibble::tibble(
    x = 
      posterior::rvar_rng(rnorm, n = 4, mean = 0, sd = c(0.0001, 0.5, 2, 40), ndraws = 1000),
    id = seq_along(x)
  )

x |>
  ggplot() +
  ggdist::stat_slab(mapping = aes(xdist = x), normalize = "panels") +
  facet_wrap( ~ id, scales = "free")

Created on 2024-01-04 by the reprex package (v2.0.1)

mjskay commented 9 months ago

No worries that's a bit of the docs folks often miss! I have plans to make it easier to find (#205)