library(PKPDmisc) # 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
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`
As you can notice from the vignette, there is still a code-based output separating each subplot, which is not ideal for reporting. As such, there is a special function print_plots
in PKPDmisc that will clean up the output slightly. It also automatically restarts the page numbering if knitting to PDF to page 1, making it each to extract only the pages related to plots to embed cleanly in other reports.
To use print_plots
we want to save the ggplot output (rather than let it be printed directly) and give that to print plots.
## Session info -------------------------------------------------------------
## setting value
## version R version 3.4.3 (2017-11-30)
## system x86_64, darwin17.4.0
## ui unknown
## language (EN)
## collate en_US.UTF-8
## tz America/New_York
## date 2018-05-15
## Packages -----------------------------------------------------------------
## package * version date source
## assertthat 0.2.0 2017-04-11 CRAN (R 3.4.2)
## backports 1.1.2 2017-12-13 CRAN (R 3.4.2)
## base * 3.4.3 2018-03-12 local
## bindr 0.1.1 2018-03-13 CRAN (R 3.4.3)
## bindrcpp * 0.2.2 2018-03-29 CRAN (R 3.4.3)
## colorspace 1.3-2 2016-12-14 CRAN (R 3.4.2)
## commonmark 1.4 2017-09-01 CRAN (R 3.4.2)
## compiler 3.4.3 2018-03-12 local
## crayon 1.3.4 2017-09-16 CRAN (R 3.4.2)
## datasets * 3.4.3 2018-03-12 local
## desc 1.1.1.9002 2018-02-12 Github (r-lib/desc@9db1377)
## devtools 1.13.3 2017-08-02 CRAN (R 3.4.2)
## digest 0.6.15 2018-01-28 CRAN (R 3.4.2)
## dplyr * 0.7.4 2017-09-28 CRAN (R 3.4.2)
## evaluate 0.10.1 2017-06-24 CRAN (R 3.4.2)
## fs 1.2.2 2018-03-21 CRAN (R 3.4.3)
## ggplot2 * 2.2.1 2016-12-30 CRAN (R 3.4.2)
## glue 1.2.0.9000 2018-03-01 Github (tidyverse/glue@9d96cbf)
## graphics * 3.4.3 2018-03-12 local
## grDevices * 3.4.3 2018-03-12 local
## grid 3.4.3 2018-03-12 local
## gtable 0.2.0 2016-02-26 CRAN (R 3.4.2)
## htmltools 0.3.6 2017-04-28 CRAN (R 3.4.2)
## knitr 1.20 2018-02-20 CRAN (R 3.4.2)
## labeling 0.3 2014-08-23 CRAN (R 3.4.2)
## lazyeval 0.2.1 2017-10-29 cran (@0.2.1)
## magrittr 1.5 2014-11-22 CRAN (R 3.4.2)
## MASS 7.3-47 2017-02-26 CRAN (R 3.4.3)
## memoise 1.1.0 2017-10-20 Github (hadley/memoise@d63ae9c)
## methods * 3.4.3 2018-03-12 local
## munsell 0.4.3 2016-02-13 CRAN (R 3.4.2)
## pillar 1.2.2 2018-04-26 CRAN (R 3.4.3)
## pkgconfig 2.0.1 2017-03-21 CRAN (R 3.4.2)
## pkgdown 1.0.0 2018-05-03 CRAN (R 3.4.3)
## PKPDmisc * 2.1.1 2017-12-17 CRAN (R 3.4.3)
## plyr 1.8.4 2016-06-08 CRAN (R 3.4.2)
## purrr * 0.2.4 2017-10-18 CRAN (R 3.4.2)
## R6 2.2.2 2017-06-17 CRAN (R 3.4.2)
## Rcpp 0.12.16 2018-03-13 CRAN (R 3.4.3)
## rlang 0.2.0 2018-02-20 CRAN (R 3.4.3)
## rmarkdown 1.9 2018-03-01 CRAN (R 3.4.3)
## roxygen2 6.0.1.9000 2018-03-01 Github (klutometis/roxygen@be3ef30)
## rprojroot 1.3-2 2018-01-03 CRAN (R 3.4.2)
## rstudioapi 0.7 2017-09-07 CRAN (R 3.4.2)
## scales 0.5.0 2017-08-24 CRAN (R 3.4.2)
## stats * 3.4.3 2018-03-12 local
## stringi 1.2.2 2018-05-02 CRAN (R 3.4.3)
## stringr 1.3.0 2018-02-19 CRAN (R 3.4.2)
## tibble 1.4.2 2018-01-22 CRAN (R 3.4.2)
## tools 3.4.3 2018-03-12 local
## utils * 3.4.3 2018-03-12 local
## withr 2.1.2 2018-03-15 CRAN (R 3.4.3)
## xml2 1.2.0 2018-01-24 CRAN (R 3.4.2)
## yaml 2.1.16 2017-12-12 CRAN (R 3.4.2)