-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplot-qq.R
269 lines (222 loc) · 7.11 KB
/
plot-qq.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
pmx_qq_stats <- function(points) {
sample <- sort(points)
distribution <- stats::qnorm
n <- length(sample)
line.p <- c(.25, .75)
quantiles <- stats::ppoints(n)
theoretical <- do.call(
distribution,
c(p = quote(quantiles))
)
x_coords <- do.call(distribution, c(list(p = line.p)))
y_coords <- quantile(sample, line.p)
slope <- diff(y_coords) / diff(x_coords)
intercept <- y_coords[1L] - slope * x_coords[1L]
data.table(intercept = intercept, slope = slope)
}
#' This function creates a qq plot object
#'
#' @param x \code{character} variable name to sample
#' @param labels list of texts/titles used within the plot
#' @param dname name of dataset to be used
#' @param point \code{list} geom_point attributes color, shape,...
#' @param reference_line \code{list} geom_line attributes. Used only for pmx_plot_eta_qq
#' @param is.reference_line \code{logical} if TRUE add reference line to the plot
#' @param facets \code{list}
#' @param xmax \code{logical} if FALSE do not use max(aes(x)) as limits default to TRUE
#' @param ... others graphics arguments passed to \code{\link{pmx_gpar}} internal object.
#'
#' @return \code{pmx_qq} object
#' @family qq_plot
#' @details
#'
#' \strong{labels} is a list that contains:
#' \itemize{
#' \item \strong{title:} plot title default "EBE vs. covariates"
#' \item \strong{x:} x axis label default to "Etas"
#' \item \strong{y:} y axis label default to empty
#' }
#'
#' \strong{point} is a list that contains:
#' \itemize{
#' \item \strong{shape:} default to 1
#' \item \strong{color:} default to black
#' \item \strong{size:} default to 1
#' }
#' @param is.hline logical if TRUE add horizontal line y=0 ( TRUE by default)
#' @param hline geom hline graphical parameters
#' @param is.vline logical if TRUE add vertical line x=0 ( TRUE by default)
#' @param vline geom vline graphical parameters
#' @param is.shrink \code{logical} if TRUE add shrinkage to the plot
#' @param shrink \code{pmxShrinkClass} shrinkage graphical parameter or
#' \code{list} coercible into one
pmx_qq <- function(
x,
labels,
dname = NULL,
point = NULL,
xmax = TRUE,
facets = NULL,
is.reference_line = NULL,
reference_line = NULL,
is.shrink = NULL,
shrink = NULL,
is.hline = NULL,
hline = NULL,
is.vline = NULL,
vline = NULL,
...) {
assert_that(is_string_or_null(dname))
if (is.null(dname)) dname <- "predictions"
if (missing(labels)) {
labels <- list(
title = sprintf("QQ plot: %s", x),
y = "",
x = "",
subtitle = ""
)
}
assert_that(is_list(labels))
default_point <- list(shape = 1, colour = "black", size = 1)
point <- l_left_join(default_point, point)
assert_that(is_list_or_null(facets))
assert_that(is_list_or_null(point))
assert_that(is_list_or_null(reference_line))
assert_that(is_list_or_null(hline))
assert_that(is_list_or_null(shrink))
labels$subtitle <- ""
structure(list(
ptype = "PMX_QQ",
strat = TRUE,
x = x,
dname = dname,
point = point,
is.reference_line = is.reference_line,
reference_line = reference_line,
xmax = xmax,
facets = facets,
is.shrink = is.shrink,
shrink = shrink,
is.hline = is.hline,
hline = hline,
is.vline = is.vline,
vline = vline,
gp = pmx_gpar(
labels = labels,
discrete = TRUE,
is.smooth = FALSE,
...
)
), class = c("pmx_qq", "pmx_gpar"))
}
#' This function plot EBE versus covariates using qq plots
#'
#' @param x pmx_qq object
#' @param dx data set
#' @param ... not used for the moment
#' @return ggplot2 plot
#' @export
#' @seealso \code{\link{eta_cov}}
#' @family plot_pmx
#' @import ggplot2
#'
plot_pmx.pmx_qq <- function(x, dx, ...) {
SHRINK <- annotation <- NULL
if (!(x$x %in% names(dx))) {
return(NULL)
}
dx <- dx[!is.infinite(get(x$x))]
strat.facet <- x[["strat.facet"]]
strat.color <- x[["strat.color"]]
vec.color <- c(grep("~", strat.color, invert = TRUE, value = TRUE))
vec.facet <- c(grep("~", strat.facet, invert = TRUE, value = TRUE))
grp <- c(vec.color, vec.facet)
if (sum(nchar(grp)) == 0) grp <- NULL
dx.ref <- if ("EFFECT" %in% names(dx)) {
dx[, pmx_qq_stats(get(x$x)), c("EFFECT", grp)]
} else {
dx[, pmx_qq_stats(get(x$x)), grp]
}
p <- ggplot(dx, aes(sample = .data[[x$x]])) +
with(
x$point,
geom_point(
stat = "qq", shape = shape, colour = colour,
size = size
)
)
hline_layer <- if (!is.null(x$is.hline) && x$is.hline) {
hline <- l_left_join(list(yintercept = 0), x$hline)
do.call(geom_hline, hline)
}
vline_layer <- if (!is.null(x$is.vline) && x$is.vline) {
vline <- l_left_join(list(xintercept = 0), x$vline)
do.call(geom_vline, vline)
}
reference_layer <- if (!is.null(x$is.reference_line) && x$is.reference_line) {
x$reference_line$mapping <- aes(slope = .data$slope, intercept = .data$intercept)
if (!is.null(strat.color)) {
if (is.formula(strat.color)) {
strat.color <- setdiff(as.character(strat.color), "~")
}
x$reference_line$colour <- NULL
x$reference_line$mapping <- aes(
slope = .data$slope, intercept = .data$intercept,
colour = .data[[strat.color]]
)
}
x$reference_line$data <- dx.ref
do.call("geom_abline", x$reference_line)
}
layer_shrink <- if (!is.null(x$is.shrink) && x$is.shrink && !is.null(x[["shrink.dx"]])) {
x$shrink$data <- x[["shrink.dx"]]
x$shrink$data$annotation <- x$shrink$annotation
x$shrink$annotation <- NULL
x$shrink$mapping <-
aes(
label = sprintf("%s=%s%%", annotation, round(SHRINK * 100))
)
x$shrink$inherit.aes <- FALSE
x$shrink$x <- -Inf
x$shrink$y <- Inf
x$shrink$fun <- NULL
do.call(geom_text, x$shrink)
}
layer_facet <- if ("EFFECT" %in% names(dx)) {
if (!is.null(strat.facet)) {
wf <- wrap_formula(strat.facet, "EFFECT")
x$facets$nrow <- NULL
x$facets$ncol <- NULL
do.call("facet_grid", c(wf, x$facets))
}
else {
wf <- formula("~EFFECT")
do.call("facet_wrap", c(wf, x$facets))
}
} else {
if (!is.null(strat.facet)) {
if (is.character(strat.facet)) {
wf <- formula(paste0("~", strat.facet))
do.call("facet_wrap", c(wf, x$facets))
}
}
}
layer_color <- if (!is.null(strat.color)) {
if (is.formula(strat.color)) {
strat.color <- setdiff(as.character(strat.color), "~")
}
geom_point(stat = "qq", aes(colour = .data[[strat.color]]))
}
p <- p + layer_facet + layer_shrink +
layer_color + reference_layer + hline_layer + vline_layer
if (!is.null(p)) p <- plot_pmx(x$gp, p)
if (x$xmax) {
xmin <- min(dx[, x$x, with = FALSE], na.rm = TRUE)
xmax <- max(dx[, x$x, with = FALSE], na.rm = TRUE)
xrange <- c(xmin - .001 * (xmax - xmin), xmax + .001 * (xmax - xmin))
p <- p +
coord_cartesian(xlim = xrange, ylim = xrange) +
theme(aspect.ratio = 1)
}
p
}