mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
712 stars 59 forks source link

limit problems with stat_gradientinterval #246

Closed yonicd closed 3 years ago

yonicd commented 4 years ago

library(magrittr)
set.seed(1234)
df <- tibble::tribble(
  ~group, ~subgroup, ~value,
  "a",          "h", rnorm(500, mean = 5),
  "b",          "h", rnorm(500, mean = 7, sd = 1.5),
  "c",          "h", rnorm(500, mean = 8),
  "c",          "i", rnorm(500, mean = 9),
  "c",          "j", rnorm(500, mean = 7)
) %>%
  tidyr::unnest(value)

df %>%
  ggplot2::ggplot(ggplot2::aes(x = group, y = value)) +
  tidybayes::stat_gradientinterval()


df %>%
  ggplot2::ggplot(ggplot2::aes(x = group, y = value)) +
  tidybayes::stat_gradientinterval() + 
  ggplot2::scale_y_continuous(limit = c(0,11))
#> Warning: Computation failed in `stat_gradientinterval()`:
#> 'x' contains missing values


details::details(sessioninfo::session_info(),summary = 'sessioninfo')
sessioninfo ``` r ─ Session info ─────────────────────────────────────────────────────────────── setting value version R version 3.5.1 (2018-07-02) os Ubuntu 14.04.5 LTS system x86_64, linux-gnu ui X11 language (EN) collate en_US.UTF-8 ctype en_US.UTF-8 tz Etc/UTC date 2020-04-15 ─ Packages ─────────────────────────────────────────────────────────────────── package * version date lib source arrayhelpers 1.1-0 2020-02-04 [1] CRAN (R 3.5.1) assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.5.1) backports 1.1.5 2019-10-02 [1] CRAN (R 3.5.1) cli 2.0.1 2020-01-08 [1] CRAN (R 3.5.1) clipr 0.7.0 2019-07-23 [1] CRAN (R 3.5.1) coda 0.19-3 2019-07-05 [1] CRAN (R 3.5.1) colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.5.1) crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.1) desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.1) details 0.2.1 2020-01-12 [1] CRAN (R 3.5.1) digest 0.6.24 2020-02-12 [1] CRAN (R 3.5.1) dplyr 0.8.4 2020-01-31 [1] CRAN (R 3.5.1) evaluate 0.14 2019-05-28 [1] CRAN (R 3.5.1) fansi 0.4.1 2020-01-08 [1] CRAN (R 3.5.1) farver 2.0.3 2020-01-16 [1] CRAN (R 3.5.1) forcats 0.4.0 2019-02-17 [1] CRAN (R 3.5.1) ggplot2 3.2.1 2019-08-10 [1] CRAN (R 3.5.1) glue 1.3.1 2019-03-12 [1] CRAN (R 3.5.1) gtable 0.3.0 2019-03-25 [1] CRAN (R 3.5.1) highr 0.8 2019-03-20 [1] CRAN (R 3.5.1) htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.5.1) httr 1.4.1 2019-08-05 [1] CRAN (R 3.5.1) knitr 1.28 2020-02-06 [1] CRAN (R 3.5.1) labeling 0.3 2014-08-23 [1] CRAN (R 3.5.1) lattice 0.20-38 2018-11-04 [1] CRAN (R 3.5.1) lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.5.1) lifecycle 0.1.0 2019-08-01 [1] CRAN (R 3.5.1) magrittr * 1.5 2014-11-22 [1] CRAN (R 3.5.1) munsell 0.5.0 2018-06-12 [1] CRAN (R 3.5.1) pillar 1.4.3 2019-12-20 [1] CRAN (R 3.5.1) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.5.1) plyr 1.8.5 2019-12-10 [1] CRAN (R 3.5.1) png 0.1-7 2013-12-03 [1] CRAN (R 3.5.1) purrr 0.3.3 2019-10-18 [1] CRAN (R 3.5.1) R6 2.4.1 2019-11-12 [1] CRAN (R 3.5.1) Rcpp 1.0.3 2019-11-08 [1] CRAN (R 3.5.1) rlang 0.4.4 2020-01-28 [1] CRAN (R 3.5.1) rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.5.1) rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.1) scales 1.1.0 2019-11-18 [1] CRAN (R 3.5.1) sessioninfo 1.1.1 2018-11-05 [3] CRAN (R 3.5.1) stringi 1.4.6 2020-02-17 [1] CRAN (R 3.5.1) stringr 1.4.0 2019-02-10 [1] CRAN (R 3.5.1) svUnit 0.7-12 2014-03-02 [1] CRAN (R 3.5.1) tibble 2.1.3 2019-06-06 [1] CRAN (R 3.5.1) tidybayes 2.0.1 2020-01-28 [1] CRAN (R 3.5.1) tidyr 1.0.2 2020-01-24 [1] CRAN (R 3.5.1) tidyselect 1.0.0 2020-01-27 [1] CRAN (R 3.5.1) vctrs 0.2.3 2020-02-20 [1] CRAN (R 3.5.1) withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.1) xfun 0.12 2020-01-13 [1] CRAN (R 3.5.1) xml2 1.2.2 2019-08-09 [1] CRAN (R 3.5.1) yaml 2.2.1 2020-02-01 [1] CRAN (R 3.5.1) [1] /data/yonis/lib [2] /usr/lib/R/library ```


