-
Notifications
You must be signed in to change notification settings - Fork 3
/
plotCDFlist.R
56 lines (54 loc) · 1.66 KB
/
plotCDFlist.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#' Plot CDFs from Numeric List
#'
#' [plotCDFlist()] creates a series of cumulative distribution
#' function (CDF) plots from a numeric list object.
#'
#' @rdname plotCDF
#' @param .data A *named* list of numeric vectors (or a data.frame)
#' containing the data to plot.
#' @param label Character. A label for the grouping
#' variable, i.e. what the entries of the list represent.
#' @param ... Additional arguments passed to either
#' [plotCDFbyGroup()] or [plotPDFbyGroup()], primarily one of:
#' \itemize{
#' \item `x.lab`
#' \item `lty`
#' \item `cols`
#' \item `xlim`
#' \item `fill`
#' \item `ablines`
#' }
#' @author Stu Field
#' @examples
#' # `plotCDFlist()`
#' x <- withr::with_seed(101,
#' mapply(mean = 3:5, n = c(10, 100, 1000), FUN = rnorm) |>
#' setNames(paste0("Group", 1:3))
#' )
#' lengths(x)
#'
#' sapply(x, mean)
#'
#' # warning: RFU values should all be positive!
#' plotCDFlist(x)
#' plotCDFlist(x, label = "SplitBy")
#' plotCDFlist(x, x.lab = "My x-axis", main = "Variable `x` CDF")
#' medians <- vapply(x, median, 0.0)
#' plotCDFlist(x, ablines = medians, main = "Variable `x` CDF")
#' @importFrom rlang sym !!
#' @importFrom tidyr gather drop_na
#' @export
plotCDFlist <- function(.data, label = "Group", main = "CDF by Group", ...) {
if ( !inherits(.data, "list") ) {
stop(
"Please pass `.data` as a list of numeric vectors.",
call. = FALSE
)
}
label <- sym(label)
lapply(.data, "length<-", max(lengths(.data))) |> # fix for jagged elements
data.frame(check.names = FALSE) |>
gather(key = !!label) |>
drop_na(value) |>
plotCDFbyGroup(apt = "value", group.var = !!label, main = main, ...)
}