library(mrgmisc) # for binning and other functions
library(ggplot2) #plotting
library(purrr) # dataset to handle lists
suppressMessages(suppressWarnings(library(dplyr)))
dat <- sd_oral_richpk %>% filter(ID <= 20)

Messy plot can’t see individuals well

ggplot(dat, aes(x = Time, y = Conc, group= ID)) + 
  geom_line() +
  facet_wrap(~ID, scales= "free")

What we’d like to be able to do is split up into ‘bins’ of specified numbers of individuals to then plot out multiple plots

# this will automatically create a column of bins such that the
# specified number of individuals is in each bin, in this case
# 9 ids per bin
# split the original dataset into subdatasets corresponding to each bin (list of dataframes)
split_dat <- dat %>% 
  mutate(PLOTS = ids_per_plot(ID, 4)) %>% # default is 9 per subplot
  split(.[["PLOTS"]])

To handle plotting each subdataframe, you need to wrap your normal ggplot into a function. You will then apply this function with map to each subdataframe. So in this case it is just like a normal plot, but wrapped up in a function, which takes 1 argument (the dataframe) and outputs the plot

p_conc_time <- function(df) {
ggplot(df, aes(x = Time, y = Conc, group= ID)) + 
  geom_line() +
  facet_wrap(~ID, scales= "free")
}

To apply the above function we use map from purrr

split_dat %>% map(p_conc_time) 
## $`1`

## 
## $`2`

## 
## $`3`

## 
## $`4`

## 
## $`5`

sessioninfo::session_info()
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value
##  version  R version 4.3.1 (2023-06-16)
##  os       Ubuntu 20.04.5 LTS
##  system   x86_64, linux-gnu
##  ui       X11
##  language en
##  collate  en_US.UTF-8
##  ctype    en_US.UTF-8
##  tz       America/New_York
##  date     2024-07-29
##  pandoc   3.1.8 @ /usr/bin/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  ! package     * version date (UTC) lib source
##    bslib         0.7.0   2024-03-29 [1] CRAN (R 4.3.1)
##    cachem        1.1.0   2024-05-16 [1] CRAN (R 4.3.1)
##    cli           3.6.3   2024-06-21 [1] CRAN (R 4.3.1)
##    colorspace    2.1-1   2024-07-26 [1] CRAN (R 4.3.1)
##    desc          1.4.3   2023-12-10 [1] CRAN (R 4.3.1)
##    digest        0.6.36  2024-06-23 [1] CRAN (R 4.3.1)
##    dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.3.1)
##    evaluate      0.24.0  2024-06-10 [1] CRAN (R 4.3.1)
##    fansi         1.0.6   2023-12-08 [1] CRAN (R 4.3.1)
##    farver        2.1.2   2024-05-13 [1] CRAN (R 4.3.1)
##    fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.3.1)
##    fs            1.6.4   2024-04-25 [1] CRAN (R 4.3.1)
##    generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.1)
##    ggplot2     * 3.5.1   2024-04-23 [1] CRAN (R 4.3.1)
##    glue          1.7.0   2024-01-09 [1] CRAN (R 4.3.1)
##    gtable        0.3.5   2024-04-22 [1] CRAN (R 4.3.1)
##    highr         0.11    2024-05-26 [1] CRAN (R 4.3.1)
##    htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.3.1)
##    htmlwidgets   1.6.4   2023-12-06 [1] CRAN (R 4.3.1)
##    jquerylib     0.1.4   2021-04-26 [1] CRAN (R 4.3.1)
##    jsonlite      1.8.8   2023-12-04 [1] CRAN (R 4.3.1)
##    knitr         1.48    2024-07-07 [1] CRAN (R 4.3.1)
##    labeling      0.4.3   2023-08-29 [1] CRAN (R 4.3.1)
##    lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.3.1)
##    magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.1)
##    mrgmisc     * 0.2.1   2024-07-29 [1] local (/data/mrgmisc_0.2.1.tar.gz)
##    munsell       0.5.1   2024-04-01 [1] CRAN (R 4.3.1)
##    pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.1)
##    pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.1)
##    pkgdown       2.1.0   2024-07-06 [1] CRAN (R 4.3.1)
##    purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.1)
##    R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.1)
##    ragg          1.3.2   2024-05-15 [1] CRAN (R 4.3.1)
##  P Rcpp          1.0.12  2024-01-09 [?] RSPM
##    renv          1.0.5   2024-02-29 [1] CRAN (R 4.3.1)
##  P rlang         1.1.3   2024-01-10 [?] RSPM
##    rmarkdown     2.27    2024-05-17 [1] CRAN (R 4.3.1)
##    rstudioapi    0.16.0  2024-03-24 [1] CRAN (R 4.3.1)
##    sass          0.4.9   2024-03-15 [1] CRAN (R 4.3.1)
##    scales        1.3.0   2023-11-28 [1] CRAN (R 4.3.1)
##    sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.1)
##    systemfonts   1.1.0   2024-05-15 [1] CRAN (R 4.3.1)
##    textshaping   0.4.0   2024-05-24 [1] CRAN (R 4.3.1)
##    tibble        3.2.1   2023-03-20 [1] CRAN (R 4.3.1)
##    tidyselect    1.2.1   2024-03-11 [1] CRAN (R 4.3.1)
##    utf8          1.2.4   2023-10-22 [1] CRAN (R 4.3.1)
##    vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.3.1)
##    withr         3.0.0   2024-01-16 [1] CRAN (R 4.3.1)
##    xfun          0.46    2024-07-18 [1] CRAN (R 4.3.1)
##    yaml          2.3.10  2024-07-26 [1] CRAN (R 4.3.1)
## 
##  [1] /data/home/michaelm/.cache/R/renv/library/mrgmisc-d8b026db/R-4.3/x86_64-pc-linux-gnu
##  [2] /data/home/michaelm/.cache/R/renv/sandbox/R-4.3/x86_64-pc-linux-gnu/5cd49154
## 
##  P ── Loaded and on-disk path mismatch.
## 
## ──────────────────────────────────────────────────────────────────────────────