Created on 2020-04-15 by the reprex package (v0.3.0)

mjskay commented 4 years ago

Thanks for this! The first thing to try would be to use coord_cartesian() instead of the scale to set limits (using the scale deletes data).

I still don't think this should be causing an error but that should hopefully at least give you the output you want.

yonicd commented 4 years ago

coord_cartestian worked. thanks.

my problem is a smidge more nuanced. i have two plots that are created indep and i am using patchwork to combine them. At this point i need to manip the axis to realign the plots.

mjskay commented 4 years ago

Makes sense, I've had to do that with patchwork myself. For axis manipulation coord_cartesian is usually better since it doesn't delete data (this difference is at the ggplot level, not the geom level).

I'm leaving this open since it still probably shouldn't cause things to totally break with slabinterval.

yonicd commented 4 years ago

getting to the actual axis ranges is a real pain in gg

esp. for mcmc outputs

axis_rng <- function(p,axis = c('x','y')) {

  g <- ggplot2::ggplot_build(p)

  axis <- match.arg(axis,several.ok = TRUE)

  names(axis) <- axis

  lapply(axis,function(x){
    g$layout[[sprintf('panel_scales_%s',x)]][[1]]$range$range   
  })

}
mjskay commented 4 years ago

Yeah I wish patchwork or cowplot had an option to automatically align ranges on plots

yonicd commented 4 years ago

how can i reproduce the intervals of tidybayes::stat_lineribbon for a given value of x?

library(magrittr)
set.seed(1234)

df <- purrr::rerun(5,tibble::tibble(x = seq(10),y = rnorm(rep(10,10),0,seq(10))))%>%dplyr::bind_rows()

df %>%
  ggplot2::ggplot(ggplot2::aes(x = x, y = y)) +
  tidybayes::stat_lineribbon()

Created on 2020-04-17 by the reprex package (v0.3.0)

yonicd commented 4 years ago

never mind. figured it out

library(magrittr)
set.seed(1234)

df <- purrr::rerun(5,tibble::tibble(x = seq(10),y = rnorm(rep(10,10),0,seq(10))))%>%dplyr::bind_rows()

p <- df %>%
  ggplot2::ggplot(ggplot2::aes(x = x, y = y)) +
  tidybayes::stat_lineribbon()

gp <- ggplot2::ggplot_build(p)

