-
Notifications
You must be signed in to change notification settings - Fork 0
/
27.fitness.BLUPS.Rmd
548 lines (415 loc) · 21.7 KB
/
27.fitness.BLUPS.Rmd
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
---
title: "27.fitness.BLUPs"
author: "Daniele Filiault"
date: "12/4/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#setwd("/Volumes/field_experiments/adaptation_sweden/common.gardens/")
library(lme4) # mixed models
library(statmod)
library(tweedie) # tweedie model specification
library(merTools)
library(MuMIn) # for calculating R2 of mixed models
library(RColorBrewer)
library(pander)
library(xtable)
library(dplyr)
library(tidyr)
library(ggplot2)
library(corrplot)
library(heatmaply)
library(gridExtra)
library(superheat) #extended heatmap functions
library(pheatmap)
library(gplots)
##for maps
library(maps)
library(mapdata)
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
library("rgeos")
library("egg")
library("gplots")
```
## Want to estimate BLUPs per accessions (i.e. use accession as a random effect in a mixed effects model)
### 1. prep data
```{r data prep}
d11=readRDS(file="./data/d11_for_analysis.rds")
d12=readRDS(file="./data/d12_for_analysis.rds")
##restrict to fitness
traits=c("fitness")
d11$year=2011
d12$year=2012
d11=d11[, c("year", "exp", "block", "id", "fitness")]
d12=d12[, c("year", "exp", "block", "id", "fitness")]
d=rbind(d11, d12)
d$region[d$exp%in%c("ULL", "RAT")]="S"
d$region[d$exp%in%c("RAM", "ADA")]="N"
d$year=as.factor(d$year)
d=d[, c("year", "region", "exp","block", "id", "fitness")]
d=na.omit(d)
hist(d$fitness)
hist(sqrt(d$fitness))
with(d, boxplot(fitness~exp+year))
pdf(file="./figures/27.figures/raw.fitness.boxplot.by.year.site.pdf", width=10, height=6)
with(d, boxplot(fitness~year+exp, col=c(rep(brewer.pal(9, "Paired")[4],4), rep(brewer.pal(9, "Paired")[3],4))))
dev.off()
## will also look at background effects captured as genetic or K groups in subsection 4
kg <- read.table("./data/29.data/all.clusters.and normalized.BLUPs.txt", fill=TRUE, header=TRUE)
```
### 2. fixed effect linear models
```{r fixed effect linear models}
### do as normal lm first to understand specification, then move to glm so can use tweedie
lm1 <- lm(fitness~id*year*exp, data=d)
lm3 <- lm(fitness~id*year*exp + exp:year/block, data=d)
plot(lm3)
anova(lm1,lm3) ### yes, lm3 is much better. The block helps.
anova(lm3)
### test model fits by removing some components
lm4 <- lm(fitness~id + year*exp + exp:year/block, data=d)
lm5 <- lm(fitness~id, data=d)
lm6 <- lm(fitness~year*exp + exp:year/block, data=d)
anova(lm3, lm4)
anova(lm3, lm5)
anova(lm3, lm6)
lm.pve <- function(up.lm){
ssq <- anova(up.lm)[[2]]
pve <- ssq/sum(ssq) *100
names(pve) <- rownames(anova(up.lm))
return(pve)
}
pve3 <- lm.pve(lm3)
pve1 <- lm.pve(lm1)
#(summary(lm3))
pander(anova(lm3))
# make anova table with PVE
lm3.tab <- as.data.frame(anova(lm3))
lm3.tab$PVE <- pve3
rownames(lm3.tab)[8] <- "year:exp/block"
xtab.lm3 <- xtable(lm3.tab)
print(xtab.lm3, file="./figures/27.figures/fixed.effects.anova.table.txt")
```
```{r heritability per site/year}
combos <- expand.grid(unique(d$year), unique(d$exp))
check.pvals <- function(d,y,e){
up.d <- d[d$year==y & d$exp==e,]
up.lm <- lm(fitness~id+block, data=up.d)
return(anova(up.lm))
}
exp.anovas <- apply(combos, 1, function(x){check.pvals(d=d, y=x[1], e=x[2])}) # all genotypes significant, block sig in all but RAM_2012
get.pve <- function(d, y, e){
up.d <- d[d$year==y & d$exp==e,]
up.lm <- lm(fitness~id+block, data=up.d)
pve <- lm.pve(up.lm)
return(pve)
}
exp.pves <- apply(combos, 1, function(x){get.pve(d=d, y=x[1], e=x[2])})
exp.pves <- as.data.frame(t(exp.pves))
exp.pves <- cbind(combos, exp.pves)
colnames(exp.pves) <- c("year", "site", "genotype", "block", "residuals")
exp.pves$site <- gsub("ULL", "SU", exp.pves$site)
exp.pves$site <- gsub("RAT", "SR", exp.pves$site)
exp.pves$site <- gsub("RAM", "NM", exp.pves$site)
exp.pves$site <- gsub("ADA", "NA", exp.pves$site)
exp.pves <- exp.pves[order(exp.pves$site),]
xtab.pve <- xtable(exp.pves)
print(xtab.pve, file="./figures/27.figures/pve.table.txt")
```
So the full model with triple interaction plus block is the best one (lm3). Plots aren't the best - you can clearly see the effect of the truncated 0 distribution. Can try this as a tweedie model?
### 3. fixed effect linear models - tweedie
```{r test tweedie models, eval=FALSE}
#profile.all.dat <- tweedie.profile(fitness~id*year*exp + exp:year/block, data = d, p.vec = seq(1.1, 1.9, 0.1),do.plot=TRUE, fit.glm = TRUE)
#print(profile.all.dat$p.max)
#vp.up <- 1.385714
#dat.up <- d
#glm.a11 <- glm(fitness~id*year*exp + exp:year/block, family=tweedie(var.power=vp.up, link.power=0), data=dat.up)
#Analysis of Deviance Table
#Model: Tweedie, link: mu^0
#Response: fitness
#Terms added sequentially (first to last)
# Df Deviance Resid. Df Resid. Dev
#NULL 25753 883.49
#id 199 49.732 25554 833.75
#year 1 0.385 25553 833.37
#exp 3 12.883 25550 820.48
#id:year 199 22.459 25351 798.03
#id:exp 597 35.043 24754 762.98
#year:exp 3 75.799 24751 687.18
#id:year:exp 595 29.557 24156 657.63
#year:exp:block 16 22.163 24140 635.46
### so the plots aren't a lot better on this model and the PVE (or PDE) by the variables is about the same as in the normal linear model.
### other thing to try might be a mixed effects model with block as a random effect...
```
Plots for tweedie model aren't much better. Also, estimates of effect sizes are really similar. Just stick with the regular linear model despite its shortcomings.
Probably more natural choice is to specify block as a random effect in a mixed effect model. I'm going that direction to estimate BLUPs anyway...
### 4. fixed effect linear models incorporating genetic groups
Genetic groups determined in script 29
```{r full linear models with Kgroups}
dk <-merge(d, kg, all.x=TRUE)
#check models and get pve for all 7 ks
ks <- colnames(dk)[grep("k.", colnames(dk))]
ks <- ks[order(ks)]
k.anovas <- lapply(as.list(ks), function(up.k){
up.lm <- lm(fitness~as.factor(get(up.k))*year*exp + exp:year/block, data=dk)
return(anova(up.lm))
})
k.pve <- sapply(ks, function(up.k){
up.lm <- lm(fitness~as.factor(get(up.k))*year*exp + exp:year/block, data=dk)
return(lm.pve(up.lm))
})
k.pve <- as.data.frame(k.pve)
k.pve$terms <- gsub("as.factor(get(up.k))", "k.group", rownames(k.pve), fixed=TRUE)
k.pve.l <- gather(k.pve, kgroup, pve, k.2:k.8)
kterms <- k.pve[grep("k.group", k.pve$terms),"terms"]
k.pve.plot <- ggplot(k.pve.l[k.pve.l$terms%in%kterms,], aes(x=kgroup, y=pve, group=terms, color=terms)) + geom_line()
ggsave(k.pve.plot, file="./figures/27.figures/pve.by.kgroup.number.pdf", width=6, height=4)
## output model with k=6
lm.k6 <- lm(fitness~as.factor(k.6)*year*exp + exp:year/block, data=dk)
lm.k6.tab <- as.data.frame(anova(lm.k6))
lm.k6.tab$PVE <- lm.pve(lm.k6)
rownames(lm.k6.tab)[8] <- "year:exp/block"
xtab.lm.k6 <- xtable(lm.k6.tab)
print(xtab.lm.k6, file="./figures/27.figures/fixed.effects.k6.anova.table.txt")
```
There is a big jump in pve for kgroup between k=5 and k=6. This reinforces the idea that k=6 is appropriate
```{r by experiment linear models with 6 kgroups}
combos <- expand.grid(unique(d$year), unique(d$exp))
up.kn <- "k.6"
check.pvals.k <- function(d,y,e, up.kn){
up.d <- d[d$year==y & d$exp==e,]
up.lm <- lm(fitness~as.factor(get(up.kn))+block, data=up.d)
return(anova(up.lm)$"Pr(>F)")
}
k6.ps <- apply(combos, 1, function(x){check.pvals.k(d=dk, y=x[1], e=x[2], up.kn=up.kn)}) # all genotypes significant, block sig in all but RAM_2012
k6.ps <-k6.ps[-3,]
k6.ps <- t(k6.ps)
colnames(k6.ps) <- c("p.kgroup", "p.block")
get.pve.k <- function(d, y, e, up.kn){
up.d <- d[d$year==y & d$exp==e,]
up.lm <- lm(fitness~as.factor(get(up.kn))+block, data=up.d)
pve <- lm.pve(up.lm)
return(pve)
}
k6.pve <- apply(combos, 1, function(x){get.pve.k(d=dk, y=x[1], e=x[2], up.kn="k.6")})
k6.pve <- as.data.frame(t(k6.pve))
k6.pve <- cbind(combos, k6.pve)
colnames(k6.pve) <- c("year", "site", "kgroup", "block", "residuals")
k6.pve$site <- gsub("ULL", "SU", k6.pve$site)
k6.pve$site <- gsub("RAT", "SR", k6.pve$site)
k6.pve$site <- gsub("RAM", "NM", k6.pve$site)
k6.pve$site <- gsub("ADA", "NA", k6.pve$site)
k6.pve <- k6.pve[order(k6.pve$site),]
xtab.k6.pve <- xtable(k6.pve)
print(xtab.k6.pve, file="./figures/27.figures/pve.table.kgroup.txt")
```
Experiments with the highest kgroup PVEs are the ones that show locally-adaptive kgroup patterns (see script 29). Expected, but still good to confirm.
### 5. Mixed effect model to get BLUPS
```{r mixed model specifications}
mm1 <- lmer(fitness ~ 1 + (1 | block), data=d)
mm2 <- lmer(fitness ~ 1 + (1 | id), data=d)
mm3 <- lmer(fitness~exp + (1|exp:block), data=d) # so this is correctly specified (without year effect, at least)
mm4 <- lmer(fitness~exp + year + (1|exp:year:block), data=d) ## I think this is also correctly specified!
ranef(mm4)
fixef(mm4)
mm5 <- lmer(fitness~exp*year + (1|exp:year:block), data=d)
anova(mm4, mm5)
ranef(mm5)
fixef(mm5)
mm6 <- lmer(fitness~exp*year + (1|exp:year:block) + (1|id), data=d)
anova(mm5,mm6)
mm7 <- lmer(fitness~exp*year + (1|exp:year:block) + (1|exp:year:id), data=d)
anova(mm6,mm7)
mm8 <- lmer(fitness~exp*year + (1|exp:year:id), data=d)
c2 <- predictInterval(mm7)
int.mm7 <- REsim(mm7)
plotREsim(REsim(mm7))
ranks <- expectedRank(mm7)
VarCorr(mm7)
anova(mm7)
summary(mm7)
# random effect variances
var.mm7 <- VarCorr(mm7)
print(var.mm7,comp=c("Variance","Std.Dev."),digits=4)
re.var <- as.data.frame(var.mm7, row.names = NULL,optional = FALSE)
r.squaredGLMM(mm5)
r.squaredGLMM(mm6)
r.squaredGLMM(mm7)
r.squaredGLMM(mm8)
## R2m is from marginal(fixed effects)
## R2c is from entire model
```
So now let's think about these BLUPs per accession per experiment.
Can we use them to demonstrate local adaptation?
Can they be used for GWAS?
## 5. explore BLUP relationships
```{r explore blups}
blups <- ranef(mm7)[[1]]
blups <- as.data.frame(blups)
desc <- do.call(rbind,strsplit(rownames(blups),":"))
colnames(desc) <- c("exp","year","id")
blups <- cbind(blups, desc)
colnames(blups)[1] <- "fb"
hist(blups$fb)
pdf(file="./figures/27.figures/blup.fitness.boxplot.by.year.site.pdf", width=10, height=6)
with(blups, boxplot(fb~year+exp, col=c(rep(brewer.pal(9, "Paired")[4],4), rep(brewer.pal(9, "Paired")[3],4))))
dev.off()
## write BLUPS to file
write.csv(blups, file="./data/27.data/marginal.blups.csv", quote=FALSE,row.names=FALSE)
## save model to file
save(mm7, file="./data/27.data/mm7.final.mixed.model.Rdat")
```
```{r load admixture results, eval=FALSE}
# parsed in 26.parse.redone.admixture.Rmd
# depreciated
a.group <- read.csv("./data/complete.admixture.groups.csv", stringsAsFactors = FALSE)
```
```{r add fitness blups plot rxn norms}
acc.fit.rxn <- blups
acc.fit.rxn <- merge(acc.fit.rxn, a.group[,c(1:3,17)], all.x=TRUE)
acc.fit.rxn$a.group <- as.factor(acc.fit.rxn$a.group)
acc.fit.rxn$site.complex <- paste(acc.fit.rxn$exp, acc.fit.rxn$year, sep="_")
acc.fit.rxn$group.names <- as.factor(acc.fit.rxn$group.names)
acc.fit.rxn$group.names = factor(acc.fit.rxn$group.names,levels(acc.fit.rxn$group.names)[c(5,3,6,4,2,7,1)])
pdf(file="./figures/27.figures/reaction.norms.blups.pdf", width=8, height=5)
rxn.norms <- ggplot(acc.fit.rxn, aes(site.complex,fb, group=id)) +
geom_line(aes(colour=group.names)) +
scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") +
xlab("Site_year") +
ylab("fitness BLUPS")+
guides(color = guide_legend(override.aes = list(size = 3)))
print(rxn.norms)
dev.off()
```
```{r pairwise correlations between blups, eval=FALSE}
## need to reformat blups to long format
blups <- read.csv(file="./data/27.data/marginal.blups.csv", stringsAsFactors=FALSE)
blups.l <- blups %>% pivot_wider(id_cols=id,names_from=c(exp,year),values_from=fb)%>%as.data.frame()
colnames(blups.l) <- c("id","NA_2011","NA_2012","NM_2011", "NM_2012","SR_2011", "SR_2012","SU_2011","SU_2012")
pdf("./figures/27.figures/correlation.fitness.blups.pdf", width=8, height=8)
corrplot(cor(blups.l[,2:9], use="na.or.complete"), method="ellipse", type="upper", diag=FALSE)
dev.off()
my_cor <- cor(blups.l[,2:9], use="na.or.complete")
colfunc<-colorRampPalette(c("red","white","royalblue"))
#heatmaply_cor(my_cor, colors= colfunc(100),cellnote=my_cor,cellnote_textposition="middle center")
### since these are interactive plots in plotly, they are hard to save statically, so I saved this one manually from the window
#file="./figures/correlation.dendrogram.fitness.blups.jpeg")
#try using egg package
##ggheatmap(
#mtcars,
#scale = "column",
#row_side_colors = mtcars[, c("cyl", "gear")]
#)
ns.site <- as.data.frame(c(rep("North",4),rep("South",4)))
ns.palette = c("North" = "green2", "South" = "dodgerblue2")
colnames(ns.site) <- "location"
ns.cols <- c(rep("green2",4), rep("dodgerblue2", 4))
mycolors <- colorRampPalette(c("blue", "lightyellow1", "red"))
heatmap.2(my_cor, trace = "none", col = mycolors)
my_cor_na <- my_cor
rna <- which(my_cor==1)
my_cor_na[rna] <- NA
pdf(file="./figures/27.figures/blup.correlation.heatmap.pdf", width=10, height=9)
heatmap.2(my_cor, trace = "none", col = mycolors, margins=c(6.5,6.5), colsep=c(3,5), rowsep=c(3,5),ColSideColors=ns.cols, RowSideColors=ns.cols)
dev.off()
```
```{r blup values by experiment and admixture group}
ga.long.short <- acc.fit.rxn
ga.long.short$exp <- gsub("ADA","NA", ga.long.short$exp)
ga.long.short$exp <- gsub("RAM","NM", ga.long.short$exp)
ga.long.short$exp <- gsub("RAT","SR", ga.long.short$exp)
ga.long.short$exp <- gsub("ULL","SU", ga.long.short$exp)
### remove admixture groups with only one accession
acc.tab <- table(ga.long.short$group.names)
ga.long.short <- ga.long.short[ga.long.short$group.names%in%names(acc.tab[acc.tab>8]),]
ga.long.short$group.names = droplevels(ga.long.short$group.names)
ga.long.short$group.names <- factor(ga.long.short$group.names, level=c("C.Europe","S.Sweden", "N.Sweden","admixed"))
ad.group.short.boxplot <- ggplot(ga.long.short, aes(x=group.names, y=fb, fill=as.factor(group.names))) +
geom_violin() +
geom_boxplot(width=0.1, show.legend=FALSE)+
xlab("admixture group")+
facet_grid(year~exp) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_fill_manual(name = "admixture group", values=brewer.pal(9, "Paired")[c(8,4,3,1)]) +
ylab("fitness BLUPS")
pdf(file="./figures/27.figures/fitness.blups.by.admix.group.short.names.pdf", width=10, height=6)
print(ad.group.short.boxplot)
dev.off()
```
### reaction norms between years, same site
```{r ind rxn norms}
#test.sub <- acc.fit.rxn[acc.fit.rxn$year=="2011" & acc.fit.rxn$exp%in%c("RAT","ULL"),]
ada.sub <- acc.fit.rxn[acc.fit.rxn$exp%in%c("ADA"),]
ram.sub <- acc.fit.rxn[acc.fit.rxn$exp%in%c("RAM"),]
ull.sub <- acc.fit.rxn[acc.fit.rxn$exp%in%c("ULL"),]
rat.sub <- acc.fit.rxn[acc.fit.rxn$exp%in%c("RAT"),]
ada.rn <- ggplot(ada.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPS") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
ram.rn <- ggplot(ram.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPS") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
ull.rn <- ggplot(ull.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPS") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
rat.rn <- ggplot(rat.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPS") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
grid.arrange(ada.rn, ram.rn, ull.rn, rat.rn, nrow = 2)
pdf(file="./figures/27.figures/site.rxn.norms.pdf", width=11, height=7)
grid.arrange(ada.rn, ram.rn, ull.rn, rat.rn, nrow = 2)
dev.off()
```
### reaction norms between sites, 2011
```{r 2011 rxn norms}
up.year <- "2011"
au.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("ADA","ULL"),]
at.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("ADA","RAT"),]
ru.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("RAM","ULL"),]
rt.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("RAM","RAT"),]
ar.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("ADA","RAM"),]
ut.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("ULL","RAT"),]
au.rn <- ggplot(au.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
at.rn <- ggplot(at.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
ru.rn <- ggplot(ru.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
rt.rn <- ggplot(rt.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
ar.rn <- ggplot(ar.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
ut.rn <- ggplot(ut.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
grid.arrange(au.rn, at.rn, ru.rn, rt.rn, ar.rn, ut.rn, nrow = 3)
pdf(file="./figures/27.figures/2011.rxn.norms.pdf", width=9, height=11)
grid.arrange(au.rn, at.rn, ru.rn, rt.rn, ar.rn, ut.rn, nrow = 3)
dev.off()
```
### reaction norms between sites, 2012
```{r 2012 rxn norms}
up.year <- "2012"
au.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("ADA","ULL"),]
at.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("ADA","RAT"),]
ru.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("RAM","ULL"),]
rt.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("RAM","RAT"),]
ar.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("ADA","RAM"),]
ut.sub <- acc.fit.rxn[acc.fit.rxn$year==up.year & acc.fit.rxn$exp%in%c("ULL","RAT"),]
au.rn <- ggplot(au.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
at.rn <- ggplot(at.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
ru.rn <- ggplot(ru.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
rt.rn <- ggplot(rt.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
ar.rn <- ggplot(ar.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
ut.rn <- ggplot(ut.sub, aes(site.complex,fb, group=id)) + geom_line(aes(colour=group.names)) + scale_color_manual(values=group.colors[c(1,2,3,4,7,9,8)],name = "admixture group") + xlab("Site_year") +ylab("fitness BLUPs") + scale_x_discrete(expand = c(0.05, 0.05)) + theme_light()
grid.arrange(au.rn, at.rn, ru.rn, rt.rn, ar.rn, ut.rn, nrow = 3)
pdf(file="./figures/27.figures/2012.rxn.norms.pdf", width=9, height=11)
grid.arrange(au.rn, at.rn, ru.rn, rt.rn, ar.rn, ut.rn, nrow = 3)
dev.off()
```
### patterns of significance and effect by line - per site and year
```{r sig pattern}
line.est <- int.mm7[int.mm7$groupFctr=="exp:year:id",] ### 2 of 1600 missing
## does CI contain zero?
line.est$ci.low <- line.est$mean-line.est$sd
line.est$ci.hi <- line.est$mean+line.est$sd
line.est$sig <- apply(line.est,1, function(x){(as.numeric(x[7])<0 & as.numeric(x[8])>0)==FALSE})
### 39% of CIs don't contain zero
line.est$mean.sig <- line.est$mean*line.est$sig
## reshape dataframe
tmp.ids <- do.call(rbind, sapply(line.est$groupID, function(x){strsplit(x,":")}))
tmp.ids <- data.frame(tmp.ids)
colnames(tmp.ids) <- c("site","year", "id")
line.est <- cbind(line.est, tmp.ids)
line.est$exp <- paste(line.est$site, line.est$year, sep="_")
test <- line.est[,c(10,13,14)]
test <- spread(test, exp, mean.sig)
write.table(line.est, file="./data/27.data/line.fitness.estimate.significance.txt")
```