library(mrgmisc) # for binning and other functions
library(ggplot2) #plotting
library(purrr) # dataset to handle lists
suppressMessages(suppressWarnings(library(dplyr)))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
## $`1`

##
## $`2`

##
## $`3`

##
## $`4`

##
## $`5`

sessioninfo::session_info()## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.6.1 (2026-06-24)
## os Ubuntu 24.04.4 LTS
## system x86_64, linux-gnu
## ui X11
## language en
## collate C.UTF-8
## ctype C.UTF-8
## tz UTC
## date 2026-08-28
## pandoc 3.1.3 @ /usr/bin/ (via rmarkdown)
## quarto 1.10.18 @ /usr/local/bin/quarto
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## bslib 0.12.0 2026-08-04 [2] prism (R 4.6.1)
## cachem 1.1.0 2024-05-16 [2] PRISM (R 4.6.0)
## cli 3.6.6 2026-04-09 [2] PRISM (R 4.6.0)
## desc 1.4.3 2023-12-10 [2] PRISM (R 4.6.0)
## digest 0.6.39 2025-11-19 [2] PRISM (R 4.6.0)
## dplyr * 1.2.1 2026-04-03 [2] PRISM (R 4.6.0)
## evaluate 1.0.5 2025-08-27 [2] PRISM (R 4.6.0)
## farver 2.1.2 2024-05-13 [2] PRISM (R 4.6.0)
## fastmap 1.2.0 2024-05-15 [2] PRISM (R 4.6.0)
## fs 2.1.0 2026-04-18 [2] prism (R 4.6.0)
## generics 0.1.4 2025-05-09 [2] PRISM (R 4.6.0)
## ggplot2 * 4.0.3 2026-04-22 [2] prism (R 4.6.0)
## glue 1.8.1 2026-04-17 [2] prism (R 4.6.0)
## gtable 0.3.6 2024-10-25 [2] PRISM (R 4.6.0)
## htmltools 0.5.9 2025-12-04 [2] PRISM (R 4.6.0)
## htmlwidgets 1.6.4 2023-12-06 [2] PRISM (R 4.6.0)
## jquerylib 0.1.4 2021-04-26 [2] PRISM (R 4.6.0)
## jsonlite 2.0.0 2025-03-27 [2] PRISM (R 4.6.0)
## knitr 1.51 2025-12-20 [2] PRISM (R 4.6.0)
## labeling 0.4.3 2023-08-29 [2] PRISM (R 4.6.0)
## lifecycle 1.0.5 2026-01-08 [2] PRISM (R 4.6.0)
## magrittr 2.0.5 2026-04-04 [2] PRISM (R 4.6.0)
## mrgmisc * 0.4.0 2026-08-28 [1] local
## otel 0.2.0 2025-08-29 [2] PRISM (R 4.6.0)
## pillar 1.11.1 2025-09-17 [2] PRISM (R 4.6.0)
## pkgconfig 2.0.3 2019-09-22 [2] PRISM (R 4.6.0)
## pkgdown 2.2.1 2026-07-07 [2] prism (R 4.6.1)
## purrr * 1.2.2 2026-04-10 [2] PRISM (R 4.6.0)
## R6 2.6.1 2025-02-15 [2] PRISM (R 4.6.0)
## ragg 1.5.2 2026-03-23 [2] PRISM (R 4.6.0)
## RColorBrewer 1.1-3 2022-04-03 [2] PRISM (R 4.6.0)
## Rcpp 1.1.2 2026-07-05 [2] prism (R 4.6.1)
## rlang 1.3.0 2026-07-05 [2] prism (R 4.6.1)
## rmarkdown 2.31 2026-03-26 [2] PRISM (R 4.6.0)
## S7 0.2.2 2026-04-22 [2] prism (R 4.6.0)
## sass 0.4.10 2025-04-11 [2] PRISM (R 4.6.0)
## scales 1.4.0 2025-04-24 [2] PRISM (R 4.6.0)
## sessioninfo 1.2.4 2026-06-04 [2] prism (R 4.6.0)
## systemfonts 1.3.2 2026-03-05 [2] PRISM (R 4.6.0)
## textshaping 1.0.5 2026-03-06 [2] PRISM (R 4.6.0)
## tibble 3.3.1 2026-01-11 [2] PRISM (R 4.6.0)
## tidyselect 1.2.1 2024-03-11 [2] PRISM (R 4.6.0)
## vctrs 0.7.3 2026-04-11 [2] PRISM (R 4.6.0)
## withr 3.0.3 2026-06-19 [2] prism (R 4.6.1)
## xfun 0.60 2026-07-09 [2] prism (R 4.6.1)
## yaml 2.3.12 2025-12-10 [2] PRISM (R 4.6.0)
##
## [1] /tmp/RtmpGsTunx/temp_libpathf9ea51061586
## [2] /tmp/.tmpZLIpOK/docs/library
## [3] /opt/R/4.6.1/lib/R/library
## * ── Packages attached to the search path.
##
## ──────────────────────────────────────────────────────────────────────────────