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

Make facet_sample and facet_strata have more flexible defaults #109

Open
njtierney opened this issue Nov 19, 2021 · 1 comment
Open

Make facet_sample and facet_strata have more flexible defaults #109

njtierney opened this issue Nov 19, 2021 · 1 comment

Comments

@njtierney
Copy link
Owner

Currently if there aren't enough keys for default facets it fails:

library(tidyverse)
library(brolgar)

df <- tibble(
  id = rep(LETTERS[1:10], each = 5),
  time = rep(1:5, length.out = 50),
  value_bin = sample(c(0L, 1L), size = 50, replace = TRUE),
  value_con = rnorm(50)
)

df_ts <- as_tsibble(
  df,
  key = id,
  index = time
)

p <- ggplot(df_ts,
       aes(x = time,
           y = value_con,
           group = id)) +
  geom_line() 

p + facet_strata()
#> Error in gList(structure(list(x = structure(0.5, unit = 0L, class = c("simpleUnit", : only 'grobs' allowed in "gList"
p + facet_sample()
#> Error in sample.int(length(x), size, replace, prob): cannot take a sample larger than the population when 'replace = FALSE'

p + facet_strata(n_strata = 3)

p + facet_sample(n_facets = 3)

Created on 2021-11-19 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.1 (2021-08-10)
#>  os       macOS Big Sur 10.16         
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_AU.UTF-8                 
#>  ctype    en_AU.UTF-8                 
#>  tz       Australia/Perth             
#>  date     2021-11-19                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package        * version    date       lib
#>  anytime          0.3.9      2020-08-27 [1]
#>  assertthat       0.2.1      2019-03-21 [1]
#>  backports        1.3.0      2021-10-27 [1]
#>  brolgar        * 0.1.2      2021-08-25 [1]
#>  broom            0.7.9      2021-07-27 [1]
#>  cellranger       1.1.0      2016-07-27 [1]
#>  cli              3.1.0      2021-10-27 [1]
#>  colorspace       2.0-2      2021-06-24 [1]
#>  crayon           1.4.1      2021-02-08 [1]
#>  curl             4.3.2      2021-06-23 [1]
#>  DBI              1.1.1      2021-01-15 [1]
#>  dbplyr           2.1.1      2021-04-06 [1]
#>  digest           0.6.28     2021-09-23 [1]
#>  distributional   0.2.2.9000 2021-10-11 [1]
#>  dplyr          * 1.0.7      2021-06-18 [1]
#>  ellipsis         0.3.2      2021-04-29 [1]
#>  evaluate         0.14       2019-05-28 [1]
#>  fabletools       0.3.1      2021-03-16 [1]
#>  fansi            0.5.0      2021-05-25 [1]
#>  farver           2.1.0      2021-02-28 [1]
#>  fastmap          1.1.0      2021-01-25 [1]
#>  forcats        * 0.5.1      2021-01-27 [1]
#>  fs               1.5.0      2020-07-31 [1]
#>  generics         0.1.1      2021-10-25 [1]
#>  ggplot2        * 3.3.5      2021-06-25 [1]
#>  glue             1.5.0      2021-11-07 [1]
#>  gtable           0.3.0      2019-03-25 [1]
#>  haven            2.4.3      2021-08-04 [1]
#>  highr            0.9        2021-04-16 [1]
#>  hms              1.1.1      2021-09-26 [1]
#>  htmltools        0.5.2      2021-08-25 [1]
#>  httr             1.4.2      2020-07-20 [1]
#>  jsonlite         1.7.2      2020-12-09 [1]
#>  knitr            1.36       2021-09-29 [1]
#>  labeling         0.4.2      2020-10-20 [1]
#>  lifecycle        1.0.1      2021-09-24 [1]
#>  lubridate        1.8.0      2021-10-07 [1]
#>  magrittr         2.0.1      2020-11-17 [1]
#>  mime             0.12       2021-09-28 [1]
#>  modelr           0.1.8      2020-05-19 [1]
#>  munsell          0.5.0      2018-06-12 [1]
#>  pillar           1.6.4      2021-10-18 [1]
#>  pkgconfig        2.0.3      2019-09-22 [1]
#>  purrr          * 0.3.4      2020-04-17 [1]
#>  R.cache          0.15.0     2021-04-30 [1]
#>  R.methodsS3      1.8.1      2020-08-26 [1]
#>  R.oo             1.24.0     2020-08-26 [1]
#>  R.utils          2.11.0     2021-09-26 [1]
#>  R6               2.5.1      2021-08-19 [1]
#>  Rcpp             1.0.7      2021-07-07 [1]
#>  readr          * 2.0.2      2021-09-27 [1]
#>  readxl           1.3.1      2019-03-13 [1]
#>  reprex           2.0.1      2021-08-05 [1]
#>  rlang            0.4.12     2021-10-18 [1]
#>  rmarkdown        2.11       2021-09-14 [1]
#>  rstudioapi       0.13       2020-11-12 [1]
#>  rvest            1.0.1      2021-07-26 [1]
#>  scales           1.1.1      2020-05-11 [1]
#>  sessioninfo      1.1.1      2018-11-05 [1]
#>  stringi          1.7.5      2021-10-04 [1]
#>  stringr        * 1.4.0      2019-02-10 [1]
#>  styler           1.6.2      2021-09-23 [1]
#>  tibble         * 3.1.5      2021-09-30 [1]
#>  tidyr          * 1.1.4      2021-09-27 [1]
#>  tidyselect       1.1.1      2021-04-30 [1]
#>  tidyverse      * 1.3.1      2021-04-15 [1]
#>  tsibble          1.0.1      2021-04-12 [1]
#>  tzdb             0.1.2      2021-07-20 [1]
#>  utf8             1.2.2      2021-07-24 [1]
#>  vctrs            0.3.8      2021-04-29 [1]
#>  withr            2.4.2      2021-04-18 [1]
#>  xfun             0.26       2021-09-14 [1]
#>  xml2             1.3.2      2020-04-23 [1]
#>  yaml             2.2.1      2020-02-01 [1]
#>  source                                           
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.1)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.1)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  Github (mitchelloharawild/distributional@82e2919)
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.1)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#>  CRAN (R 4.1.0)                                   
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

