-
Notifications
You must be signed in to change notification settings - Fork 60
/
plot_stat_table.R
227 lines (189 loc) · 6.74 KB
/
plot_stat_table.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
plot_stats_table <- function(data, nmstats, method,
sim.lines,
sim.col = NULL, sim.lwd = NULL, mean.line,
mean.smooth, mean.col = NULL,
mean.lwd, mean.lty, qnts, qnts.col = NULL,
qnts.alpha,
qnts.smooth, targ.line, targ.col = NULL, targ.lwd,
targ.lty, plots.joined = NULL, draw_legend = NULL,
grid, targets,
dynamic,
xlim = NULL, ylim = NULL,
xlab = NULL, ylab = NULL,
...) {
nstats <- length(nmstats)
if (is.null(plots.joined)) {
plots.joined <- nstats <= 3
}
if (nstats == 1) {
plots.joined <- TRUE
sim.col <- "dodgerblue3"
}
xlim <- NVL(xlim, c(1, dim(data)[1]))
xlab <- if (isFALSE(plots.joined)) "" else NVL(xlab, if (isTRUE(dynamic)) "time" else "simulation number")
ylab <- if (isFALSE(plots.joined)) "" else NVL(ylab, if (nstats == 1) nmstats else "Statistic")
if (is.null(sim.lwd)) {
if (dim(data)[3] > 1) {
sim.lwd <- max(c(1 - (dim(data)[3] * 0.05), 0.5))
} else {
sim.lwd <- 1
}
}
## Color Vector Validation
# 1. Sim.col, mean.col, qnts.col, targ.col must be missing or a vector of
# length 1 or nstats
# 2. If sim.col, mean.col, qnts.col, or targ.col is not missing
# but is a vector of length 1 and nstats is greater than 1,
# then replicate the color vector nstats times to achieve a vector of
# size nstats.
check_len_rep <- function(object, default, name) {
if (!is.null(object)) {
if (length(object) %in% c(1, nstats)) {
rep(object, length.out = nstats)
} else {
stop(paste0(name, " must be either missing or a vector of length 1 or nstats (", nstats, ")"))
}
} else {
rep(default, length.out = nstats)
}
}
sim.col <- check_len_rep(
sim.col,
if (isFALSE(plots.joined)) "dodgerblue3" else seq_len(nstats + 1L)[-1L],
"sim.col"
)
mean.col <- check_len_rep(
mean.col,
if (isFALSE(plots.joined)) "black" else sim.col,
"mean.col"
)
qnts.col <- check_len_rep(qnts.col, sim.col, "qnts.col")
qnts.col <- adjustcolor(qnts.col, qnts.alpha)
targ.col <- check_len_rep(
targ.col,
if (isFALSE(plots.joined) || nstats == 1) "black" else sim.col,
"targ.col"
)
draw_legend <- isTRUE(plots.joined) &&
((!is.null(draw_legend) && isTRUE(draw_legend)) ||
(is.null(draw_legend) && nstats == 1))
draw_qnts <- isTRUE(dynamic) && is.numeric(qnts)
mains <- if (isTRUE(plots.joined)) character(nstats) else nmstats
if (method == "l") {
qnts_list <- list()
means <- list()
ylims <- list()
for (j in seq_len(nstats)) {
dataj <- matrix(data[, j, ], nrow = dim(data)[1])
if (isTRUE(draw_qnts)) {
qnts_list[[j]] <- get_qnts(dataj, qnts, qnts.smooth)
}
if (isTRUE(mean.line)) {
means[[j]] <- get_means(dataj, mean.smooth)
}
}
for (j in seq_len(nstats)) {
if (!is.null(ylim)) {
ylims[[j]] <- ylim
} else {
limdat <- c(
if (isTRUE(plots.joined) && isTRUE(sim.lines)) data,
if (isFALSE(plots.joined) && isTRUE(sim.lines)) data[, j, ],
if (isTRUE(plots.joined) && isTRUE(mean.line)) sapply(means, `[[`, "y"),
if (isFALSE(plots.joined) && isTRUE(mean.line)) means[[j]]$y,
if (isTRUE(plots.joined) && isTRUE(draw_qnts)) sapply(qnts_list, `[[`, "y"),
if (isFALSE(plots.joined) && isTRUE(draw_qnts)) qnts_list[[j]]$y,
if (isTRUE(plots.joined) && isTRUE(targ.line)) targets,
if (isFALSE(plots.joined) && isTRUE(targ.line)) targets[j]
)
ylimsj <- suppressWarnings(c(min(limdat, na.rm = TRUE), max(limdat, na.rm = TRUE)))
if (any(is.infinite(ylimsj))) {
## should both be infinite in this case, indicating no non-missing data to plot;
## set both limits to 0 simply to avoid errors when calling plot below
ylimsj <- c(0, 0)
} else {
## give +/- 10% buffer in a way that works for signed statistics
ylimsj[1] <- ylimsj[1] - 0.1 * abs(ylimsj[1])
ylimsj[2] <- ylimsj[2] + 0.1 * abs(ylimsj[2])
}
ylims[[j]] <- ylimsj
}
}
if (isFALSE(plots.joined)) {
if (nstats == 1) dimens <- c(1, 1)
if (nstats == 2) dimens <- c(1, 2)
if (nstats == 3) dimens <- c(1, 3)
if (nstats == 4) dimens <- c(2, 2)
if (nstats == 5) dimens <- c(2, 3)
if (nstats == 6) dimens <- c(2, 3)
if (nstats %in% 7:9) dimens <- c(3, 3)
if (nstats %in% 10:12) dimens <- c(4, 3)
if (nstats %in% 13:16) dimens <- c(4, 4)
if (nstats > 16) dimens <- rep(ceiling(sqrt(nstats)), 2)
# Pull graphical parameters
ops <- list(mar = par()$mar, mfrow = par()$mfrow, mgp = par()$mgp)
par(mar = c(2.5, 2.5, 2, 1), mgp = c(2, 1, 0), mfrow = dimens)
}
## do actual plotting
for (j in seq_len(nstats)) {
if (j == 1 || isFALSE(plots.joined)) {
plot(NULL,
xlim = xlim,
ylim = ylims[[j]],
type = "n",
xlab = xlab,
ylab = ylab,
main = mains[j])
}
dataj <- matrix(data[, j, ], nrow = dim(data)[1])
if (isTRUE(draw_qnts)) {
polygon(qnts_list[[j]]$x, qnts_list[[j]]$y, col = qnts.col[j], border = NA)
}
if (isTRUE(sim.lines)) {
apply(dataj,
2,
function(y) lines(which(!is.na(y)), y[!is.na(y)], lwd = sim.lwd, col = sim.col[j]))
}
if (isTRUE(mean.line)) {
lines(means[[j]]$x,
means[[j]]$y,
lwd = mean.lwd,
col = mean.col[j],
lty = mean.lty)
}
if (isTRUE(targ.line)) {
abline(h = targets[j],
lty = targ.lty,
lwd = targ.lwd,
col = targ.col[j])
}
if (isTRUE(grid) && isFALSE(plots.joined)) {
grid()
}
}
if (isTRUE(grid) && isTRUE(plots.joined)) {
grid()
}
if (isTRUE(draw_legend)) {
legend("topleft", legend = nmstats, lwd = 2,
col = sim.col[1:nstats], cex = 0.75, bg = "white")
}
if (isFALSE(plots.joined)) {
# Reset graphical parameters
on.exit(par(ops))
}
}
if (method == "b") {
data <- matrix(aperm(data, c(1, 3, 2)), nrow = dim(data)[1] * dim(data)[3])
colnames(data) <- nmstats
boxplot(data, ...)
for (j in seq_len(nstats)) {
points(x = j, y = targets[j],
pch = 16, cex = 1.5, col = "blue")
## Grid
if (isTRUE(grid)) {
grid()
}
}
}
}