-
Notifications
You must be signed in to change notification settings - Fork 190
/
ggpiestats.R
264 lines (234 loc) · 8.75 KB
/
ggpiestats.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
#' @title Pie charts with statistical tests
#' @name ggpiestats
#'
#' @description
#'
#'
#'
#' Pie charts for categorical data with statistical details included in the plot
#' as a subtitle.
#'
#' @param x The variable to use as the **rows** in the contingency table. Please
#' note that if there are empty factor levels in your variable, they will be
#' dropped.
#' @param y The variable to use as the **columns** in the contingency table.
#' Please note that if there are empty factor levels in your variable, they
#' will be dropped. Default is `NULL`. If `NULL`, one-sample proportion test
#' (a goodness of fit test) will be run for the `x` variable. Otherwise an
#' appropriate association test will be run. This argument can not be `NULL`
#' for `ggbarstats` function.
#' @param proportion.test Decides whether proportion test for `x` variable is to
#' be carried out for each level of `y` (Default: `TRUE`). In `ggbarstats`,
#' only *p*-values from this test will be displayed.
#' @param perc.k Numeric that decides number of decimal places for percentage
#' labels (Default: `0`).
#' @param label Character decides what information needs to be displayed
#' on the label in each pie slice. Possible options are `"percentage"`
#' (default), `"counts"`, `"both"`.
#' @param label.args Additional aesthetic arguments that will be passed to
#' `geom_label`.
#' @param label.repel Whether labels should be repelled using `ggrepel` package.
#' This can be helpful in case the labels are overlapping.
#' @param legend.title Title text for the legend.
#' @inheritParams ggbetweenstats
#' @inheritParams statsExpressions::contingency_table
#' @inheritParams theme_ggstatsplot
#' @inheritParams gghistostats
#'
#' @seealso \code{\link{grouped_ggpiestats}}, \code{\link{ggbarstats}},
#' \code{\link{grouped_ggbarstats}}
#'
#' @import ggplot2
#'
#' @importFrom dplyr select mutate vars pull across everything
#' @importFrom rlang !! enquo as_name ensym !!! exec
#' @importFrom ggrepel geom_label_repel
#' @importFrom tidyr uncount drop_na
#' @importFrom statsExpressions contingency_table
#'
#' @details For more details, see:
#' \url{https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggpiestats.html}
#'
#' @examples
#' \donttest{
#' # for reproducibility
#' set.seed(123)
#'
#' # one sample goodness of fit proportion test
#' ggstatsplot::ggpiestats(ggplot2::msleep, vore)
#'
#' # association test (or contingency table analysis)
#' ggstatsplot::ggpiestats(
#' data = mtcars,
#' x = vs,
#' y = cyl
#' )
#' }
#' @export
# defining the function
ggpiestats <- function(data,
x,
y = NULL,
counts = NULL,
type = "parametric",
paired = FALSE,
results.subtitle = TRUE,
label = "percentage",
label.args = list(direction = "both"),
label.repel = FALSE,
k = 2L,
proportion.test = TRUE,
perc.k = 0,
bf.message = TRUE,
ratio = NULL,
conf.level = 0.95,
sampling.plan = "indepMulti",
fixed.margin = "rows",
prior.concentration = 1,
title = NULL,
subtitle = NULL,
caption = NULL,
legend.title = NULL,
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
x <- rlang::ensym(x)
y <- if (!rlang::quo_is_null(rlang::enquo(y))) rlang::ensym(y)
# one-way or two-way table?
test <- ifelse(!rlang::quo_is_null(rlang::enquo(y)), "two.way", "one.way")
# =============================== dataframe ================================
# creating a dataframe
data %<>%
dplyr::select({{ x }}, {{ y }}, .counts = {{ counts }}) %>%
tidyr::drop_na(.)
# untable the dataframe based on the count for each observation
if (".counts" %in% names(data)) data %<>% tidyr::uncount(weights = .counts)
# x and y need to be a factor; also drop the unused levels of the factors
data %<>% dplyr::mutate(dplyr::across(dplyr::everything(), ~ droplevels(as.factor(.x))))
# x
x_levels <- nlevels(data %>% dplyr::pull({{ x }}))[[1]]
# y
if (test == "two.way") {
y_levels <- nlevels(data %>% dplyr::pull({{ y }}))[[1]]
# TO DO: until one-way table is supported by `BayesFactor`
if (y_levels == 1L) bf.message <- FALSE
} else {
y_levels <- 0L
}
# faceting is happening only if both vars have more than one levels
facet <- ifelse(y_levels > 1L, TRUE, FALSE)
if ((x_levels == 1L && isTRUE(facet)) || type == "bayes") proportion.test <- FALSE
# -------------------------- statistical analysis --------------------------
# if subtitle with results is to be displayed
if (isTRUE(results.subtitle)) {
subtitle_df <- tryCatch(
expr = statsExpressions::contingency_table(
data = data,
x = {{ x }},
y = {{ y }},
type = type,
k = k,
paired = paired,
ratio = ratio,
conf.level = conf.level
),
error = function(e) NULL
)
if (!is.null(subtitle_df)) subtitle <- subtitle_df$expression[[1]]
# preparing Bayes Factor caption
if (type != "bayes" && isTRUE(bf.message) && isFALSE(paired)) {
caption_df <- tryCatch(
expr = statsExpressions::contingency_table(
data = data,
x = {{ x }},
y = {{ y }},
type = "bayes",
k = k,
top.text = caption,
sampling.plan = sampling.plan,
fixed.margin = fixed.margin,
prior.concentration = prior.concentration
),
error = function(e) NULL
)
if (!is.null(caption_df)) caption <- caption_df$expression[[1]]
}
}
# return early if anything other than plot
if (output != "plot") {
return(switch(output,
"caption" = caption,
subtitle
))
}
# =================================== plot =================================
# dataframe with summary labels
df_descriptive <- df_descriptive(data, {{ x }}, {{ y }}, label, perc.k)
# dataframe containing all details needed for prop test
if (test == "two.way") df_proptest <- df_proptest(data, {{ x }}, {{ y }}, k)
# if no. of factor levels is greater than the default palette color count
palette_message(package, palette, min_length = x_levels)
# creating the basic plot
p <- ggplot2::ggplot(df_descriptive, mapping = ggplot2::aes(x = "", y = perc)) +
ggplot2::geom_col(
mapping = ggplot2::aes(fill = {{ x }}),
position = "fill",
color = "black",
width = 1
)
# whether labels need to be repelled
if (isTRUE(label.repel)) .fn <- ggrepel::geom_label_repel
if (isFALSE(label.repel)) .fn <- ggplot2::geom_label
# adding label with percentages and/or counts
suppressWarnings(suppressMessages(p <- p +
rlang::exec(
.fn,
mapping = ggplot2::aes(label = .label, group = {{ x }}),
position = ggplot2::position_fill(vjust = 0.5),
min.segment.length = 0,
fill = "white",
alpha = 1,
!!!label.args
)))
# if facet_wrap *is* happening
if (isTRUE(facet)) p <- p + ggplot2::facet_wrap(facets = dplyr::vars({{ y }}))
# polar coordinates plus formatting
p <- p +
ggplot2::coord_polar(theta = "y") +
ggplot2::scale_y_continuous(breaks = NULL) +
paletteer::scale_fill_paletteer_d(paste0(package, "::", palette), name = "") +
theme_pie(ggtheme, ggstatsplot.layer) +
ggplot2::guides(fill = ggplot2::guide_legend(override.aes = list(color = NA)))
# ================ sample size + proportion test labels =================
# adding labels with proportion tests
if (isTRUE(facet) && isTRUE(proportion.test)) {
p <- p +
rlang::exec(
ggplot2::geom_text,
data = df_proptest,
mapping = ggplot2::aes(label = .label, x = 1.65, y = 0.5),
position = ggplot2::position_fill(vjust = 1),
size = 2.8,
parse = TRUE
)
}
# =========================== putting all together ========================
# preparing the plot
p +
ggplot2::labs(
x = NULL,
y = NULL,
subtitle = subtitle,
title = title,
caption = caption
) +
ggplot2::guides(fill = ggplot2::guide_legend(title = legend.title %||% rlang::as_name(x))) +
ggplot.component
}