/
xpose.plot.qq.R
327 lines (291 loc) · 11.6 KB
/
xpose.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Xpose 4
# An R-based population pharmacokinetic/
# pharmacodynamic model building aid for NONMEM.
# Copyright (C) 1998-2004 E. Niclas Jonsson and Mats Karlsson.
# Copyright (C) 2005-2008 Andrew C. Hooker, Justin J. Wilkins,
# Mats O. Karlsson and E. Niclas Jonsson.
# Copyright (C) 2009-2010 Andrew C. Hooker, Mats O. Karlsson and
# E. Niclas Jonsson.
# This file is a part of Xpose 4.
# Xpose 4 is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation, either version 3
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. A copy can be cound in the R installation
# directory under \share\licenses. If not, see http://www.gnu.org/licenses/.
## Added by Justin Wilkins
## 28/11/2005
#' The generic Xpose functions for QQ plots
#'
#' This is a wrapper function for the lattice \code{\link[lattice]{qqmath}}
#' function.
#'
#'
#' @param x A string or a vector of strings with the name(s) of the
#' x-variable(s).
#' @param object An "xpose.data" object.
#' @param inclZeroWRES A logical value indicating whether rows with WRES=0
#' should be plotted.
#' @param onlyfirst A logical value indicating whether only the first row per
#' individual should be included in the plot.
#' @param samp An integer between 1 and object@Nsim
#' (see\code{\link{xpose.data-class}}) specifying which of the simulated data
#' sets to extract from SData.
#' @param aspect The aspect ratio of the display (see
#' \code{\link[lattice]{qqmath}}).
#' @param scales A list to be used for the \code{scales} argument in
#' \code{\link[lattice]{qqmath}}.
#' @param by A string or a vector of strings with the name(s) of the
#' conditioning variables.
#' @param force.by.factor Logical value. If TRUE, and \code{by} is not
#' \code{NULL}, the variable specified by \code{by} is taken as categorical.
#' @param ordby A string with the name of a variable to be used to reorder any
#' factor conditioning variables (\code{by}). The variable is used in a call to
#' the \code{reorder} function.
#' @param byordfun The name of the function to be used when reordering a factor
#' conditioning variable (see argument \code{ordby}).
#' @param shingnum The number of shingles ("parts") a continuous conditioning
#' variable should be divided into.
#' @param shingol The amount of overlap between adjacent shingles (see argument
#' \code{shingnum}).
#' @param strip The name of the function to be used as the strip argument to
#' the \code{\link[lattice]{xyplot}}.
#' @param subset A string giving the subset expression to be applied to the
#' data before plotting. See \code{\link{xsubset}}.
#' @param main A string giving the plot title or \code{NULL} if none.
#' @param xlb A string giving the label for the x-axis. \code{NULL} if none.
#' @param ylb A string giving the label for the y-axis. \code{NULL} if none.
#' @param pch Plotting symbol.
#' @param col Color of plotting symbol.
#' @param cex Amount to scale the plotting character by.
#' @param abllty Line type for qqline.
#' @param abllwd Line width for qqline.
#' @param ablcol Color for qqline.
#' @param mirror Should we create mirror plots from simulation data? Value can
#' be \code{FALSE}, \code{TRUE} or \code{1} for one mirror plot, or \code{3}
#' for three mirror plots.
#' @param max.plots.per.page The maximum number of plots per page that can be
#' created with the mirror plots.
#' @param mirror.aspect The aspect ratio of the plots used for mirror
#' functionality.
#' @param pass.plot.list Should we pass the list of plots created with mirror
#' or should we print them directly. Values can be \code{TRUE/FALSE}.
#' @param x.cex The size of the x-axis label.
#' @param y.cex The size of the y-axis label.
#' @param main.cex The size of the title.
#' @param mirror.internal an internal mirror argument used in
#' \code{\link{create.mirror}}. Checks if the \code{strip} argument from
#' \code{\link[lattice]{qqmath}} has been used.
#' @param \dots Other arguments passed to \code{\link{xpose.plot.qq}}.
#' @author E. Niclas Jonsson, Mats Karlsson, Andrew Hooker & Justin Wilkins
#' @seealso \code{\link{xpose.panel.qq}}, \code{\link[lattice]{qqmath}},
#' \code{\link[lattice]{panel.qqmathline}}, \code{\link{xpose.data-class}}
#' @keywords methods
#' @examples
#'
#' \dontrun{
#' ## xpdb5 is an Xpose data object
#' ## We expect to find the required NONMEM run and table files for run
#' ## 5 in the current working directory
#' xpdb5 <- xpose.data(5)
#'
#' ## A QQ plot of WRES
#' xpose.plot.qq("WRES", xpdb5)
#' }
#'
#'
#' @export xpose.plot.qq
xpose.plot.qq <-
function(x,object,
inclZeroWRES = FALSE,
onlyfirst = FALSE,
samp = NULL, ## Check if this is needed
aspect = object@Prefs@Graph.prefs$aspect,
scales = list(),
## Conditioning settings
by = object@Prefs@Graph.prefs$condvar,
force.by.factor = FALSE,
ordby = object@Prefs@Graph.prefs$ordby,
byordfun = object@Prefs@Graph.prefs$byordfun,
shingnum = object@Prefs@Graph.prefs$shingnum,
shingol = object@Prefs@Graph.prefs$shingol,
strip = function(...)
strip.default(...,strip.names=c(TRUE,TRUE)),
##par.strip.text=trellis.par.get("add.text"),
## Subset stuff
subset = xsubset(object),
## Axes and titles
main = xpose.create.title.hist(x,object,subset,...),
## main = NULL,
xlb = "Quantiles of Normal",
ylb = paste("Quantiles of ",xlabel(x,object),sep=""),
## Colors and stuff
pch=object@Prefs@Graph.prefs$pch,
col=object@Prefs@Graph.prefs$col,
cex=object@Prefs@Graph.prefs$cex,
abllty = object@Prefs@Graph.prefs$abllty,
abllwd = object@Prefs@Graph.prefs$abllwd,
ablcol = object@Prefs@Graph.prefs$ablcol,
## mirror stuff
mirror = FALSE,
max.plots.per.page=4,
mirror.aspect="fill",
pass.plot.list=FALSE,
x.cex=NULL,
y.cex=NULL,
main.cex=NULL,
mirror.internal=list(strip.missing=missing(strip)),
...) {
plotTitle <- main
## for MIRROR functionality
arg.list <- formals(xpose.plot.qq)
arg.names <- names(arg.list)
new.arg.list <- vector("list",length(arg.names))
names(new.arg.list) <- arg.names
for (argnam in arg.names){
if (argnam=="..."){
next
}
tmp <- get(argnam)
if (is.null(tmp)){
} else {
new.arg.list[[argnam]]=tmp
}
}
if (mirror){
create.mirror(xpose.plot.qq,
new.arg.list,mirror,plotTitle,...)
} else { # end if mirror
## x-label
#if(!is.null(x)) {
# if(length(x)> 1) {
# xlb <- NULL
# } else {
# xlb <- label(x,object)
# }
#}
## y-label
#ylb <- ylb
##Get data
if(!is.null(samp)) {
data <- SData(object,inclZeroWRES,onlyfirst=onlyfirst,
subset=subset,samp=samp)
} else {
data <- Data(object,inclZeroWRES,onlyfirst=onlyfirst,subset=subset)
}
## Strip "missing" data
data <- subset(data, get(x) != object@Prefs@Miss)
if(any(is.null(data))) return("The subset expression is invalid!")
## Make sure by is a factor if requested
if(!is.null(by) && force.by.factor) {
for(b in by) {
data[,b] <- as.factor(data[,b])
}
}
## Check to see if more that one x-variable
if(length(x) > 1) {
reps <-c(xvardef("id",object),xvardef("idlab",object),
xvardef("wres",object))
if(!is.null(by)) reps <- c(reps,by)
data <- xpose.stack(data,object,x,reps)
object <- new("xpose.data",
Runno=object@Runno,
Data = NULL)
Data(object) <- data
onlyfirst = FALSE
if(is.null(by)) {
by <- "ind"
} else {
by <- c("ind",by)
}
x <- "values"
scales=list(relation="free")
}
## Collect the basic plot formula
bb <- NULL
if(any(is.null(by))) { ## No conditioning
formel <- paste("~",x,sep="")
} else {
for(b in by) {
bb <- c(bb,xlabel(b,object))
if(!is.factor(data[,b])) {
data[,b] <- equal.count(data[,b],number=shingnum,overl=shingol)
} else {
if(any(!is.null(ordby))) {
data[,b] <- reorder(data[,b],data[,ordby],byordfun)
}
if(names(data[,b,drop=F])!="ind") {
levels(data[,b]) <-
paste(xlabel(names(data[,b,drop=F]),object),":", ## Needs to be fixed
levels(data[,b]),sep="")
}
}
}
bys <- paste(by,collapse="*")
formel <- paste("~",x,"|",bys,sep="")
}
if(missing(strip)) {
strip <- function(var.name,...)
strip.default(var.name=bb,strip.names=c(F,T),...)
}
#xvarnam <- x
if(!is.null(x.cex)) {
if (is.list(xlb)){
xlb$cex=x.cex
} else {
xlb <- list(xlb,cex=x.cex)
}
}
if(!is.null(y.cex)) {
if (is.list(ylb)){
ylb$cex=y.cex
} else {
ylb <- list(ylb,cex=y.cex)
}
}
if(is.null(main)) {
} else {
if(!is.null(main.cex)) {
if (is.list(main)){
main$cex=main.cex
} else {
main <- list(main,cex=main.cex)
}
}
}
xplot <- qqmath(formula(formel),
data,
obj=object,
#prepanel = function(x) {
# if(length(levs <- unique(x)) < object@Prefs@Cat.levels) {
# xlim <- as.character(sort(levs))
# return(list(xlim=xlim))
# } else {
# xlim <- range(x)
# return(list(xlim=xlim))
# }
#},
panel=xpose.panel.qq,
aspect=aspect,
ylab=ylb,
xlab=xlb,
scales=scales,
main=main,
#xvarnam=xvarnam,
abllty = abllty,
abllwd = abllwd,
ablcol = ablcol,
#border = hiborder,
pch=pch,
col=col,
cex=cex,
strip=strip,
...)
return(xplot)
}
}