-
Notifications
You must be signed in to change notification settings - Fork 1
/
Abundance_plots_metabarcoding.Rmd
268 lines (201 loc) · 10.4 KB
/
Abundance_plots_metabarcoding.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
---
title: "Abundance_metabarcoding_090519"
author: "Georgina Brennan"
date: "5/10/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Load packages
```{r, results='hide',message=FALSE}
library(ggplot2)
library(reshape2)
library(tidyr)
#library(tidyverse)
library(RColorBrewer)
#library(cowplot)
#library(grid)
library(plyr)
```
load tree height metabarcoding count data and the mapping file or metadata
```{r}
OTUdata<-read.csv("bact_alldata_taxatable_wTax.csv", row.names = 1, check.names =FALSE)
str(OTUdata)
meta<-read.table("bact_alldata_mapfile.txt")
names(meta)
```
Remove taxonomy before transposing the data
```{r}
OTUdata$taxonomy<-NULL
```
Transpose the data to have sample names on rows
```{r}
OTUt<-as.data.frame(t(OTUdata))
```
Apply proportion normalisation
```{r}
OTUdata<-OTUt/rowSums(OTUt)
OTUdata<-OTUdata[,order(colSums(OTUdata),decreasing=TRUE)]
```
#Extract list of top N Taxa
```{r}
N<-20
taxa_list<-colnames(OTUdata)[1:N]
N<-length(taxa_list)
```
Generate a new table with everything in the top N list
```{r}
abundOTU<-data.frame(OTUdata[,colnames(OTUdata) %in% taxa_list])
names(abundOTU)
```
Decide how you want to filter your data for visualization
Here I am looking at the most the most abundant OTUS - top 20 abundant OTUs across all sampling ID's
```{r}
abundOTU$Index<-rownames(abundOTU)
OTUt.prop <- merge(meta, abundOTU, by.y = 'Index', by.x = "SampleNo", all=FALSE)
# check for errors in merging data by looking for missing data
sum(is.na(OTUt.prop$Project)) # 0 missing data
```
make data frame into a long format for plotting and statistical analysis
Here we have the OTUs in columns 15 to the Nth column in the data.frame. This should be edited if you change the input file
```{r}
names(OTUt.prop)
OTU.long <- gather(OTUt.prop, Taxa, proportion, 15:34, factor_key=TRUE)
names(OTU.long)
```
Read in the tax ID - we removed this at the begining before we tranposed the data
```{r}
taxalist <- read.csv("taxalist.csv", header=T)
```
Merge the tax ID with the data based on the OTU ID
```{r}
OTU.long<-merge(OTU.long, taxalist, by.x = "Taxa", by.y = "OTU")
```
TIP: if you have a time series with dates that you want to plot use the following code using the as.Date function
# > df$Date <- as.Date(df$Date, "%d/%m/%Y")
```{r}
```
Load some pretty colours
```{r}
colours29 <- c("#404142", "#2f4b7c", "#a05195", "#ff7c43", "#665191", "#e0e084",
"#c68d23", "#8DD3C7", "#9894ae", "#FB8072", "#80B1D3", "#f95d6a", "#B3DE69", "#FCCDE5",
"#D9D9D9", "#BC80BD", "#CCEBC5", "#d45087", "#FFED6F", "#1F78B4", "#33A02C", "#FB9A99",
"#FDBF6F", "#FF7F00", "#a51213", "#CAB2D6", "#77a070", "#B15928", "#003f5c")
```
Start plotting data
1) abundance graph to visualise the abundant taxa across different tree heights.
```{r}
P1<-ggplot(OTU.long,aes(Vertposition,proportion,fill=Order, col=Order)) +
geom_density( stat = "identity", alpha = 0.2)+
#geom_line(data = df.data, aes(x = Date2, y= counts, col = Site), linetype = "dashed")+
xlab("Vertical Hieght (m)")+
ylab("Proportion of Sequence Reads")+
#ylim(0,4.5e+06)+
facet_wrap( . ~ transecttree, ncol=2, scales = "free_y") +
theme_bw() + # remove grey background
#theme(legend.position="none")+
scale_fill_manual(values= colours29)+
scale_colour_manual(values= colours29)+
theme(panel.grid.minor = element_blank())+ # remove minor lines on plot
theme(panel.grid.major = element_blank())+ # remove major lines on plot
theme(axis.text.x = element_text(colour="grey20",size=12,angle=0,hjust=.5,vjust=.5,face="plain"), #horizontal axis text, grey, size 16, no angle etc.
axis.text.y = element_text(colour="grey20",size=12,angle=0,hjust=1,vjust=0,face="plain"), #vertical axis text, grey, size 16, no angle etc.
axis.title.x = element_text(colour="grey20",size=14,angle=0,hjust=.5,vjust=0,face="plain"), #horizontal axis label, grey, size 20, no angle etc.
axis.title.y = element_text(colour="grey20",size=14,angle=90,hjust=.5,vjust=.5,face="plain")) #vertical axis label, grey, size 20, no angle etc.
P1
```
2) Have a look at the biological replicates - one option is to use the facet function:
```{r}
P2<-ggplot(OTU.long,aes(Vertposition,proportion,fill=Order, col=Order)) +
geom_density( stat = "identity", alpha = 0.2)+
#geom_line(data = df.data, aes(x = Date2, y= counts, col = Site), linetype = "dashed")+
xlab("Vertical Hiegh (m)")+
ylab("Proportion of Sequence Reads")+
# ylim(0,1)+
facet_grid(transecttree ~ Order, scales = "free") +
theme_bw() + # remove grey background
theme(legend.position="none")+
theme(strip.text.x = element_text(size=8, angle=75),
strip.text.y = element_text(size=12, face="bold"),
strip.background = element_rect(colour="black", fill="#CCCCFF"))+
scale_fill_manual(values= colours29)+
scale_colour_manual(values= colours29)+
theme(panel.grid.minor = element_blank())+ # remove minor lines on plot
theme(panel.grid.major = element_blank())+ # remove major lines on plot
theme(axis.text.x = element_text(colour="grey20",size=8,angle=0,hjust=.5,vjust=.5,face="plain"), #horizontal axis text, grey, size 16, no angle etc.
axis.text.y = element_text(colour="grey20",size=8,angle=0,hjust=1,vjust=0,face="plain"), #vertical axis text, grey, size 16, no angle etc.
axis.title.x = element_text(colour="grey20",size=12,angle=0,hjust=.5,vjust=0,face="plain"), #horizontal axis label, grey, size 20, no angle etc.
axis.title.y = element_text(colour="grey20",size=12,angle=90,hjust=.5,vjust=.5,face="plain")) #vertical axis label, grey, size 20, no angle etc.
P2
```
This data contains 10 biological replicates (samples collected from 10 different trees).
Using the summarize function here we calculated the average proportion of sequences, the variance and standard deviation at each height across all biolgical replicates.
```{r}
## summary statistcs
names(OTU.long)
str(OTU.long)
summary<-ddply(OTU.long, .(Vertposition, Order), summarize,
average.proportion = mean(proportion, na.rm=TRUE), sd.proportion = (sd(proportion, na.rm=TRUE)),var.proportion = (var(proportion)))
str(summary)
```
3) Plot summary statistics: average ± the biological variation
Here we can see that most orders found a lower vertical distances have similar proportion across all replicate trees.
```{r}
P3<-ggplot(summary,aes(x = Vertposition, y = average.proportion,fill=Order, col=Order, group = Order)) +
geom_point(shape = 21, size = 2, alpha = 0.3, position = position_dodge(width = 0.3))+
#facet_wrap( ~ site, ncol=3, scales = "free_y")+
geom_errorbar(data = summary, aes(x = Vertposition, y = average.proportion, ymin = average.proportion - var.proportion, ymax = average.proportion + var.proportion, width = 0.7),col = "black", position = position_dodge(width = 0.3)) +
#geom_errorbar(aes(x = Vertposition, y = average.proportion, ymin = average.proportion - var.proportion, ymax = average.proportion + var.proportion, group = Taxa), col = "black", position = position_dodge(width = 0.3)) +
xlab("Vertical Height")+
ylab("Proportion of Sequence reads")+
theme_bw() + # remove grey background
#theme(legend.position="none")+
theme(panel.grid.minor = element_blank())+ # remove minor lines on plot
theme(panel.grid.major = element_blank())+ # remove major lines on plot
theme(axis.text.x = element_text(colour="grey20",size=12,angle=0,hjust=.5,vjust=.5,face="plain"), #horizontal axis text, grey, size 16, no angle etc.
axis.text.y = element_text(colour="grey20",size=12,angle=0,hjust=1,vjust=0,face="plain"), #vertical axis text, grey, size 16, no angle etc.
axis.title.x = element_text(colour="grey20",size=14,angle=0,hjust=.5,vjust=0,face="plain"), #horizontal axis label, grey, size 20, no angle etc.
axis.title.y = element_text(colour="grey20",size=14,angle=90,hjust=.5,vjust=.5,face="plain")) #vertical axis label, grey, size 20, no angle etc.
P3
```
Here, even with a subset of the data, we can plot the mean variance relationship which shows a postive relationship between the mean proportion of sequences and the variance of the proportion of sequences across all samples i.e. there are many zeros and many low abundant taxa therefore producing lower variance and few taxa present at high proportions. This also suggests that the whole dataset best fits a negative binomial distribution due to many zeros in the dataset, which is common for abundance data.
Simple linear model to support the mean and variance relationship:
```{r}
m1<-lm(log(average.proportion) ~ log(var.proportion), data = summary)
anova(m1)
summary(m1)
```
Make a funtion to add the model to the plot
```{r}
lm_eqn = function(m1) {
l <- list(a = format(coef(m1)[1], digits = 2),
b = format(abs(coef(m1)[2]), digits = 2),
r2 = format(summary(m1)$r.squared, digits = 2));
if (coef(m1)[2] >= 0) {
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
} else {
eq <- substitute(italic(y) == a - b %.% italic(x)*","~~italic(r)^2~"="~r2,l)
}
as.character(as.expression(eq));
}
```
4) Creat a mean variance plot
```{r}
P4<-ggplot(summary,aes(x = log(average.proportion), y = log(var.proportion),fill=Order, col=Order, group = Order)) +
geom_point(shape = 21, size = 2, alpha = 0.3)+
#geom_smooth(method = "lm", se = FALSE)+
xlab("Mean proportion")+
ylab("Variation of the proportion")+
geom_text(aes(x = -6, y = 0, label = lm_eqn(lm(log(average.proportion) ~ log(var.proportion)))), parse = TRUE, col = "black")+
annotate("text", x = -7, y = -2, label="p < 0.0001" )+
theme_bw() + # remove grey background
#theme(legend.position="none")+
theme(panel.grid.minor = element_blank())+ # remove minor lines on plot
theme(panel.grid.major = element_blank())+ # remove major lines on plot
theme(axis.text.x = element_text(colour="grey20",size=12,angle=0,hjust=.5,vjust=.5,face="plain"), #horizontal axis text, grey, size 16, no angle etc.
axis.text.y = element_text(colour="grey20",size=12,angle=0,hjust=1,vjust=0,face="plain"), #vertical axis text, grey, size 16, no angle etc.
axis.title.x = element_text(colour="grey20",size=14,angle=0,hjust=.5,vjust=0,face="plain"), #horizontal axis label, grey, size 20, no angle etc.
axis.title.y = element_text(colour="grey20",size=14,angle=90,hjust=.5,vjust=.5,face="plain")) #vertical axis label, grey, size 20, no angle etc.
P4
```