-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathadd_summary.R
291 lines (252 loc) · 9.84 KB
/
add_summary.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
#' @include utilities.R
NULL
#'Add Summary Statistics onto a ggplot.
#'@description add summary statistics onto a ggplot.
#'@param p a ggplot on which you want to add summary statistics.
#'@param fun a function that is given the complete data and should return a data
#' frame with variables ymin, y, and ymax. Allowed values are one of: "mean",
#' "mean_se", "mean_sd", "mean_ci", "mean_range", "median", "median_iqr",
#' "median_hilow", "median_q1q3", "median_mad", "median_range".
#'@param error.plot plot type used to visualize error. Allowed values are one of
#' \code{c("pointrange", "linerange", "crossbar", "errorbar", "upper_errorbar",
#' "lower_errorbar", "upper_pointrange", "lower_pointrange", "upper_linerange",
#' "lower_linerange")}. Default value is "pointrange".
#'@param color point or outline color.
#'@param fill fill color. Used only whne \code{error.plot = "crossbar"}.
#'@param group grouping variable. Allowed values are 1 (for one group) or a
#' character vector specifying the name of the grouping variable. Used only for
#' adding statistical summary per group.
#'@param width numeric value between 0 and 1 specifying bar or box width.
#' Example width = 0.8. Used only when \code{error.plot} is one of
#' c("crossbar", "errorbar").
#'@param shape point shape. Allowed values can be displayed using the function
#' \code{\link{show_point_shapes}()}.
#'@param size numeric value in [0-1] specifying point and line size.
#'@param linetype line type.
#'@param show.legend logical. Should this layer be included in the legends? NA,
#' the default, includes if any aesthetics are mapped. \code{FALSE} never includes,
#' and TRUE always includes. It can also be a named logical vector to finely
#' select the aesthetics to display.
#'@param data a \code{data.frame} to be displayed. If \code{NULL}, the default,
#' the data is inherited from the plot data as specified in the call to
#' \link[ggplot2]{ggplot}.
#'@param position position adjustment, either as a string, or the result of a
#' call to a position adjustment function. Used to adjust position for multiple
#' groups.
#'@param x a numeric vector.
#'@param ci the percent range of the confidence interval (default is 0.95).
#'@param error.limit allowed values are one of ("both", "lower", "upper",
#' "none") specifying whether to plot the lower and/or the upper limits of
#' error interval.
#'@examples
#'
#'# Basic violin plot
#'p <- ggviolin(ToothGrowth, x = "dose", y = "len", add = "none")
#'p
#'
#'# Add mean_sd
#'add_summary(p, "mean_sd")
#'
#'
#'@describeIn add_summary add summary statistics onto a ggplot.
#'@export
add_summary <- function(p, fun = "mean_se", error.plot = "pointrange",
color = "black", fill = "white", group = 1,
width = NULL, shape = 19, size = 1, linetype = 1,
show.legend = NA, ci = 0.95,
data = NULL, position = position_dodge(0.8))
{
if(is.null(data)) data <- p$data
if(fun == "mean_se") fun <- "mean_se_"
else if(fun == "median_hilow") fun <- "median_hilow_"
allowed.fun <- .summary_functions()
if(!(fun %in% allowed.fun))
stop("Don't support ", fun, ". Possibilities for the argument fun are: ",
.collapse(allowed.fun, sep = ", "))
allowed.error.plot = c("pointrange", "linerange", "crossbar", "errorbar",
"upper_errorbar", "lower_errorbar", "upper_pointrange", "lower_pointrange",
"upper_linerange", "lower_linerange")
if(!(error.plot %in% allowed.error.plot))
stop("Don't support ", error.plot, ". Possibilities for the argument error.plot are: ",
.collapse(allowed.error.plot, sep = ", "))
if(missing(width)) width <- 0.8
.map <- .mapping(p)
if(missing(color) & !is.null(.map$colour))
color <- .map$colour
if(missing(fill) & !is.null(.map$fill))
fill <- .map$fill
# Error limits
#::::::::::::::::::::::::::::::::::::::::::::::::::
. <- NULL
error.limit <- strsplit(error.plot, "_") %>%
unlist() %>%
.[1]
if(!(error.limit %in% c("upper", "lower")))
error.limit <- "both"
if(fun %in% c("mean", "median")) error.limit <- "none"
# Defining plot geometry
#::::::::::::::::::::::::::::::::::::::::::::::::::
geom <- error.plot
if(error.plot %in% c("pointrange", "lower_pointrange", "upper_pointrange"))
geom <- "pointrange"
else if(error.plot %in% c("linerange", "lower_linerange", "upper_linerange"))
geom <- "linerange"
else if(error.plot %in% c("errorbar", "lower_errorbar", "upper_errorbar"))
geom <- "errorbar"
fun.data <- fun.y <- fun.ymin <- fun.ymax <- NULL
if(fun %in% c("mean", "median")){
fun.y <- fun.ymin <- fun.ymax <- fun
}
else fun.data <- fun
# General option
#::::::::::::::::::::::::::::::::::::::::::::::::::
opts <- list(geomfunc = "stat_summary", fun.data = fun.data, fun.y = fun.y,
fun.ymin = fun.ymin, fun.ymax = fun.ymax,
color = color, geom = geom, size = size, linetype = linetype,
show.legend = show.legend, data = data, position = position,
fun.args = list(error.limit = error.limit), group = group)
if(fun %in% c("mean_ci", "median_hilow_")){
opts$fun.args$ci <- ci
}
# Specific option
#::::::::::::::::::::::::::::::::::::::::::::::::::
if(geom == "crossbar") opts <- opts %>%
.add_item(fill = fill, width = width)
else if(geom == "errorbar"){
if(missing(width)) opts$width = 0.1
else opts$width = width
}
opts %>% .update_plot(p)
}
#' @describeIn add_summary returns the \code{mean} and the error limits defined by the
#' \code{standard error}. We used the name \code{mean_se_}() to avoid masking \code{\link[ggplot2]{mean_se}}().
#' @export
mean_se_ <- function(x, error.limit = "both")
{
length <- base::sum(!is.na(x))
sd = stats::sd(x, na.rm=TRUE)
se <- sd / sqrt(length)
.mean <- base::mean(x, na.rm = TRUE)
data.frame(
y = .mean,
ymin = .mean - se,
ymax = .mean + se
) %>% .format_error(error.limit)
}
#' @describeIn add_summary returns the \code{mean} and the error limits defined by the
#' \code{standard deviation}.
#' @export
mean_sd <- function(x, error.limit = "both"){
sd = stats::sd(x, na.rm=TRUE)
.mean <- base::mean(x, na.rm = TRUE)
data.frame(
y = .mean,
ymin = .mean - sd,
ymax = .mean + sd
) %>% .format_error(error.limit)
}
#' @describeIn add_summary returns the \code{mean} and the error limits defined by the
#' \code{confidence interval}.
#' @export
mean_ci <- function(x, ci = 0.95, error.limit = "both"){
length <- base::sum(!is.na(x))
sd = stats::sd(x, na.rm=TRUE)
se <- sd / sqrt(length)
.mean <- base::mean(x, na.rm = TRUE)
ci <- stats::qt(ci/2 + .5, length-1)*se
data.frame(
y = .mean,
ymin = .mean - ci,
ymax = .mean + ci
) %>% .format_error(error.limit)
}
#' @describeIn add_summary returns the \code{mean} and the error limits defined by the
#' \code{range = max - min}.
#' @export
mean_range <- function(x, error.limit = "both"){
.mean <- base::mean(x, na.rm = TRUE)
.min <- base::min(x, na.rm=TRUE)
.max <- base::max(x, na.rm=TRUE)
.range <- .max - .min
data.frame(
y = .mean,
ymin = .mean - .range,
ymax = .mean + .range
) %>% .format_error(error.limit)
}
#' @describeIn add_summary returns the \code{median} and the error limits
#' defined by the \code{interquartile range}.
#' @export
median_iqr <- function(x, error.limit = "both"){
.median = stats::median(x, na.rm=TRUE)
.iqr <- stats::IQR(x, na.rm=TRUE)
data.frame(
y = .median,
ymin = .median - .iqr,
ymax = .median + .iqr
) %>% .format_error(error.limit)
}
#' @describeIn add_summary computes the sample median and a selected pair of
#' outer quantiles having equal tail areas. This function is a reformatted
#' version of \code{Hmisc::smedian.hilow()}. The confidence limits are computed
#' as follow: \code{lower.limits = (1-ci)/2} percentiles; \code{upper.limits =
#' (1+ci)/2} percentiles. By default (\code{ci = 0.95}), the 2.5th and the
#' 97.5th percentiles are used as the lower and the upper confidence limits,
#' respectively. If you want to use the 25th and the 75th percentiles as the
#' confidence limits, then specify \code{ci = 0.5} or use the function
#' \code{median_q1q3()}.
#' @export
median_hilow_ <- function(x, ci = 0.95, error.limit = "both"){
quant <- stats::quantile(
x,
probs = c(0.5, (1 - ci)/2, (1 + ci)/2),
na.rm = TRUE
)
names(quant) <- c("median", "lower", "upper")
data.frame(
y = quant["median"],
ymin = quant["lower"],
ymax = quant["upper"]
) %>%
.format_error(error.limit)
}
#' @describeIn add_summary computes the sample median and, the 25th and 75th
#' percentiles. Wrapper around the function \code{median_hilow_()} using
#' \code{ci = 0.5}.
#' @export
median_q1q3 <- function(x, error.limit = "both"){
median_hilow_(x, ci = 0.5, error.limit = error.limit)
}
#' @describeIn add_summary returns the \code{median} and the error limits
#' defined by the \code{median absolute deviation}.
#' @export
median_mad <- function(x, error.limit = "both"){
.median = stats::median(x, na.rm=TRUE)
.mad = stats::mad(x, na.rm=TRUE)
data.frame(
y = .median,
ymin = .median - .mad,
ymax = .median + .mad
) %>% .format_error(error.limit)
}
#' @describeIn add_summary returns the \code{median} and the error limits
#' defined by the \code{range = max - min}.
#' @export
median_range <- function(x, error.limit = "both"){
.median = stats::median(x, na.rm=TRUE)
.min <- base::min(x, na.rm=TRUE)
.max <- base::max(x, na.rm=TRUE)
.range <- .max - .min
data.frame(
y = .median,
ymin = .median - .range,
ymax = .median + .range
) %>% .format_error(error.limit)
}
# Format error
.format_error <- function(d, error.limit = "both"){
if(error.limit == "upper") d$ymin <- d$y
else if(error.limit == "lower") d$ymax <- d$y
else if(error.limit == "none") d$ymin <- d$ymax <- d$y
d
}