-
Notifications
You must be signed in to change notification settings - Fork 190
/
ggwithinstats.R
347 lines (318 loc) · 11.1 KB
/
ggwithinstats.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#' @title Box/Violin plots for group or condition comparisons in
#' within-subjects (or repeated measures) designs.
#' @name ggwithinstats
#'
#' @description
#'
#'
#'
#' A combination of box and violin plots along with raw (unjittered) data points
#' for within-subjects designs with statistical details included in the plot as
#' a subtitle.
#'
#' @note
#' 1. Please note that the function expects that the data is
#' already sorted by subject/repeated measures ID.
#'
#' 2. To carry out Bayesian analysis for ANOVA designs, you will need to install
#' the development version of `BayesFactor` (`0.9.12-4.3`). You can download it
#' by running:
#' `remotes::install_github("richarddmorey/BayesFactor/pkg/BayesFactor")`.
#'
#' @inheritParams ggbetweenstats
#' @param point.path,centrality.path Logical that decides whether individual data
#' points and means, respectively, should be connected using `geom_path`. Both
#' default to `TRUE`. Note that `point.path` argument is relevant only when
#' there are two groups (i.e., in case of a *t*-test). In case of large number
#' of data points, it is advisable to set `point.path = FALSE` as these lines
#' can overwhelm the plot.
#' @param centrality.path.args,point.path.args A list of additional aesthetic
#' arguments passed on to `geom_path` connecting raw data points and mean
#' points.
#' @inheritParams statsExpressions::oneway_anova
#'
#' @seealso \code{\link{grouped_ggbetweenstats}}, \code{\link{ggbetweenstats}},
#' \code{\link{grouped_ggwithinstats}}
#'
#' @importFrom rlang exec !! enquo := !!! exec
#' @importFrom pairwiseComparisons pairwise_comparisons pairwise_caption
#' @importFrom dplyr select mutate row_number group_by ungroup anti_join
#'
#' @references
#' \url{https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggwithinstats.html}
#'
#' @examples
#' \donttest{
#' # setup
#' set.seed(123)
#' library(ggstatsplot)
#'
#' # two groups (*t*-test)
#' ggwithinstats(
#' data = VR_dilemma,
#' x = modality,
#' y = score,
#' xlab = "Presentation modality",
#' ylab = "Proportion of utilitarian decisions"
#' )
#'
#' # more than two groups (anova)
#' library(WRS2)
#'
#' ggstatsplot::ggwithinstats(
#' data = WineTasting,
#' x = Wine,
#' y = Taste,
#' type = "np", # non-parametric test
#' pairwise.comparisons = TRUE,
#' outlier.tagging = TRUE,
#' outlier.label = Taster
#' )
#' }
#' @export
# defining the function
ggwithinstats <- function(data,
x,
y,
type = "parametric",
pairwise.comparisons = TRUE,
pairwise.display = "significant",
p.adjust.method = "holm",
effsize.type = "unbiased",
bf.prior = 0.707,
bf.message = TRUE,
results.subtitle = TRUE,
xlab = NULL,
ylab = NULL,
caption = NULL,
title = NULL,
subtitle = NULL,
k = 2L,
conf.level = 0.95,
nboot = 100L,
tr = 0.2,
centrality.plotting = TRUE,
centrality.type = type,
centrality.point.args = list(size = 5, color = "darkred"),
centrality.label.args = list(size = 3, nudge_x = 0.4, segment.linetype = 4),
centrality.path = TRUE,
centrality.path.args = list(color = "red", size = 1, alpha = 0.5),
point.path = TRUE,
point.path.args = list(alpha = 0.5, linetype = "dashed"),
outlier.tagging = FALSE,
outlier.label = NULL,
outlier.coef = 1.5,
outlier.label.args = list(size = 3),
violin.args = list(width = 0.5, alpha = 0.2),
ggsignif.args = list(textsize = 3, tip_length = 0.01),
ggtheme = ggplot2::theme_bw(),
ggstatsplot.layer = TRUE,
package = "RColorBrewer",
palette = "Dark2",
ggplot.component = NULL,
output = "plot",
...) {
# convert entered stats type to a standard notation
type <- ipmisc::stats_type_switch(type)
# ensure the variables work quoted or unquoted
c(x, y) %<-% c(rlang::ensym(x), rlang::ensym(y))
outlier.label <- if (!rlang::quo_is_null(rlang::enquo(outlier.label))) {
rlang::ensym(outlier.label)
}
# --------------------------------- data -----------------------------------
# creating a dataframe
data %<>%
dplyr::select({{ x }}, {{ y }}, outlier.label = {{ outlier.label }}) %>%
dplyr::mutate({{ x }} := droplevels(as.factor({{ x }}))) %>%
dplyr::group_by({{ x }}) %>%
dplyr::mutate(.rowid = dplyr::row_number()) %>%
dplyr::ungroup(.) %>%
dplyr::anti_join(x = ., y = dplyr::filter(., is.na({{ y }})), by = ".rowid")
# if `outlier.label` column is not present, just use the values from `y` column
if (!"outlier.label" %in% names(data)) data %<>% dplyr::mutate(outlier.label = {{ y }})
# add a logical column indicating whether a point is or is not an outlier
data %<>%
outlier_df(
x = {{ x }},
y = {{ y }},
outlier.coef = outlier.coef,
outlier.label = outlier.label
)
# --------------------- subtitle/caption preparation ------------------------
# figure out which test to run based on the no. of levels of the independent variable
test <- ifelse(nlevels(data %>% dplyr::pull({{ x }}))[[1]] < 3, "t", "anova")
# these analyses do require latest Github version of Bayes Factor
if (type %in% c("parametric", "bayes") && test == "anova" &&
utils::packageVersion("BayesFactor") < "0.9.12-4.3") {
if (type == "parametric") bf.message <- FALSE else results.subtitle <- FALSE
}
if (isTRUE(results.subtitle)) {
# preparing the bayes factor message
if (type == "parametric" && isTRUE(bf.message)) {
caption_df <- tryCatch(
function_switch(
test = test,
# arguments relevant for expression helper functions
data = data,
x = rlang::as_string(x),
y = rlang::as_string(y),
type = "bayes",
bf.prior = bf.prior,
top.text = caption,
paired = TRUE,
k = k
),
error = function(e) NULL
)
caption <- if (!is.null(caption_df)) caption_df$expression[[1]]
}
# extracting the subtitle using the switch function
subtitle_df <- tryCatch(
function_switch(
test = test,
# arguments relevant for expression helper functions
data = data,
x = rlang::as_string(x),
y = rlang::as_string(y),
paired = TRUE,
type = type,
effsize.type = effsize.type,
var.equal = TRUE,
bf.prior = bf.prior,
tr = tr,
nboot = nboot,
conf.level = conf.level,
k = k
),
error = function(e) NULL
)
subtitle <- if (!is.null(subtitle_df)) subtitle_df$expression[[1]]
}
# return early if anything other than plot
if (output != "plot") {
return(switch(output,
"caption" = caption,
subtitle
))
}
# --------------------------------- basic plot ------------------------------
# plot
plot <-
ggplot2::ggplot(
data = data,
mapping = ggplot2::aes(x = {{ x }}, y = {{ y }}, group = .rowid)
) +
ggplot2::geom_point(
alpha = 0.5,
size = 3,
ggplot2::aes(color = {{ x }})
) +
ggplot2::geom_boxplot(
mapping = ggplot2::aes(x = {{ x }}, y = {{ y }}),
inherit.aes = FALSE,
fill = "white",
width = 0.2,
alpha = 0.5
) +
rlang::exec(
.fn = ggplot2::geom_violin,
mapping = ggplot2::aes(x = {{ x }}, y = {{ y }}),
inherit.aes = FALSE,
fill = "white",
!!!violin.args
)
# add a connecting path only if there are only two groups
if (test != "anova" && isTRUE(point.path)) {
plot <- plot +
rlang::exec(
.fn = ggplot2::geom_path,
!!!point.path.args
)
}
# ---------------------------- outlier labeling -----------------------------
# If `outlier.label` is not provided, outlier labels will just be values of
# the `y` vector. If the outlier tag has been provided, just use the dataframe
# already created.
if (isTRUE(outlier.tagging)) {
# applying the labels to tagged outliers with `ggrepel`
plot <- plot +
rlang::exec(
.fn = ggrepel::geom_label_repel,
data = dplyr::filter(.data = data, isanoutlier),
mapping = ggplot2::aes(x = {{ x }}, y = {{ y }}, label = outlier.label),
show.legend = FALSE,
min.segment.length = 0,
inherit.aes = FALSE,
!!!outlier.label.args
)
}
# ---------------- centrality tagging -------------------------------------
# add labels for mean values
if (isTRUE(centrality.plotting)) {
plot <-
centrality_ggrepel(
plot = plot,
data = data,
x = {{ x }},
y = {{ y }},
k = k,
type = ipmisc::stats_type_switch(centrality.type),
tr = tr,
centrality.path = centrality.path,
centrality.path.args = centrality.path.args,
centrality.point.args = centrality.point.args,
centrality.label.args = centrality.label.args
)
}
# ggsignif labels -----------------------------------------------------------
if (isTRUE(pairwise.comparisons) && test == "anova") {
# creating dataframe with pairwise comparison results
df_pairwise <-
pairwiseComparisons::pairwise_comparisons(
data = data,
x = {{ x }},
y = {{ y }},
type = type,
tr = tr,
paired = TRUE,
p.adjust.method = p.adjust.method,
k = k
)
# adding the layer for pairwise comparisons
plot <-
ggsignif_adder(
plot = plot,
df_pairwise = df_pairwise,
data = data,
x = {{ x }},
y = {{ y }},
pairwise.display = pairwise.display,
ggsignif.args = ggsignif.args
)
# preparing the caption for pairwise comparisons test
if (type != "bayes") {
caption <-
pairwiseComparisons::pairwise_caption(
caption,
unique(df_pairwise$test.details),
pairwise.display
)
}
}
# ------------------------ annotations and themes -------------------------
# specifying annotations and other aesthetic aspects for the plot
aesthetic_addon(
plot = plot,
x = data %>% dplyr::pull({{ x }}),
xlab = xlab %||% rlang::as_name(x),
ylab = ylab %||% rlang::as_name(y),
title = title,
subtitle = subtitle,
caption = caption,
ggtheme = ggtheme,
ggstatsplot.layer = ggstatsplot.layer,
package = package,
palette = palette,
ggplot.component = ggplot.component
)
}