gp$data[[1]]%>%dplyr::select(.width,x,y,ymin,ymax)
#>    .width  x          y        ymin        ymax
#> 1    0.50  1  0.1340882  -0.4771927  1.10229755
#> 2    0.80  1  0.1340882  -0.9151165  1.31061678
#> 3    0.95  1  0.1340882  -1.1340784  1.41477639
#> 4    0.50  2 -0.9813718  -1.9967729 -0.95118616
#> 5    0.80  2 -0.9813718  -2.0810804 -0.04755937
#> 6    0.95  2 -0.9813718  -2.1232342  0.40425402
#> 7    0.50  3 -2.1283201  -2.3287617 -1.32164362
#> 8    0.80  3 -2.1283201  -2.4711610  1.42333667
#> 9    0.95  3 -2.1283201  -2.5423607  2.79582682
#> 10   0.50  4 -1.1224920  -2.0050322  0.25783527
#> 11   0.80  4 -1.1224920  -6.4316874  1.20614877
#> 12   0.95  4 -1.1224920  -8.6450150  1.68030551
#> 13   0.50  5 -3.4686012  -4.9717004  2.14562344
#> 14   0.80  5 -3.4686012  -6.8759606  3.73673155
#> 15   0.95  5 -3.4686012  -7.8280906  4.53228561
#> 16   0.50  6 -5.8110859  -7.0057156 -0.66171297
#> 17   0.80  6 -5.8110859  -8.0158239  1.55711603
#> 18   0.95  6 -5.8110859  -8.5208781  2.66653052
#> 19   0.50  7 -4.0231797  -7.7512273 -3.57706654
#> 20   0.80  7 -4.0231797 -12.2566575  0.98314741
#> 21   0.95  7 -4.0231797 -14.5093725  3.26325439
#> 22   0.50  8 -8.1892458 -10.0158871 -7.28956333
#> 23   0.80  8 -8.1892458 -10.4431222 -5.53965824
#> 24   0.95  8 -8.1892458 -10.6567397 -4.66470569
#> 25   0.50  9 -4.7144531  -5.0800680 -2.64864473
#> 26   0.80  9 -4.7144531  -6.5527543 -1.14120471
#> 27   0.95  9 -4.7144531  -7.2890974 -0.38748471
#> 28   0.50 10 -4.9684996  -8.9003783 -4.65897540
#> 29   0.80 10 -4.9684996  -9.1758429 12.63142091
#> 30   0.95 10 -4.9684996  -9.3135752 21.27661907

df%>%
  dplyr::group_by(x)%>%
  dplyr::summarise(
    w80_q10_ymin = quantile(y,0.1),w80_q90_ymax = quantile(y,1-.1),
    w95_q025_ymin = quantile(y,0.025),w95_q975_ymax = quantile(y,1-.025)
  )%>%
  tidyr::pivot_longer(names_to = 'q',values_to = 'val',dplyr::contains('q'))%>%
  tidyr::separate(q,c('width','place','type'))%>%
  dplyr::select(-place)%>%
  tidyr::pivot_wider(names_from = type,values_from = val)
#> # A tibble: 20 x 4
#>        x width    ymin    ymax
#>    <int> <chr>   <dbl>   <dbl>
#>  1     1 w80    -0.915  1.31  
#>  2     1 w95    -1.13   1.41  
#>  3     2 w80    -2.08  -0.0476
#>  4     2 w95    -2.12   0.404 
#>  5     3 w80    -2.47   1.42  
#>  6     3 w95    -2.54   2.80  
#>  7     4 w80    -6.43   1.21  
#>  8     4 w95    -8.65   1.68  
#>  9     5 w80    -6.88   3.74  
#> 10     5 w95    -7.83   4.53  
#> 11     6 w80    -8.02   1.56  
#> 12     6 w95    -8.52   2.67  
#> 13     7 w80   -12.3    0.983 
#> 14     7 w95   -14.5    3.26  
#> 15     8 w80   -10.4   -5.54  
#> 16     8 w95   -10.7   -4.66  
#> 17     9 w80    -6.55  -1.14  
#> 18     9 w95    -7.29  -0.387 
#> 19    10 w80    -9.18  12.6   
#> 20    10 w95    -9.31  21.3

Created on 2020-04-17 by the reprex package (v0.3.0)

mjskay commented 4 years ago

You can also use median_qi (or any of the point_interval functions) to do this

mjskay commented 3 years ago

closing this as everything seems resolved