Skip to content

Commit 13f0378

Browse files
committedFeb 7, 2023
Update 07/02/2023
1 parent 1b0794e commit 13f0378

File tree

5 files changed

+125
-8
lines changed

5 files changed

+125
-8
lines changed
 

‎DESCRIPTION

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ Roxygen: list(markdown = TRUE)
1010
RoxygenNote: 7.2.1
1111
Imports:
1212
dplyr,
13-
ggplot2
13+
ggplot2,
14+
grid,
15+
gridExtra

‎LICENSE

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
YEAR: 2022
2-
COPYRIGHT HOLDER: fxsizedistr authors
1+
YEAR: 2023
2+
COPYRIGHT HOLDER: ESDist authors

‎LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22

3-
Copyright (c) 2022 ESDist authors
3+
Copyright (c) 2023 ESDist authors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

‎R/esd_plot.R

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
#' @param es Column name of effect sizes
55
#' @param es_type A string describing the type of effect size used (e.g.,
66
#' "Cohen's d")
7-
#' @param method Defaults to 'none', can also be 'thirds' for 16.65th, 50th, and
7+
#' @param method Defaults to FALSE, can also be 'thirds' for 16.65th, 50th, and
88
#' 83.35th percentiles, or 'quads' for 25th, 50th, and 75th percentiles.
9-
#' @param bin_width Sets the bin width for the histogram
10-
#' @param pop_es A numeric argument that corresponds to the population ES of
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 #' @param pop_es A numeric argument that corresponds to the population ES of
1112
#' interest. This will split the histogram into two parts around the inputted
1213
#' value.
14+
#' @param bin_width Numeric argument that corresponds to the bin width for the
15+
#' histogram. Defaults to 0.1
1316
#'
1417
#' @return A ggplot object
1518
#' @export
1619
#'
1720
#' @examples esd_plot(df, es, es_type = "Cohen's d", method = "thirds", pop)
18-
esd_plot <- function(df, es, es_type, pop_es = NULL, method = FALSE, mean = FALSE, bin_width = 0.1) {
21+
esd_plot <- function(df, es, es_type, method = FALSE, mean = FALSE, pop_es = NULL, bin_width = 0.1) {
1922
es_col <- df[, deparse(substitute(es))]
2023

2124
if (missing(pop_es)){

‎R/esd_plot_group.R

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)
Please sign in to comment.