-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_F_SumSquareAnalysis.R
281 lines (220 loc) · 9.28 KB
/
1_F_SumSquareAnalysis.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
# This script will combine the analysis and create final figures for my final combined data.
getwd() # PLEASE CHANGE WD
setwd("/home/coreyschultz/1.Projects/2.Maize.Endophyte.Project/Final_Data_and_Figs")
getwd()
library(tidyr)
library(tidyverse)
library(broom)
library(ggplot2)
library(car)
library(data.table)
library(ggpubr)
Herb_data = read.csv("Final_Herb_Data.csv", sep =",")
Burk_data = read.csv("Final_Burk_Data.csv", sep =",")
Serendip_data = read.csv("Final_Sbecsii_Data.csv", sep =",")
names(Herb_data) <- c("Genotype", "Condition", "Rep", "Endophyte","Chlorophyll1","Chlorophyll2","Chlorophyll3",
"PlantHeight","LeafArea","RootLength","RootVolume","Group_or_Date")
names(Burk_data) <- c("Genotype", "Condition", "Rep", "Endophyte",
"PlantHeight","LeafArea","RootLength","RootVolume")
names(Serendip_data) <- c("Genotype", "Condition", "Rep", "Endophyte",
"PlantHeight","RootLength","RootMass","ShootMass")
Serendip_data$Condition <- gsub('C', 'Control', Serendip_data$Condition)
Serendip_data$Condition <- gsub('I', 'Inoculated', Serendip_data$Condition)
Herb_data$Condition <- gsub('C', 'Control', Herb_data$Condition)
Herb_data$Condition <- gsub('I', 'Inoculated', Herb_data$Condition)
Burk_data$Condition <- gsub('C', 'Control', Burk_data$Condition)
Burk_data$Condition <- gsub('I', 'Inoculated', Burk_data$Condition)
# number of variables in our model + residuals
n = 4
# Serendipita #Drop rep
SereResultsT1 <- data.frame()
SereResultsT1 <- data.frame(PlantHeight=numeric(n),RootLength=numeric(n),RootMass=numeric(n),ShootMass=numeric(n))
rownames(SereResultsT1) <- c("Genotype","Inoculation","Genotype:Inoculation", "Residuals")
myvars <- names(Serendip_data[5:8]) # create a list of traits
Signif_list <- list()
for( m in myvars){
print(m)
print(as.name(m))
linmod <- lm(Serendip_data[[m]] ~ Genotype + Condition + Genotype:Condition, data = Serendip_data)
HPH1 <- anova(linmod)
print(HPH1)
SereResultsT1[[m]] <- c(HPH1[1,2],HPH1[2,2],HPH1[3,2],HPH1[4,2])
itlist = HPH1[,5]
Signif_list <- append(Signif_list, itlist)
}
#Get rid of the NAs from residuals
Signif_list[is.na(Signif_list)] = 1
Signif_list
# Convert these p values to *
for( s in 1:length(Signif_list)){
#print(s)
if (Signif_list[s] < .05){
Signif_list[s] <- '*'
#print("True")
} else {
Signif_list[s] <- " "
#print("False")
}
}
Signif_list
# Now Normalize all the columns
ST1 <- SereResultsT1
ST1[] <- lapply(ST1[], function(x) x/sum(x))
setDT(ST1, keep.rownames = TRUE)[]
SereT1_long <- ST1 %>%
gather(ST1, value,PlantHeight:ShootMass)
SereT1_long$Signif <- Signif_list
# Herbaspirillum
# Average Chlorophyll together
Herb_data$Chlorophyll <- rowMeans(Herb_data[ , c("Chlorophyll1","Chlorophyll2","Chlorophyll3")], na.rm=TRUE)
HerbResultsT1 <- data.frame(Chlorophyll=numeric(n),PlantHeight=numeric(n),LeafArea=numeric(n),RootLength=numeric(n),RootVolume=numeric(n))
rownames(HerbResultsT1) <- c("Genotype","Inoculation","Genotype:Inoculation", "Residuals")
herb_cols <- c(13, 8:11)
myvars <- names(Herb_data[,herb_cols]) # create a list of traits
Signif_list <- list()
for( m in myvars){
print(m)
print(as.name(m))
linmod <- lm(Herb_data[[m]] ~ Genotype + Condition + Genotype:Condition, data = Herb_data)
HPH1 <- anova(linmod)
print(HPH1)
HerbResultsT1[[m]] <- c(HPH1[1,2],HPH1[2,2],HPH1[3,2],HPH1[4,2])
itlist = HPH1[,5]
Signif_list <- append(Signif_list, itlist)
}
#Get rid of the NAs from residuals
Signif_list[is.na(Signif_list)] = 1
Signif_list
# Convert these p values to *
for( s in 1:length(Signif_list)){
#print(s)
if (Signif_list[s] < .05){
Signif_list[s] <- '*'
#print("True")
} else {
Signif_list[s] <- " "
#print("False")
}
}
Signif_list
# Now Normalize all the columns
HerbT1 <- HerbResultsT1
HerbT1[] <- lapply(HerbT1[], function(x) x/sum(x))
setDT(HerbT1, keep.rownames = TRUE)[]
HerbT1_long <- HerbT1 %>%
gather(HerbT1, value,Chlorophyll:RootVolume)
HerbT1_long$Signif <- Signif_list
# Burkholderia
Burk_data$RootLength <- as.numeric(Burk_data$RootLength)
Burk_data$RootVolume <- as.numeric(Burk_data$RootVolume)
burkResultsT1 <- data.frame(PlantHeight=numeric(n),LeafArea=numeric(n),RootLength=numeric(n),RootVolume=numeric(n))
rownames(burkResultsT1) <- c("Genotype","Inoculation","Genotype:Inoculation", "Residuals")
myvars <- names(Burk_data[5:8]) # create a list of traits
Signif_list <- list()
for( m in myvars){
print(m)
print(as.name(m))
linmod <- lm(Burk_data[[m]] ~ Genotype + Condition + Genotype:Condition, data = Burk_data)
HPH1 <- anova(linmod)
print(HPH1)
burkResultsT1[[m]] <- c(HPH1[1,2],HPH1[2,2],HPH1[3,2],HPH1[4,2])
itlist = HPH1[,5]
Signif_list <- append(Signif_list, itlist)
}
#Get rid of the NAs from residuals
Signif_list[is.na(Signif_list)] = 1
Signif_list
# Convert these p values to *
for( s in 1:length(Signif_list)){
#print(s)
if (Signif_list[s] < .05){
Signif_list[s] <- '*'
#print("True")
} else {
Signif_list[s] <- " "
#print("False")
}
}
Signif_list
# Now Normalize all the columns
BurkT1 <- burkResultsT1
BurkT1[] <- lapply(BurkT1[], function(x) x/sum(x))
setDT(BurkT1, keep.rownames = TRUE)[]
BurkT1_long <- BurkT1 %>%
gather(BurkT1, value,PlantHeight:RootVolume)
BurkT1_long$Signif <- Signif_list
# Endophytes included in Name: don't like that but without empty faceting
SereT1_long["Endophyte"] <- "Experiment 3 \n (Serendipita)"
HerbT1_long["Endophyte"] <- "Experiment 1 \n (Herbaspirillum)"
BurkT1_long["Endophyte"] <- "Experiment 2 \n (Burkholderia)"
names(SereT1_long)[names(SereT1_long) == "ST1"] <- "Phenotype"
names(HerbT1_long)[names(HerbT1_long) == "HerbT1"] <- "Phenotype"
names(BurkT1_long)[names(BurkT1_long) == "BurkT1"] <- "Phenotype"
SandHandB <- rbind(HerbT1_long,BurkT1_long,SereT1_long)
cbPalette <- c("#999999", "forestgreen", "tan3", "#56B4E9", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# Add spaces to phenotypes
SandHandB$Phenotype[SandHandB$Phenotype == "PlantHeight"] <- "Plant Height"
SandHandB$Phenotype[SandHandB$Phenotype == "LeafArea"] <- "Leaf Area"
SandHandB$Phenotype[SandHandB$Phenotype == "RootLength"] <- "Root Length"
SandHandB$Phenotype[SandHandB$Phenotype == "RootVolume"] <- "Root Volume"
SandHandB$Phenotype[SandHandB$Phenotype == "ShootMass"] <- "Shoot Mass"
SandHandB$Phenotype[SandHandB$Phenotype == "RootMass"] <- "Root Mass"
# Change names in legends to fit the manuscript text
SandHandB$rn[SandHandB$rn == "Genotype"] <- "Maize Genotype"
SandHandB$rn[SandHandB$rn == "Inoculation"] <- "Endophyte Inoculation"
SandHandB$rn[SandHandB$rn == "Genotype:Inoculation"] <- "Genome-by-Genome Interaction"
# reorder legend
SandHandB$rn <- factor(SandHandB$rn, levels = c("Maize Genotype","Genome-by-Genome Interaction","Endophyte Inoculation","Residuals"))
# why do I need to use another vjust? cus its turned sideways? that is stupid
ggplot(SandHandB, aes(x = Phenotype, y = value, fill = forcats::fct_rev(rn), label = Signif)) +
geom_col(position=position_stack()) + theme(axis.text.x = element_text( size = 14)) +
theme(axis.text.y = element_text(size = 14)) +
labs(fill = "Variables") + geom_text(aes(label = Signif), size = 10, position = position_stack(vjust = 0.5),
vjust = .75)+
xlab("Measured Phenotypes") + coord_flip() +
ylab("Proportion of SS") + scale_fill_manual(values=cbPalette) +
theme(panel.background = element_rect(fill = "white",
colour = "white"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "white")) +
facet_grid(rows = vars(Endophyte), drop = TRUE, space = "free", scales = "free", switch = NULL) +
theme(legend.position = "right") + theme(axis.line = element_line(colour = "white"),
panel.border = element_blank()) +
theme(legend.title = element_text(size=18)) + theme(legend.text = element_text(size=14)) +
theme(strip.text.y = element_text(size = 14)) + theme(plot.title = element_text(size=20)) +
theme(axis.title.x = element_text(30)) + theme(axis.title.y = element_text(20)) +
guides(fill = guide_legend(reverse = TRUE))
# Need to change Genotype: Inoculation name - just changed column names above
SandHandB <- as.data.frame(lapply(SandHandB, function(x) {
gsub("Genotype:Inoculation", "GenxInoc", x)
}))
# Calculate total variance due to genotype
SandHandB
# Need to change Genotype: Inoculation name - just changed column names above
SandHandB <- as.data.frame(lapply(SandHandB, function(x) {
gsub("Genotype:Inoculation", "GenxInoc", x)
}))
count = 0.0
sum = 0.0
for (row in 1:nrow(SandHandB)){
if(grepl("GenxInoc", SandHandB[row, 1])){
count = count + 1
sum = sum + as.numeric(as.character(SandHandB[row,3]))
#print(SandHandB[row,3])
}}
interaction_avg = (sum/count)
print(interaction_avg)
count = 0.0
sum = 0.0
for (row in 1:nrow(SandHandB)){
if(grepl("Genotype", SandHandB[row, 1])){
count = count + 1
sum = sum + as.numeric(as.character(SandHandB[row,3]))
#print(SandHandB[row,3])
}}
Genotype_avg = (sum/count)
print(Genotype_avg)
print("Interaction over Genotype")
print(interaction_avg / Genotype_avg)