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.4.2 (2024-10-31)
##  os       Ubuntu 22.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     2025-04-29
##  pandoc   2.9.2.1 @ /usr/bin/ (via rmarkdown)
##  quarto   1.5.57 @ /usr/local/bin/quarto
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version date (UTC) lib source
##  bslib         0.9.0   2025-01-30 [2] PRISM (R 4.4.1)
##  cachem        1.1.0   2024-05-16 [2] PRISM (R 4.4.1)
##  cli           3.6.4   2025-02-13 [2] PRISM (R 4.4.1)
##  colorspace    2.1-1   2024-07-26 [2] PRISM (R 4.4.1)
##  desc          1.4.3   2023-12-10 [2] PRISM (R 4.4.1)
##  digest        0.6.37  2024-08-19 [2] PRISM (R 4.4.1)
##  dplyr       * 1.1.4   2023-11-17 [2] PRISM (R 4.4.1)
##  evaluate      1.0.3   2025-01-10 [2] PRISM (R 4.4.1)
##  farver        2.1.2   2024-05-13 [2] PRISM (R 4.4.1)
##  fastmap       1.2.0   2024-05-15 [2] PRISM (R 4.4.1)
##  fs            1.6.6   2025-04-12 [2] PRISM (R 4.4.1)
##  generics      0.1.3   2022-07-05 [2] PRISM (R 4.4.1)
##  ggplot2     * 3.5.2   2025-04-09 [2] PRISM (R 4.4.1)
##  glue          1.8.0   2024-09-30 [2] PRISM (R 4.4.1)
##  gtable        0.3.6   2024-10-25 [2] PRISM (R 4.4.1)
##  htmltools     0.5.8.1 2024-04-04 [2] PRISM (R 4.4.1)
##  jquerylib     0.1.4   2021-04-26 [2] PRISM (R 4.4.1)
##  jsonlite      2.0.0   2025-03-27 [2] PRISM (R 4.4.1)
##  knitr         1.50    2025-03-16 [2] PRISM (R 4.4.1)
##  labeling      0.4.3   2023-08-29 [2] PRISM (R 4.4.1)
##  lifecycle     1.0.4   2023-11-07 [2] PRISM (R 4.4.1)
##  magrittr      2.0.3   2022-03-30 [2] PRISM (R 4.4.1)
##  mrgmisc     * 0.2.2   2025-04-29 [1] local
##  munsell       0.5.1   2024-04-01 [2] PRISM (R 4.4.1)
##  pillar        1.10.2  2025-04-05 [2] PRISM (R 4.4.1)
##  pkgconfig     2.0.3   2019-09-22 [2] PRISM (R 4.4.1)
##  pkgdown       2.1.1   2024-09-17 [2] PRISM (R 4.4.1)
##  purrr       * 1.0.4   2025-02-05 [2] PRISM (R 4.4.1)
##  R6            2.6.1   2025-02-15 [2] PRISM (R 4.4.1)
##  ragg          1.4.0   2025-04-10 [2] PRISM (R 4.4.1)
##  Rcpp          1.0.14  2025-01-12 [2] PRISM (R 4.4.1)
##  rlang         1.1.6   2025-04-11 [2] PRISM (R 4.4.1)
##  rmarkdown     2.29    2024-11-04 [2] PRISM (R 4.4.1)
##  sass          0.4.10  2025-04-11 [2] PRISM (R 4.4.1)
##  scales        1.3.0   2023-11-28 [2] PRISM (R 4.4.1)
##  sessioninfo   1.2.3   2025-02-05 [2] PRISM (R 4.4.1)
##  systemfonts   1.2.2   2025-04-04 [2] PRISM (R 4.4.1)
##  textshaping   1.0.0   2025-01-20 [2] PRISM (R 4.4.1)
##  tibble        3.2.1   2023-03-20 [2] PRISM (R 4.4.1)
##  tidyselect    1.2.1   2024-03-11 [2] PRISM (R 4.4.1)
##  vctrs         0.6.5   2023-12-01 [2] PRISM (R 4.4.1)
##  withr         3.0.2   2024-10-28 [2] PRISM (R 4.4.1)
##  xfun          0.52    2025-04-02 [2] PRISM (R 4.4.1)
##  yaml          2.3.10  2024-07-26 [2] PRISM (R 4.4.1)
## 
##  [1] /tmp/RtmpfnYMkN/temp_libpath12243c2cf9958
##  [2] /cluster-data/user-homes/elizabethb/prism-pkgdocs-build/rv-toml-files/rv/library/4.4/x86_64/jammy
##  [3] /opt/R/4.4.2/lib/R/site-library
##  [4] /opt/R/4.4.2/lib/R/library
##  * ── Packages attached to the search path.
## 
## ──────────────────────────────────────────────────────────────────────────────