-
Notifications
You must be signed in to change notification settings - Fork 7
/
plot.R
53 lines (47 loc) · 1.57 KB
/
plot.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
#' @title Plot bootstrap results
#'
#' @description
#' Generate a density plot with a half-eye plot representing the 68% and 95%
#' percentile intervals from an \code{lmeresamp} object.
#'
#' @param x The lmeresamp object to plot.
#' @param var The estimated parameter to plot, as a string or column number.
#' @param ... not used
#'
#' @rdname plot
#' @export
#' @method plot lmeresamp
#' @importFrom ggplot2 ggplot labs aes
#' @importFrom ggdist stat_halfeye
#' @importFrom tidyr pivot_longer
plot.lmeresamp <- function(x, var, ...){
value <- term <- NULL
if(is.numeric(x$replicates)) {
ggplot2::ggplot(data = NULL, ggplot2::aes(x = x$replicates)) +
ggdist::stat_halfeye(fill = "cadetblue", alpha = 0.5)
} else{
# set default
if(missing(var)){
tidy_reps <- tidyr::pivot_longer(
x$replicates,
cols = dplyr::everything(),
names_to = "term",
values_to = "value"
)
ggplot2::ggplot(tidy_reps, ggplot2::aes(x = value, y = term)) +
ggdist::stat_halfeye(fill = "cadetblue", alpha = 0.5)
} else{
x$replicates <- as.data.frame(x$replicates)
if(is.numeric(var)) var <- colnames(x$replicates)[var]
if(grepl("[()]", var)) var <- paste0("`", var, "`")
# to_plot <- unlist(x$replicates[var])
ggplot2::ggplot(x$replicates, ggplot2::aes_string(x = var)) +
ggdist::stat_halfeye(fill = "cadetblue", alpha = 0.5) +
ggplot2::labs(
title = paste("Distribution of", var),
x = var,
y = "density"
)
}
}
}