-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path04-MAW-PIII.Rmd
219 lines (133 loc) · 6.69 KB
/
04-MAW-PIII.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
---
title: "OPEN & REPRODUCIBLE MICROBIOME DATA ANALYSIS SPRING SCHOOL 2018"
author: "Sudarshan"
date: "`r Sys.Date()`"
output: bookdown::gitbook
site: bookdown::bookdown_site
---
# Composition plots
Barplots are a one way of visualising the composition of your samples.
We will use the filtered phyloseq object from **Set-up and Pre-processing** section.
**Load packages**
```{r, warning=FALSE, message=FALSE}
library(microbiome) # data analysis and visualisation
library(phyloseq) # also the basis of data object. Data analysis and visualisation
library(RColorBrewer) # nice color options
library(ggpubr) # publication quality figures, based on ggplot2
library(dplyr) # data handling
```
```{r}
ps1 <- readRDS("./phyobjects/ps.ng.tax.rds")
# use print option to see the data saved as phyloseq object.
print(ps1)
```
## Barplot counts
```{r, fig.height= 6, fig.width=20, warning=FALSE, message= FALSE}
ps1.com <- ps1
# if you have dada2/deblur output and sequences as taxa names, then you can change them as follows
taxa_names(ps1.com) <- paste0("ASV_", rownames(tax_table(ps1.com)))
# We need to set Palette
taxic <- as.data.frame(ps1.com@tax_table) # this will help in setting large color options
# colourCount = length(unique(taxic$Family)) #define number of variable colors based on number of Family (change the level accordingly to phylum/class/order)
# getPalette = colorRampPalette(brewer.pal(12, "Paired")) # change the palette as well as the number of colors will change according to palette.
taxic$OTU <- rownames(taxic) # Add the OTU ids from OTU table into the taxa table at the end.
colnames(taxic) # You can see that we now have extra taxonomy levels.
taxmat <- as.matrix(taxic) # convert it into a matrix.
new.tax <- tax_table(taxmat) # convert into phyloseq compatible file.
tax_table(ps1.com) <- new.tax # incroporate into phyloseq Object
# now edit the unclassified taxa
tax_table(ps1.com)[tax_table(ps1.com)[, "Family"] == "", "Family"] <- "Unclassified family"
# it would be nice to have the Taxonomic names in italics.
# for that we set this
guide_italics <- guides(fill = guide_legend(label.theme = element_text(
size = 15,
face = "italic", colour = "Black", angle = 0
)))
## Now we need to plot at family level, we can do it as follows:
# first remove the phy_tree
ps1.com@phy_tree <- NULL
# Second merge at family level
ps1.com.fam <- microbiome::aggregate_top_taxa(ps1.com, "Family", top = 10)
plot.composition.COuntAbun <- plot_composition(ps1.com.fam) + theme(legend.position = "bottom") +
scale_fill_brewer("Family", palette = "Paired") + theme_bw() +
theme(axis.text.x = element_text(angle = 90)) +
ggtitle("Relative abundance") + guide_italics + theme(legend.title = element_text(size = 18))
plot.composition.COuntAbun
#ggsave("./Test_Outputfiles/Family_barplot_CountAbundance.pdf", height = 6, width = 8)
```
This plot is based on the reads per sample. In the next step, we plot the relative abundance.
## Barplot relative abundance
Make it relative abundance
```{r, fig.height= 6, fig.width=20, warning=FALSE, message= FALSE}
# the previous pseq object ps1.com.fam is only counts.
# Use traqnsform function of microbiome to convert it to rel abun.
ps1.com.fam.rel <- microbiome::transform(ps1.com.fam, "compositional")
plot.composition.relAbun <- plot_composition(ps1.com.fam.rel,
sample.sort = "scientific_name",
x.label = "env_material")
plot.composition.relAbun <- plot.composition.relAbun + theme(legend.position = "bottom")
plot.composition.relAbun <- plot.composition.relAbun + scale_fill_brewer("Family", palette = "Paired") + theme_bw()
plot.composition.relAbun <- plot.composition.relAbun + theme(axis.text.x = element_text(angle = 90))
plot.composition.relAbun <- plot.composition.relAbun + ggtitle("Relative abundance") + guide_italics + theme(legend.title = element_text(size = 18))
print(plot.composition.relAbun)
#ggsave("./figures/Family_barplot_RelAbundance.pdf", height = 6, width = 8)
```
### Barplot customize
```{r}
data.com <- plot.composition.relAbun$data
colnames(data.com)
```
```{r}
p.com <- ggplot(data.com, aes(x = Sample, y = Abundance, fill = Tax))
p.com <- p.com + geom_bar(position = "stack", stat = "identity")
p.com <- p.com + scale_x_discrete(labels = data.com$xlabel, breaks = data.com$Sample)
p.com <- p.com + facet_grid(~xlabel, scales = "free") + theme_bw()
p.com <- p.com + scale_fill_brewer("Family", palette = "Paired")
p.com <- p.com + rremove("x.text")
ggsave("./figures/Composition plots.pdf", height = 4, width = 6)
```
For more information [Microbiome tutorial](http://microbiome.github.io/microbiome/Composition.html)
## Heatmaps
These are a good alternative to barplots (if done right).
```{r}
# base plot
p.heat <- ggplot(data.com, aes(x = Sample, y = Tax)) + geom_tile(aes(fill = Abundance))
# Change color
p.heat <- p.heat + scale_fill_distiller("Abundance", palette = "RdYlBu") + theme_bw()
# Make bacterial names italics
p.heat <- p.heat + theme(axis.text.y = element_text(colour = 'black',
size = 10,
face = 'italic'))
# Make seperate samples based on main varaible
p.heat <- p.heat + facet_grid(~xlabel,
scales = "free") + rremove("x.text")
p.heat <- p.heat + ylab("Family")
#Clean the x-axis
p.heat <- p.heat + theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
# Clean the facet label box
p.heat <- p.heat + theme(legend.key = element_blank(),
strip.background = element_rect(colour="black", fill="white"))
print(p.heat)
ggsave("./figures/Heatmap.pdf", height = 4, width = 6)
# + geom_text(aes(label = round(Abundance)), size = 0.4)
```
**Extra**
Following is an example of customizing the plot using ggpubr.
```{r}
# we use count data at family level from the barplot for counts
ps_df <- microbiomeutilities::phy_to_ldf(ps1.com.fam,
transform.counts = "compositional")
colnames(ps_df)
# this data.frame can be used to customize several plots.
# example boxplot at family level
p.box <- ggstripchart(ps_df, "scientific_name", "Abundance",
facet.by = "Family", color = "body_product",
palette = "jco"
)
p.box + rremove("x.text")
```
```{r}
sessionInfo()
```