-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgac_empirical.R
620 lines (553 loc) · 19.2 KB
/
gac_empirical.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# Front-end needs ---------------------------------------------------------
# Package load
library(plyr)
library(R2jags)
library(lubridate)
# Make function for inverting logit
inv.logit=function(x){
exp(x)/(1+exp(x))
}
# Make a function to get lower 95% credible limit with short name
low = function(x){
quantile(x, probs=c(0.025))
}
# Make a function to get upper 95% credible limit with short name
up = function(x){
quantile(x, probs=c(0.975))
}
# Make a function for dividing that can be passed to apply
divi <- function(x, y){
out <- x/y
return(out)
}
# Make a function for subtraction that can be passed to apply
subtract <- function(x, y){
out <- x-y
return(out)
}
# Data read -----
# Read in the data
# These are data for sunfishes
# represented in the NYSDEC 2015
# State-wide fisheries database
fish <- read.csv('sunnyAges.txt',
stringsAsFactors = FALSE)
# Remove everything but Pumpkinseed
fish <- fish[fish$Name=='Pumpkinseed' & fish$Age <= 10,]
# Count the number of fish per waterbody
tallies <- data.frame(with(fish, table(Water, Name)))
# Remove tallies for waterbodies with fewer than 50 fish
tallies <- tallies[tallies$Freq >= 50, ]
#tallies <- tallies[tallies$Water!="Chautauqua Lake\\oiuy-098765",]
# Convert waterbody to character string for merging
tallies$Water <- as.character(tallies$Water)
# Keep only those observations from the original data
# that have 50 or more samples
fish <- fish[fish$Water %in% tallies$Water, ]
# Renumber row names
row.names(fish) <- seq(1, nrow(fish), 1)
# Remove what appear to be erroneous age assignments or lengths
# fish <- fish[-2183, ]
# fish <- fish[-which(fish$Age==0 & fish$Length > 100),]
# fish <- fish[-which(fish$Age==1 & fish$Length > 150),]
#
# Have a look at the length-at-age data
plot(fish$Age, fish$Length)
# Calculate shoreline development index
fish$dev = (fish$ShrLen/.62)/(sqrt(4*pi*(fish$Sarea*.00156))) # Hutchinson (1957)
# Drop the two biggest lakes from the data set
fish <- fish[fish$Sarea < 10e3, ]
# Get year for each observation
fish$Date <- as.Date(fish$Date, format="%m/%d/%Y")
fish$year <- year(fish$Date)
# Get rid of missing values and age-0 fish
fish <- fish[!is.na(fish$Age), ];
fish <- fish[!is.na(fish$Length), ];
fish <- fish[fish$Age!=0, ];
# Create a new variable with combo of
# lake and region bc there are lakes
# in diff regions with same names.
fish$WaterR <- paste0(fish$Water, fish$Region)
# Model specification -----
# . Write the model in bugs language ----
modelString = "
model{
for(i in 1:N){
# Likelihood
Y[i] ~ dnorm(L[i], tau[Ti[i]])
# Length described by Galluci & Quinn (1979)
L[i] <- (w[i]/K[pop[i]])*(1-exp(-K[pop[i]]*(Ti[i]-to[pop[i]])))
# Linear predictor of w
log(w[i]) <- beta0[pop[i]] + betaY[region[i]]*dev[i]
}
# Priors
for(j in 1:npops){
# Priors on VBGF parameters (w defined below by linear model)
# Brody growth coefficient
lK[j] ~ dnorm(k.mu, k.tau)
logit(K[j]) <- lK[j]
# Age at length zero
to[j] ~ dnorm(t0.mu, t0.tau)T(-2,2)
# Priors on parameters of linear model on w
# Intercept
beta0[j] ~ dnorm(b.mu, b.tau)
}
# Regional priors based on NYSDEC Regions
for(r in 1:nregions){
betaY[r] ~ dnorm(bd.mu, bd.tau)
}
# Global priors parameters above
b.mu ~ dnorm(0, 0.001)
b.tau ~ dgamma(0.01, 0.001)
bd.mu ~ dnorm(0, 0.001)
bd.tau ~ dgamma(0.01, 0.001)
k.mu ~ dnorm(0, 0.001)
k.tau ~ dgamma(0.01, 0.001)
t0.mu ~ dnorm(0, 0.001)T(-2,0)
t0.tau ~ dgamma(0.01, 0.001)
# Prior distribution for precision at each age
# This imposes a multiplicative error structure on length at age
for(t in 1:Tmax){
tau[t] ~ dgamma(0.01, 0.001)
}
}"
# Model calibration -----
# . Parameters monitored ----
params = c('to', 'K', 'beta0', 'b.mu', 'b.tau', 'betaY',
'bd.mu', 'bd.tau', 'k.mu', 'k.tau',
't0.mu', 't0.tau')
# . Package the data for JAGS ----
vb_data = list(
Y = fish$Length,
Ti = as.numeric(as.factor(fish$Age)),
Tmax = max(as.numeric(as.factor(fish$Age))),
N = nrow(fish),
npops = length(unique(fish$WaterR)),
pop = as.numeric(as.factor(fish$WaterR)),
dev = as.vector(scale(fish$year)),
nregions = length(unique(fish$Region)),
region = as.numeric(as.factor(fish$Region))
)
# . Initial values ----
inits <- function(){
list(
b.mu = rnorm(1, 0, 1),
b.tau = rgamma(1, 0.01, 1),
bd.mu = rnorm(1, 0, 1),
bd.tau = rgamma(1, 0.01, 1),
k.mu = rnorm(1, 0, 1),
k.tau = rgamma(1, 0.01, 1),
t0.mu = runif(1, -2, 0),
t0.tau = rgamma(1, 0.01, 1),
tau = rgamma(max(as.numeric(as.factor(fish$Age))), 0.01, 1)
)
}
# . MCMC settings ----
ni <- 150000 # Number of draws from posterior (for each chain)
nt <- 100 # Thinning rate
nb <- 50000 # Number of draws to discard as burn-in
nc <- 3 # Number of chains
# . Call jags and run the model ----
vbModgq <- jags(data=vb_data, inits=inits, params,
textConnection(modelString),
n.chains = nc, n.thin = nt,
n.iter = ni, n.burnin = nb,
working.directory = getwd())
# Print a summary of the model
print(vbModgq)
# Save the results out to a file
save(vbModgq, file='empiricalresult.rda')
# Model results -----
# . Get posteriors -----
# Read in the model file
load('empiricalresult.rda')
# Get posterior distributions for parameter estimates
ek <- vbModgq$BUGSoutput$sims.list$K
et0 <- vbModgq$BUGSoutput$sims.list$to
ew <- exp(vbModgq$BUGSoutput$sims.list$beta0)
mu.w <- exp(vbModgq$BUGSoutput$sims.list$b.mu)
betaY <- vbModgq$BUGSoutput$sims.list$betaY
mu.k <- inv.logit(vbModgq$BUGSoutput$sims.list$k.mu)
mu.t0 <- vbModgq$BUGSoutput$sims.list$t0.mu
# . State-wide growth curve -----
# Make a sequence of new ages for which we will predict lengths
Age = seq(1, 13, 1)
samp = nrow(ek)
# Predict mean length at age for each sample
preds = matrix(data = NA, nrow=samp, ncol=length(Age))
for(i in 1:nrow(ek)){
for(t in 1:length(Age)){
preds[i, t] = mu.w[i]/mu.k[i]*(1-exp(-mu.k[i]*(Age[t]-mu.t0[i])))
}
}
# Plot the raw data
png(filename = 'statewide.png',
height = 878, width = 1314,
pointsize = 32)
par(mar=c(5,5,1,1))
plot(fish$Age[fish$Age!=0]+1,
fish$Length[fish$Age!=0],
ylim=c(0, 500),
yaxt='n', xlab='Age (years)',
ylab='Total length (mm)',
xlim=c(0,12), axes = FALSE,
pch = 21, bg=rgb(0.5,0.5,0.5,0.15),
col=rgb(0.5,0.5,0.5,0.05),
main='Statewide pumpkinseed growth')
# Plot the posterior predictions
for(i in 1:nrow(ek)){
lines(x = Age, y = preds[i, ], col=rgb(.7,.7,.7,.05), lwd=1)
}
# Calculate the mean and 95% CRIs for posterior predictions
muPred = apply(preds, 2, mean)
lowPred = apply(preds, 2, low)
upPred = apply(preds, 2, up)
# Plot the mean and 95% CRI for predicted length at each age
lines(Age, muPred, col='blue', lwd=2, lty=1)
lines(Age, upPred, col='red', lwd=2, lty=2)
lines(Age, lowPred, col='red', lwd=2, lty=2)
axis(1, pos=0, at=seq(1,13,1), labels=seq(0,12,1))
axis(2, pos=1, las=2)
dev.off()
# . Boxplot of k that shows 95% CRIs by waterbody ----
# Set margins
par(mar=c(5,5,1,1))
# Set up the boxplot, will write over this with new whiskers
bb <- boxplot(ek,
outline=FALSE, col='gray87', ylim=c(0,1),
col.axis='white', notch=FALSE, plot=FALSE)
# Replace stats for whiskers with 95% CI
bb$stats[c(1,5), ] <- apply(
ek,
2,
quantile,
probs=c(.025, 0.975), na.rm = TRUE
)
# Re-plot
bxp(bb, outline=FALSE, ylim=c(0,1),
boxfill='gray87', col.axis='white',
staplewex=0, whisklty=1, whiskcol='gray40',
whisklwd=2, boxcol='gray40', boxlwd=1,
medcol='gray40')
# Add x (side=1) and y (side=2) axes
axis(side=1, at=seq(0,50,5), labels = seq(0,50,5))
axis(side=2, las=2)
# Close it up with a box so axis styles match
box()
# Add axis labels
mtext(text='Population number', side=1, line=3.5)
mtext(text=expression(italic('k'['g'])), side=2, line=3.5)
# . Histogram of global omega (b.mu) ----
# Set plotting margins
par(mar=c(5,5,1,1))
# Make the histogram
hist(mu.w, main='', xlab=expression(mu[omega]),
axes=FALSE, col = 'gray87', border='gray90',
xlim = c(35,65), breaks=50)
# Add axes
axis(side=1, pos=0)
axis(side=2, pos=35, las=2)
# Add line for observed length at age 0
abline(v=mean(mu.w), col='gray40', lwd=2, lty=3)
# . Boxplot of omega that shows 95% CRIs by waterbody ----
# Set margins
par(mar=c(5,5,1,1))
# Set up the boxplot, will write over this with new whiskers
bb <- boxplot(ew,
outline=FALSE, col='gray87', ylim=c(0,125),
col.axis='white', notch=FALSE, plot=FALSE)
# Replace stats for whiskers with 95% CI
bb$stats[c(1,5), ] <- apply(
ew,
2,
quantile,
probs=c(.025, 0.975), na.rm = TRUE
)
# Re-plot
bxp(bb, outline=FALSE, ylim=c(0,125),
boxfill='gray87', col.axis='white',
staplewex=0, whisklty=1, whiskcol='gray40',
whisklwd=2, boxcol='gray40', boxlwd=1,
medcol='gray40')
# Add x (side=1) and y (side=2) axes
axis(side=1, at=seq(0,50,5), labels = seq(0,50,5))
axis(side=2, las=2)
# Close it up with a box so axis styles match
box()
# Add axis labels
mtext(text='Population number', side=1, line=3.5)
mtext(text=expression(omega['g']), side=2, line=3.5)
abline(h=median(ew))
# . Boxplot of coefficient that shows 95% CRIs by waterbody ----
# Set margins
par(mar=c(5,5,1,1))
# Set up the boxplot, will write over this with new whiskers
bb <- boxplot(betaY,
outline=FALSE, col='gray87', ylim=c(-.5,.5),
col.axis='white', notch=FALSE, plot=FALSE)
# Replace stats for whiskers with 95% CI
bb$stats[c(1,5), ] <- apply(
betaY,
2,
quantile,
probs=c(.025, 0.975), na.rm = TRUE
)
# Re-plot
bxp(bb, outline=FALSE, ylim=c(-.5,.5),
boxfill='gray87', col.axis='white',
staplewex=0, whisklty=1, whiskcol='gray40',
whisklwd=2, boxcol='gray40', boxlwd=1,
medcol='gray40', boxwex=0.3)
# Add x (side=1) and y (side=2) axes
axis(side=1, at=c(1,seq(2,8,1)), labels = c(1,seq(3,9,1)))
axis(side=2, las=2)
# Close it up with a box so axis styles match
box()
# Add axis labels
mtext(text='NYSDEC management region', side=1, line=3.5)
mtext(text=expression(beta['g']), side=2, line=3.5)
abline(h=0, lty=2)
# . Compare omegas to state-wide average ----
# Calculate ratio of waterbody omega to statewide average omega
reg <-data.frame(unique(cbind(fish$WaterR, fish$Region)))
regs <- reg[with(reg, order(X1)),]
row.names(regs) <- seq(1:nrow(regs))
regs2 <- regs[with(regs, order(X2)),]
reg <- as.factor(regs2[,2])
quotient <- apply(ew, 2, divi, mu.w)
quotient <- quotient[,as.numeric(row.names(regs))]
#diff <- apply(ew, 2, subtract, mu.w)
# Set up plotting file
png(filename = 'deltas.png',
height = 878, width = 1314,
pointsize = 32)
# Set margins
par(mar=c(5,5,1,1))
# Set up the boxplot, will write over this with new whiskers
bb <- boxplot(quotient,
outline=FALSE, col='gray87', ylim=c(.5,2),
col.axis='white', notch=FALSE, plot=FALSE)
# Replace stats for whiskers with 95% CI
bb$stats[c(1,5), ] <- apply(
quotient,
2,
quantile,
probs=c(.025, 0.975), na.rm = TRUE
)
# Re-plot
bxp(bb, outline=FALSE, ylim=c(.5,2), xlim=c(0,51),
boxfill=gray.colors(length(unique(reg)))[reg],
col.axis='white',
staplewex=0,
staplecol='gray40',
whisklty=1,
whiskcol='gray40',
whisklwd=2,
boxcol='gray40',
boxlwd=1,
medcol='gray40',
xaxt='n')
# Add x (side=1) and y (side=2) axes
axis(side=1, at=seq(0,50,5), labels = seq(0,50,5))
axis(side=2, las=2)
# Close it up with a box so axis styles match
# Add axis labels
mtext(text='Stock ID', side=1, line=3.5)
mtext(text=expression(delta['g']), side=2, line=3.5)
#abline(h=1, lty=2)
abline(v= which(!duplicated(reg))[-1]-.5,
col='gray30')
for(i in 1:length(unique(reg))){
text(x=c((which(!duplicated(reg))[-1]-.5),52)[i],
y=2,
labels=unique(reg)[i],
adj=c(rep(1.2, 3),1,rep(1.2, 3),.5)[i])
}
dev.off()
# . Write lake-specific quotients to a file -----
quoti <- apply(quotient, 2, mean)
outquote <- data.frame(regs, quoti)
boxplot(quoti~X2, data=outquote)
# . Figure 4 ----
# .. Load empirical results ----
load('empiricalresult.rda')
# Get posterior distributions for parameter estimates
ek <- vbModgq$BUGSoutput$sims.list$K
et0 <- vbModgq$BUGSoutput$sims.list$to
ew <- exp(vbModgq$BUGSoutput$sims.list$beta0)
mu.w <- exp(vbModgq$BUGSoutput$sims.list$b.mu)
betaY <- vbModgq$BUGSoutput$sims.list$betaY
mu.k <- inv.logit(vbModgq$BUGSoutput$sims.list$k.mu)
mu.t0 <- vbModgq$BUGSoutput$sims.list$t0.mu
# .. Set up image file ----
tiff(filename = 'Figure4.tif',
height = 2000, width = 2600,
res=350, pointsize = 10)
# Set up graphical parameters
par(mfrow=c(2,2), oma=c(1,1,0,0))
# .. State-wide growth curve -----
# Make a sequence of new ages for which we will predict lengths
Age = seq(1, 13, 1)
samp = nrow(ek)
# Predict mean length at age for each sample
preds = matrix(data = NA, nrow=samp, ncol=length(Age))
for(i in 1:nrow(ek)){
for(t in 1:length(Age)){
preds[i, t] = mu.w[i]/mu.k[i]*(1-exp(-mu.k[i]*(Age[t]-mu.t0[i])))
}
}
# Plot the raw data
par(mar=c(4.5,1.25,1.5,2))
plot(fish$Age[fish$Age!=0]+1,
fish$Length[fish$Age!=0],
ylim=c(0, 300),
yaxt='n', xlab='',
ylab='', xaxt='n',
xlim=c(0,12), axes = FALSE,
pch = 21, bg=rgb(0.5,0.5,0.5,0.15),
col=rgb(0.5,0.5,0.5,0.05),
main='')
# Plot the posterior predictions
for(i in 1:nrow(ek)){
lines(x = Age, y = preds[i, ], col=rgb(.7,.7,.7,.05), lwd=1)
}
# Calculate the mean and 95% CRIs for posterior predictions
muPred = apply(preds, 2, mean)
lowPred = apply(preds, 2, low)
upPred = apply(preds, 2, up)
# Plot the mean and 95% CRI for predicted length at each age
lines(Age, muPred, col='black', lwd=1, lty=1)
lines(Age, upPred, col='black', lwd=1, lty=2)
lines(Age, lowPred, col='black', lwd=1, lty=2)
# Add axes and labels
axis(1, pos=0, at=seq(1,13,1), labels=seq(0,12,1))
axis(2, pos=1, las=2)
mtext('Age (years)', side=1, line=3)
par(xpd=NA)
text(x=-1.25, y=150, 'Total length (mm)', srt=90)
#mtext('Total length (mm)', outer=TRUE, line=0)
# .. Boxplot of coefficient that shows 95% CRIs by waterbody ----
# Set margins
par(mar=c(5,4,2,2), xpd=FALSE)
# Set up the boxplot, will write over this with new whiskers
bb <- boxplot(betaY,
outline=FALSE, col='gray87', ylim=c(-.5,.5),
col.axis='white', notch=FALSE, plot=FALSE)
# Replace stats for whiskers with 95% CI
bb$stats[c(1,5), ] <- apply(
betaY,
2,
quantile,
probs=c(.025, 0.975), na.rm = TRUE
)
# Re-plot
bxp(bb, outline=FALSE, ylim=c(-.5,.5),
boxfill='gray87', col.axis='white',
staplewex=0, staplecol='gray40', whisklty=1, whiskcol='gray40',
whisklwd=1, boxcol='gray40', boxlwd=1,
medcol='gray40', boxwex=0.3)
# Add x (side=1) and y (side=2) axes
axis(side=1, at=c(1,seq(2,8,1)), labels = c(1,seq(3,9,1)))
axis(side=2, las=2)
# Close it up with a box so axis styles match
box()
# Add axis labels
mtext(text='Management region', side=1, line=3.5)
mtext(text=expression(beta['g']), side=2, line=3.5)
abline(h=0, lty=2)
# .. Boxplot of omega that shows 95% CRIs by waterbody ----
# Calculate ratio of waterbody omega to statewide average omega
reg <-data.frame(unique(cbind(fish$WaterR, fish$Region)))
regs <- reg[with(reg, order(X1)),]
row.names(regs) <- seq(1:nrow(regs))
regs2 <- regs[with(regs, order(X2)),]
reg <- as.factor(regs2[,2])
ew <- ew[,as.numeric(row.names(regs))]
# Set margins
par(mar=c(5,4,2,2))
# Set up the boxplot, will write over this with new whiskers
bb <- boxplot(ew,
outline=FALSE, col='gray87', ylim=c(0,125),
col.axis='white', notch=FALSE, plot=FALSE)
# Replace stats for whiskers with 95% CI
bb$stats[c(1,5), ] <- apply(
ew,
2,
quantile,
probs=c(.025, 0.975), na.rm = TRUE
)
# Re-plot
bxp(bb, outline=FALSE, ylim=c(0,125),
boxfill=gray.colors(length(unique(reg)))[reg],
col.axis='white',
staplewex=0, staplecol='gray40', whisklty=1, whiskcol='gray40',
whisklwd=1, boxcol='gray40', boxlwd=1,
medcol='gray40', xaxt='n')
# Add x (side=1) and y (side=2) axes
axis(side=1, at=seq(0,50,5), labels = seq(0,50,5))
axis(side=2, las=2)
# Close it up with a box so axis styles match
box()
# Add axis labels
mtext(text='Stock ID', side=1, line=3.5)
mtext(text=expression(omega['g']), side=2, line=3.5)
#abline(h=median(ew))
abline(v= which(!duplicated(reg))[-1]-.5,
col='gray30')
for(i in 1:length(unique(reg))){
text(x=c((which(!duplicated(reg))[-1]-.5),52)[i],
y=125,
labels=unique(reg)[i],
adj=c(rep(1.2, 3),1,rep(1.2, 3),.5)[i])
}
# .. Compare omegas to state-wide average ----
# Calculate ratio of waterbody omega to statewide average omega
reg <-data.frame(unique(cbind(fish$WaterR, fish$Region)))
regs <- reg[with(reg, order(X1)),]
row.names(regs) <- seq(1:nrow(regs))
regs2 <- regs[with(regs, order(X2)),]
reg <- as.factor(regs2[,2])
quotient <- apply(ew, 2, divi, mu.w)
quotient <- quotient[,as.numeric(row.names(regs))]
# Set margins
par(mar=c(5,4,2,2))
# Set up the boxplot, will write over this with new whiskers
bb <- boxplot(quotient,
outline=FALSE, col='gray87', ylim=c(.5,2),
col.axis='white', notch=FALSE, plot=FALSE)
# Replace stats for whiskers with 95% CI
bb$stats[c(1,5), ] <- apply(
quotient,
2,
quantile,
probs=c(.025, 0.975), na.rm = TRUE
)
# Re-plot
bxp(bb, outline=FALSE, ylim=c(.5,2), xlim=c(0,51),
boxfill=gray.colors(length(unique(reg)))[reg],
col.axis='white',
staplewex=0,
staplecol='gray40',
whisklty=1,
whiskcol='gray40',
whisklwd=1,
boxcol='gray40',
boxlwd=1,
medcol='gray40',
xaxt='n')
# Add x (side=1) and y (side=2) axes
axis(side=1, at=seq(0,50,5), labels = seq(0,50,5))
axis(side=2, las=2)
# Add axis labels
mtext(text='Stock ID', side=1, line=3.5)
mtext(text=expression(paste(Delta, omega['g'])), side=2, line=3.5)
abline(v= which(!duplicated(reg))[-1]-.5,
col='gray30')
for(i in 1:length(unique(reg))){
text(x=c((which(!duplicated(reg))[-1]-.5),52)[i],
y=2,
labels=unique(reg)[i],
adj=c(rep(1.2, 3),1,rep(1.2, 3),.5)[i])
}
# .. Close graphics device ----
dev.off()