A couple of solutions:

  • Gracefully error and say, "the default is 9, we can't do that, change argument <this_arg>"
  • Error and provide duplicated panels that are blank?
  • Change the number of facets dynamically, with the ideal being 9, but could scale down
@njtierney
Copy link
Owner Author

Just adding a little helper function to create different sized data.

library(tidyverse)
library(brolgar)

create_example_tsibble <- function(n_id = 10,
                                   n_obs = 10){
  
  df_obs_total <- n_obs * n_id
  
  df <- tibble(
    id = rep(x = LETTERS[seq_len(n_id)], 
             each = n_obs),
    time = rep(x = seq_len(n_obs), 
               length.out = df_obs_total),
    value_bin = sample(x = c(0L, 1L), 
                       size = df_obs_total, 
                       replace = TRUE),
    value_con = rnorm(n = df_obs_total),
    value_fct = factor(
      x = sample(x = state.name[1:5], 
                 size = df_obs_total, 
                 replace = TRUE)
    )
  )
  
  df_ts <- as_tsibble(
    df,
    key = id,
    index = time
  )
  
  df_ts
  
}

df_ts <- create_example_tsibble(n_id = 2, n_obs = 5)

df_ts
#> # A tsibble: 10 x 5 [1]
#> # Key:       id [2]
#>    id     time value_bin value_con value_fct 
#>    <chr> <int>     <int>     <dbl> <fct>     
#>  1 A         1         1     0.613 Alabama   
#>  2 A         2         0    -0.397 California
#>  3 A         3         0    -2.07  California
#>  4 A         4         1    -0.586 Arizona   
#>  5 A         5         1    -0.733 Arizona   
#>  6 B         1         1    -2.01  Arkansas  
#>  7 B         2         0     1.35  Arizona   
#>  8 B         3         0    -0.688 Alaska    
#>  9 B         4         0     0.854 Arkansas  
#> 10 B         5         0     1.19  Alaska

