-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboxquant.s
More file actions
153 lines (122 loc) · 3.49 KB
/
boxquant.s
File metadata and controls
153 lines (122 loc) · 3.49 KB
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
# This is David Shera's version of the function.
libsrc(lighten)
boxquant = function(x,y,main="",
xlab=deparse(substitute(x)),ylab=deparse(substitute(y)),
yrange=range(y,na.rm=TRUE),
xrange=range(x,na.rm=TRUE),
col=gray(0),pch=1,
boxcol = darken(col),
ptpch=1,
hlines=0,
meanpch = NA,
## nonames=FALSE,
boxwex = .5,
test=0,
ptlab = NA,
ptlabcex = .7,
cex=1,
## xgrplab = NULL,
...
) {
## x = categorical group variable
## Automatically splits boxplots by x
## uses col for color
## if pch exists, uses it for plot character
## sel = !is.na(y) & !is.na(x)
## this code figures out which colors to use based on data$col
if (length(col)==1) {
colvec = col
} else {
if (test) cat("boxquant: before col > 1\n")
if (test) pv(length(col))
if (test) pv(length(x))
tcol = table(x,col)
if (test>0) print(tcol)
tpos = tcol>0
if (test>0) pv(tpos)
trow = apply(tpos,1,any)
if (test>0) pv(trow)
tcol = tcol[trow,]
if (test>0) print(tcol)
tdim = dim(tcol)
tcolcol = colnames(tcol)
for (ir in 1:(tdim[1])) {
trow = tcol[ir,]
if (ir == 1) {
colvec = tcolcol[trow>0]
} else {
colvec = c(colvec, tcolcol[trow>0])
}
}
}
if (test>0) pv(colvec)
colveclt = lighten(colvec)
colvecdk = darken(colvec)
if (test>0) pv(colveclt)
bqdata = data.frame(x=x,y=y,ptlab=ptlab)
if (test>0) cat("just after bqdata= \n")
if (test>0) pv(dim(bqdata))
collt = lighten(col)
bqdata$col = collt
bqdata$ptcol = col
bqdata$pch = ptpch
if (test>0) pv(colnames(bqdata))
if (test>0) pv(dim(bqdata))
if (test>0) pv(yrange)
ff = as.formula("y ~ x")
if (test>0) pv(ff)
if (test>0) pv(dim(data))
pv(ptpch)
boxplot(ff,data=bqdata,
main = main,
ylab = ylab,
xlab = xlab,
boxcol = colveclt,
whiskcol = colveclt,
whisklty = 1,
outcol = colveclt,
outpch = bqdata$ptpch,
staplecol = colveclt,
medcol = colveclt,
boxwex = boxwex,
staplwex = .3,
ylim = yrange,
...
)
## overlayed quantile plots
xlist = sort(unique(x))
if (test>0) pv(xlist)
abline(h=hlines,col=gray(.8))
center = 0
abline(v=center+.5,col=gray(.9))
for (g in xlist) {
if (test>0) pv(g)
seldd = bqdata$x==g & !is.na(bqdata$y)
dd = bqdata[seldd,]
colvecdd = colvec[seldd]
if (test>0) pv(dim(dd))
if (test>0) pv(colnames(dd))
center = center + 1
abline(v=center+.5,col=gray(.9))
if (!is.na(meanpch)) {
grpmean = mean(dd[,"y"],na.rm=TRUE)
points(center,grpmean,pch=meanpch,col=dd[1,"col"],cex=2)
}
ngrp = dim(dd)[1]
if (test>0) pv(ngrp)
## print(ngrp)
## print(dim(dd))
sel = !is.na(dd[,"y"])
if (test>0) tb(sel)
yy = dd[sel,"y"]
if (test>0) pv(length(yy))
r = rank(yy,ties="random")
rx = (r-.5)/ngrp - .5 + center
## print(cbind(x,dd$ageresid))
if (test>0) pv(length(rx))
points(rx,yy,pch=dd$pch,col=dd$ptcol,cex=cex)
if (length(ptlab)>1 | !is.na(ptlab)) {
text(rx,yy,labels=dd$ptlab,cex=ptlabcex,col=dd$ptcol,adj=c(-.1,.5))
}
}
}