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     2025-01-13
##  pandoc   3.1.8 @ /usr/bin/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version  date (UTC) lib source
##  bslib         0.8.0    2024-07-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.37   2024-08-19 [1] CRAN (R 4.3.1)
##  dplyr       * 1.1.4    2023-11-17 [1] CRAN (R 4.3.1)
##  evaluate      1.0.1    2024-10-10 [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.5    2024-10-30 [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.8.0    2024-09-30 [1] CRAN (R 4.3.1)
##  gtable        0.3.6    2024-10-25 [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.9    2024-09-20 [1] CRAN (R 4.3.1)
##  knitr         1.49     2024-11-08 [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.2    2025-01-13 [1] local
##  munsell       0.5.1    2024-04-01 [1] CRAN (R 4.3.1)
##  pillar        1.10.1   2025-01-07 [1] CRAN (R 4.3.1)
##  pkgconfig     2.0.3    2019-09-22 [1] CRAN (R 4.3.1)
##  pkgdown       2.1.1    2024-09-17 [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.3    2024-09-11 [1] CRAN (R 4.3.1)
##  Rcpp          1.0.13-1 2024-11-02 [1] CRAN (R 4.3.1)
##  renv          0.14.0   2021-07-21 [1] RSPM (R 4.3.0)
##  rlang         1.1.4    2024-06-04 [1] CRAN (R 4.3.1)
##  rmarkdown     2.29     2024-11-04 [1] CRAN (R 4.3.1)
##  rstudioapi    0.17.1   2024-10-22 [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.1    2024-12-06 [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)
##  vctrs         0.6.5    2023-12-01 [1] CRAN (R 4.3.1)
##  withr         3.0.2    2024-10-28 [1] CRAN (R 4.3.1)
##  xfun          0.50     2025-01-07 [1] CRAN (R 4.3.1)
##  yaml          2.3.10   2024-07-26 [1] CRAN (R 4.3.1)
## 
##  [1] /data/Projects/package_dev/mrgmisc/renv/library/R-4.3/x86_64-pc-linux-gnu
##  [2] /tmp/RtmpIr20U5/renv-system-library
##  [3] /tmp/RtmpJeN9Na/renv-system-library
## 
## ──────────────────────────────────────────────────────────────────────────────