Created on 2021-12-14 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_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Perth
#>  date     2021-12-14
#>  pandoc   2.14.0.3 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package        * version    date (UTC) lib source
#>  anytime          0.3.9      2020-08-27 [1] CRAN (R 4.1.0)
#>  assertthat       0.2.1      2019-03-21 [1] CRAN (R 4.1.0)
#>  backports        1.4.0      2021-11-23 [1] CRAN (R 4.1.0)
#>  brolgar        * 0.1.2      2021-08-25 [1] CRAN (R 4.1.0)
#>  broom            0.7.10     2021-10-31 [1] CRAN (R 4.1.0)
#>  cellranger       1.1.0      2016-07-27 [1] CRAN (R 4.1.0)
#>  cli              3.1.0      2021-10-27 [1] CRAN (R 4.1.1)
#>  colorspace       2.0-2      2021-06-24 [1] CRAN (R 4.1.0)
#>  crayon           1.4.2      2021-10-29 [1] CRAN (R 4.1.0)
#>  DBI              1.1.1      2021-01-15 [1] CRAN (R 4.1.0)
#>  dbplyr           2.1.1      2021-04-06 [1] CRAN (R 4.1.0)
#>  digest           0.6.29     2021-12-01 [1] CRAN (R 4.1.0)
#>  distributional   0.2.2.9000 2021-10-11 [1] Github (mitchelloharawild/distributional@82e2919)
#>  dplyr          * 1.0.7      2021-06-18 [1] CRAN (R 4.1.0)
#>  ellipsis         0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate         0.14       2019-05-28 [1] CRAN (R 4.1.0)
#>  fabletools       0.3.2      2021-11-29 [1] CRAN (R 4.1.0)
#>  fansi            0.5.0      2021-05-25 [1] CRAN (R 4.1.0)
#>  farver           2.1.0      2021-02-28 [1] CRAN (R 4.1.0)
#>  fastmap          1.1.0      2021-01-25 [1] CRAN (R 4.1.0)
#>  forcats        * 0.5.1      2021-01-27 [1] CRAN (R 4.1.0)
#>  fs               1.5.1      2021-11-30 [1] CRAN (R 4.1.0)
#>  generics         0.1.1      2021-10-25 [1] CRAN (R 4.1.0)
#>  ggplot2        * 3.3.5      2021-06-25 [1] CRAN (R 4.1.0)
#>  glue             1.5.1      2021-11-30 [1] CRAN (R 4.1.0)
#>  gtable           0.3.0      2019-03-25 [1] CRAN (R 4.1.0)
#>  haven            2.4.3      2021-08-04 [1] CRAN (R 4.1.0)
#>  highr            0.9        2021-04-16 [1] CRAN (R 4.1.0)
#>  hms              1.1.1      2021-09-26 [1] CRAN (R 4.1.0)
#>  htmltools        0.5.2      2021-08-25 [1] CRAN (R 4.1.0)
#>  httr             1.4.2      2020-07-20 [1] CRAN (R 4.1.0)
#>  jsonlite         1.7.2      2020-12-09 [1] CRAN (R 4.1.0)
#>  knitr            1.36       2021-09-29 [1] CRAN (R 4.1.0)
#>  lifecycle        1.0.1      2021-09-24 [1] CRAN (R 4.1.0)
#>  lubridate        1.8.0      2021-10-07 [1] CRAN (R 4.1.0)
#>  magrittr         2.0.1      2020-11-17 [1] CRAN (R 4.1.0)
#>  modelr           0.1.8      2020-05-19 [1] CRAN (R 4.1.0)
#>  munsell          0.5.0      2018-06-12 [1] CRAN (R 4.1.0)
#>  pillar           1.6.4      2021-10-18 [1] CRAN (R 4.1.0)
#>  pkgconfig        2.0.3      2019-09-22 [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.7      2021-07-07 [1] CRAN (R 4.1.0)
#>  readr          * 2.1.1      2021-11-30 [1] CRAN (R 4.1.0)
#>  readxl           1.3.1      2019-03-13 [1] CRAN (R 4.1.0)
#>  reprex           2.0.1      2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang            0.4.12     2021-10-18 [1] CRAN (R 4.1.0)
#>  rmarkdown        2.11       2021-09-14 [1] CRAN (R 4.1.0)
#>  rstudioapi       0.13       2020-11-12 [1] CRAN (R 4.1.0)
#>  rvest            1.0.2      2021-10-16 [1] CRAN (R 4.1.0)
#>  scales           1.1.1      2020-05-11 [1] CRAN (R 4.1.0)
#>  sessioninfo      1.2.2      2021-12-06 [1] CRAN (R 4.1.1)
#>  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.6      2021-11-07 [1] CRAN (R 4.1.0)
#>  tidyr          * 1.1.4      2021-09-27 [1] CRAN (R 4.1.0)
#>  tidyselect       1.1.1      2021-04-30 [1] CRAN (R 4.1.0)
#>  tidyverse      * 1.3.1      2021-04-15 [1] CRAN (R 4.1.0)
#>  tsibble          1.1.1      2021-12-03 [1] CRAN (R 4.1.0)
#>  tzdb             0.2.0      2021-10-27 [1] CRAN (R 4.1.0)
#>  utf8             1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs            0.3.8      2021-04-29 [1] CRAN (R 4.1.0)
#>  withr            2.4.3      2021-11-30 [1] CRAN (R 4.1.0)
#>  xfun             0.28       2021-11-04 [1] CRAN (R 4.1.0)
#>  xml2             1.3.3      2021-11-30 [1] CRAN (R 4.1.0)
#>  yaml             2.2.1      2020-02-01 [1] CRAN (R 4.1.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

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

No branches or pull requests

1 participant