-
Notifications
You must be signed in to change notification settings - Fork 5
/
plot-pair_BA.R
226 lines (209 loc) · 5.44 KB
/
plot-pair_BA.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
#' Bland-Altman plot
#'
#' @inheritParams basic_arguments
#' @inheritParams plot_raincloud
#' @return \code{\link[ggplot2]{ggplot}} object
#' @export
#' @seealso \code{\link{plot_pair_lm}} for lm residuals plot and
#' \code{\link{plot_pair_OLP}} for ordinary-least-squares residuals
#' plot
#' @examples
#' criterion <- rnorm(
#' n = 100,
#' mean = 100,
#' sd = 10
#' )
#'
#' practical <- criterion * 1.2 + rnorm(n = 100, mean = -12, sd = 5)
#'
#' plot_pair_BA(practical,
#' criterion,
#' SESOI_lower = -10,
#' SESOI_upper = 10,
#' predictor_label = "Practical",
#' outcome_label = "Criterion"
#' )
plot_pair_BA <- function(predictor,
outcome,
SESOI_lower = 0,
SESOI_upper = 0,
confidence = 0.95,
predictor_label = "Predictor",
outcome_label = "Outcome",
control = plot_control(),
na.rm = FALSE) {
# +++++++++++++++++++++++++++++++++++++++++++
# Code chunk for dealing with R CMD check note
midway <- NULL
difference <- NULL
# +++++++++++++++++++++++++++++++++++++++++++
if (length(predictor) != length(outcome)) {
stop("Predictor and Outcome variables differ in size. Unable to proceed", call. = FALSE)
}
# Prepare DF for plotting
plot_data <- data.frame(
predictor = predictor,
outcome = outcome
)
if (na.rm) plot_data <- stats::na.omit(plot_data)
n_observations <- nrow(plot_data)
plot_data$difference <- plot_data$outcome - plot_data$predictor
plot_data$midway <- (plot_data$predictor + plot_data$outcome) / 2
plot_data$SESOI_lower <- plot_data$predictor + SESOI_lower
plot_data$SESOI_upper <- plot_data$predictor + SESOI_upper
# Level of agreement
mean_difference <- mean(plot_data$difference)
sd_difference <- stats::sd(plot_data$difference)
LOA <- sd_difference * stats::qt(
1 - ((1 - confidence) / 2),
df = n_observations - 1
)
LOA_lower <- mean_difference - LOA
LOA_upper <- mean_difference + LOA
# Scatter plot
bland_altman_A <- ggplot2::ggplot(
plot_data,
ggplot2::aes(
x = predictor,
y = outcome
)
) +
cowplot::theme_cowplot(control$font_size)
# Identity line
if (control$identity_line) {
if (control$SESOI_band) {
bland_altman_A <- bland_altman_A +
# SESOI band around identity line
ggplot2::geom_ribbon(
ggplot2::aes(
ymin = SESOI_lower,
ymax = SESOI_upper
),
fill = control$SESOI_color,
alpha = control$SESOI_alpha
)
}
bland_altman_A <- bland_altman_A +
ggplot2::geom_abline(
slope = 1,
intercept = 0,
colour = "black",
linetype = "dashed",
alpha = 0.4
)
}
bland_altman_A <- bland_altman_A +
ggplot2::geom_point(
alpha = control$points_alpha,
size = control$points_size,
shape = control$points_shape,
color = control$points_color,
fill = control$points_fill
) +
ggplot2::geom_smooth(
formula = y ~ x,
method = control$smooth_method,
level = confidence,
se = control$smooth_se,
size = control$smooth_size,
alpha = control$smooth_alpha,
fill = control$smooth_fill,
color = control$smooth_color
) +
ggplot2::xlab(predictor_label) +
ggplot2::ylab(outcome_label)
bland_altman_B <- ggplot2::ggplot(
plot_data,
ggplot2::aes(
x = midway,
y = difference
)
) +
cowplot::theme_cowplot(control$font_size)
# SESOI band
if (control$SESOI_band) {
bland_altman_B <- bland_altman_B +
ggplot2::annotate(
"rect",
xmin = -Inf,
xmax = Inf,
ymin = SESOI_lower,
ymax = SESOI_upper,
fill = control$SESOI_color,
alpha = control$SESOI_alpha
)
}
bland_altman_B <- bland_altman_B +
ggplot2::geom_abline(
slope = 0,
intercept = 0,
color = "white",
size = 0.75
) +
# Mean and LOA
ggplot2::geom_hline(
yintercept = mean_difference,
color = "black",
alpha = 0.4
) +
ggplot2::geom_hline(
yintercept = LOA_lower,
color = "black",
linetype = "dashed",
alpha = 0.4
) +
ggplot2::geom_hline(
yintercept = LOA_upper,
color = "black",
linetype = "dashed",
alpha = 0.4
) +
ggplot2::geom_point(
alpha = control$points_alpha,
size = control$points_size,
shape = control$points_shape,
color = control$points_color,
fill = control$points_fill
) +
# Regression line
ggplot2::geom_smooth(
formula = y ~ x,
method = control$smooth_method,
level = confidence,
se = control$smooth_se,
size = control$smooth_size,
alpha = control$smooth_alpha,
color = control$smooth_color,
fill = control$smooth_fill
) +
ggplot2::xlab(
paste(
"(",
predictor_label,
" + ",
outcome_label,
") / 2",
sep = ""
)
) +
ggplot2::ylab(
paste(
outcome_label,
" - ",
predictor_label,
sep = ""
)
)
bland_altman <- cowplot::plot_grid(
bland_altman_A,
bland_altman_B,
labels = control$panel_labels,
label_size = control$panel_labels_size,
hjust = c(0, 0, 0),
align = "hv",
axis = "l",
ncol = 2,
nrow = 1
)
return(bland_altman)
}