|
| 1 | +#' Creating an ESD plot per group |
| 2 | +#' |
| 3 | +#' @param df Dataset |
| 4 | +#' @param es Column name of effect sizes |
| 5 | +#' @param es_type A string describing the type of effect size used (e.g., |
| 6 | +#' "Cohen's d") |
| 7 | +#' @param grouping_var Column name of grouping variable |
| 8 | +#' @param method Defaults to FALSE, but can be 'quads' or 'thirds' |
| 9 | +#' @param mean Defaults to FALSE, but will insert a ggplot geom_vline element |
| 10 | +#' that corresponds to the mean effect size |
| 11 | +#' @param pop_es A numeric argument that corresponds to the population ES of |
| 12 | +#' interest. This will split the histogram into two parts around the inputted |
| 13 | +#' value. |
| 14 | +#' @param bin_width Numeric argument that corresponds to the bin width for the |
| 15 | +#' histogram. Defaults to 0.1 |
| 16 | +#' |
| 17 | +#' @return A ggplot element |
| 18 | +#' @export |
| 19 | +#' |
| 20 | +#' @examples |
| 21 | +esd_plot_group <- function(df, es, es_type, grouping_var, method = FALSE, mean = FALSE, pop_es = NULL, bin_width = 0.1) { |
| 22 | + es_col <- df[, deparse(substitute(es))] |
| 23 | + |
| 24 | + dat_b <- df %>% |
| 25 | + group_by({{grouping_var}}) %>% |
| 26 | + mutate(mean = mean({{es}}), |
| 27 | + q16 = quantile({{es}}, prob = 0.1665), |
| 28 | + q25 = quantile({{es}}, prob = 0.25), |
| 29 | + q50 = quantile({{es}}, prob = 0.50), |
| 30 | + q75 = quantile({{es}}, prob = 0.75), |
| 31 | + q83 = quantile({{es}}, prob = 0.8335),) %>% |
| 32 | + ungroup() |
| 33 | + q16_label <- "16.65th" |
| 34 | + q25_label <- "25th" |
| 35 | + q50_label <- "50th" |
| 36 | + q75_label <- "75th" |
| 37 | + q83_label <- "83.35th" |
| 38 | + |
| 39 | + if (missing(pop_es)){ |
| 40 | + plot <- ggplot(data = dat_b)+ |
| 41 | + geom_histogram(aes({{es}}), fill = "#355C7D", binwidth = bin_width)+ |
| 42 | + scale_x_continuous(breaks = seq(0, 5, 0.5)) + |
| 43 | + labs(x = es_type, y = "Frequency")+ |
| 44 | + theme_minimal() + |
| 45 | + facet_grid(vars({{grouping_var}}), |
| 46 | + switch = "y")+ |
| 47 | + theme(axis.text = element_text(size=12), |
| 48 | + axis.title = element_text(size=20), |
| 49 | + strip.text.y.left = element_text(angle = 0, |
| 50 | + size = 12), |
| 51 | + legend.position = "bottom") |
| 52 | + } else { |
| 53 | + rank <- length(es_col[es_col <= pop_es])/length(es_col) * 100 |
| 54 | + rank_rev <- 100 - rank |
| 55 | + |
| 56 | + rank_perc <- sprintf("%.2f%%", rank) |
| 57 | + rank_rev_perc <- sprintf("%.2f%%", rank_rev) |
| 58 | + |
| 59 | + plot <- ggplot(data = dat_b) + |
| 60 | + geom_histogram(aes(es_col, fill = stat(x) > pop_es), binwidth = bin_width) + |
| 61 | + scale_fill_manual(name = sprintf("ES < or > %.2f", pop_es), |
| 62 | + labels = c(rank_perc, rank_rev_perc), |
| 63 | + values = c("#EEE0CB", "#355C7D")) + |
| 64 | + labs(x = es_type, y = "Frequency")+ |
| 65 | + theme_minimal()+ |
| 66 | + facet_grid(vars({{grouping_var}}), |
| 67 | + switch = "y")+ |
| 68 | + theme(axis.text = element_text(size=12), |
| 69 | + axis.title = element_text(size=20), |
| 70 | + strip.text.y.left = element_text(angle = 0, |
| 71 | + size = 12), |
| 72 | + legend.position = "bottom") |
| 73 | + } |
| 74 | + if (!isFALSE(method)) { |
| 75 | + if (method == "quads") { |
| 76 | + plot <- plot + |
| 77 | + geom_vline(aes(xintercept = q25, color = "q25"), linetype = "dashed", size = 1) + |
| 78 | + geom_vline(aes(xintercept = q50, color = "q50"), linetype = "dashed", size = 1) + |
| 79 | + geom_vline(aes(xintercept = q75, color = "q75"), linetype = "dashed", size = 1) + |
| 80 | + scale_color_manual(name = "Percentiles", |
| 81 | + values = c(q25 = "#F8B195", |
| 82 | + q50 = "#F67280", |
| 83 | + q75 = "#C06C84", |
| 84 | + q4 = "#7DAA92"), |
| 85 | + labels = c(q25_label, q50_label, q75_label, "Mean")) |
| 86 | + } else if (method == "thirds") { |
| 87 | + plot <- plot+ |
| 88 | + geom_vline(aes(xintercept = q16, color = "q16"), linetype = "dashed", size = 1) + |
| 89 | + geom_vline(aes(xintercept = q50, color = "q50"), linetype = "dashed", size = 1) + |
| 90 | + geom_vline(aes(xintercept = q83, color = "q83"), linetype = "dashed", size = 1) + |
| 91 | + scale_color_manual(name = "Percentiles", |
| 92 | + values = c(q16 = "#F8B195", |
| 93 | + q50 = "#F67280", |
| 94 | + q83 = "#C06C84", |
| 95 | + q4 = "#7DAA92"), |
| 96 | + labels = c(q16_label, q50_label, q83_label, "Mean")) |
| 97 | + } else { |
| 98 | + return("Please enter a valid method") |
| 99 | + } |
| 100 | + } else if (isFALSE(method)) { |
| 101 | + plot <- plot |
| 102 | + } else { |
| 103 | + return("Please enter a valid method") |
| 104 | + } |
| 105 | + if (mean == TRUE) { |
| 106 | + plot <- plot + |
| 107 | + geom_vline(aes(xintercept = mean, color = "q4"), linetype = "dotted", size = 1) |
| 108 | + } else { |
| 109 | + plot <- plot |
| 110 | + } |
| 111 | + return(plot) |
| 112 | +} |
0 commit comments