Skip to contents
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.5.1 (2025-06-13)
##  os       Ubuntu 24.04.3 LTS
##  system   x86_64, linux-gnu
##  ui       X11
##  language en
##  collate  C.UTF-8
##  ctype    C.UTF-8
##  tz       Etc/UTC
##  date     2026-02-04
##  pandoc   3.6.3 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/x86_64/ (via rmarkdown)
##  quarto   1.7.31 @ /usr/local/bin/quarto
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  ! package      * version date (UTC) lib source
##  P bslib          0.9.0   2025-01-30 [?] PRISM (R 4.5.0)
##  P cachem         1.1.0   2024-05-16 [?] PRISM (R 4.5.0)
##  P cli            3.6.5   2025-04-23 [?] PRISM (R 4.5.0)
##  P desc           1.4.3   2023-12-10 [?] PRISM (R 4.5.0)
##  P digest         0.6.39  2025-11-19 [?] PRISM (R 4.5.0)
##  P dplyr        * 1.1.4   2023-11-17 [?] PRISM (R 4.5.0)
##  P evaluate       1.0.5   2025-08-27 [?] PRISM (R 4.5.0)
##  P farver         2.1.2   2024-05-13 [?] PRISM (R 4.5.0)
##  P fastmap        1.2.0   2024-05-15 [?] PRISM (R 4.5.0)
##  P fs             1.6.6   2025-04-12 [?] PRISM (R 4.5.0)
##  P generics       0.1.4   2025-05-09 [?] PRISM (R 4.5.0)
##  P ggplot2      * 4.0.1   2025-11-14 [?] PRISM (R 4.5.0)
##  P glue           1.8.0   2024-09-30 [?] PRISM (R 4.5.0)
##  P gtable         0.3.6   2024-10-25 [?] PRISM (R 4.5.0)
##  P htmltools      0.5.9   2025-12-04 [?] PRISM (R 4.5.0)
##  P jquerylib      0.1.4   2021-04-26 [?] PRISM (R 4.5.0)
##  P jsonlite       2.0.0   2025-03-27 [?] PRISM (R 4.5.0)
##  P knitr          1.51    2025-12-20 [?] PRISM (R 4.5.0)
##  P labeling       0.4.3   2023-08-29 [?] PRISM (R 4.5.0)
##  P lifecycle      1.0.5   2026-02-03 [?] local
##  P magrittr       2.0.4   2025-09-12 [?] PRISM (R 4.5.0)
##    mrgmisc      * 0.3.0   2026-02-04 [1] local
##  P pillar         1.11.1  2025-09-17 [?] PRISM (R 4.5.0)
##  P pkgconfig      2.0.3   2019-09-22 [?] PRISM (R 4.5.0)
##  P pkgdown        2.2.0   2025-11-06 [?] PRISM (R 4.5.0)
##  P purrr        * 1.2.1   2026-02-03 [?] local
##  P R6             2.6.1   2025-02-15 [?] PRISM (R 4.5.0)
##  P ragg           1.5.0   2025-09-02 [?] PRISM (R 4.5.0)
##  P RColorBrewer   1.1-3   2022-04-03 [?] PRISM (R 4.5.0)
##  P Rcpp           1.1.1   2026-01-10 [?] PRISM (R 4.5.0)
##  P rlang          1.1.7   2026-02-03 [?] local
##  P rmarkdown      2.30    2025-09-28 [?] PRISM (R 4.5.0)
##  P S7             0.2.1   2025-11-14 [?] PRISM (R 4.5.0)
##  P sass           0.4.10  2025-04-11 [?] PRISM (R 4.5.0)
##  P scales         1.4.0   2025-04-24 [?] PRISM (R 4.5.0)
##  P sessioninfo    1.2.3   2025-02-05 [?] PRISM (R 4.5.0)
##  P systemfonts    1.3.1   2025-10-01 [?] PRISM (R 4.5.0)
##  P textshaping    1.0.4   2025-10-10 [?] PRISM (R 4.5.0)
##  P tibble         3.3.1   2026-02-03 [?] local
##  P tidyselect     1.2.1   2024-03-11 [?] PRISM (R 4.5.0)
##  P vctrs          0.6.5   2023-12-01 [?] PRISM (R 4.5.0)
##  P withr          3.0.2   2024-10-28 [?] PRISM (R 4.5.0)
##  P xfun           0.55    2025-12-16 [?] PRISM (R 4.5.0)
##  P yaml           2.3.12  2025-12-10 [?] PRISM (R 4.5.0)
## 
##  [1] /tmp/RtmpnAnMyw/temp_libpath662e13ad81c06
##  [2] /data/user-homes/andrew/Projects/prism-pkgdocs-build/installed-pkgs/2026-01-14/mrgmisc_0.3.0_lib
##  [3] /opt/R/4.5.1/lib/R/library
## 
##  * ── Packages attached to the search path.
##  P ── Loaded and on-disk path mismatch.
## 
## ──────────────────────────────────────────────────────────────────────────────