-
Notifications
You must be signed in to change notification settings - Fork 3
/
Diversity_BarPlots.R
75 lines (42 loc) · 2.42 KB
/
Diversity_BarPlots.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
#Load libraries
library(phyloseq)
library(microbiome)
library(readxl)
library(randomcoloR)
# Upload two Excel files
# To graph different cohorts, the mouse2 file will change to graph the different cohorts. mouse1 will remain the same
genus_names <- read_excel('/Users/lindsayhopson/Desktop/THESIS/genus_names.xlsx')
abundance <- read_excel('/Users/lindsayhopson/Desktop/THESIS/abundance.xlsx')
OTU = otu_table(abundance, taxa_are_rows = TRUE)
TAX = tax_table(genus_names)
physeq = phyloseq(OTU, TAX)
# Define the order of the x-axis by first getting the values from mouse2.
order_values <- names(abundance)
library(formatR)
#This code revises the origanal plot_bar function so that give non default rainbow colors,and chnage other features for the bar plot to best plot our data
custom_color_bar_plot <- function (physeq, x_pass = 'Sample', y_pass = 'Abundance', fill_variable = NULL, title = NULL, fac_grid = NULL, order_pass = NULL)
{
mdf = psmelt(physeq)
if(!is.null(order_pass))
{
# Order the data frame first.
#Turn your 'treatment' column into a character vector
mdf$Sample <- as.character(mdf$Sample)
#Then turn it back into a factor with the levels in the correct order
mdf$Sample <- factor(mdf$Sample, levels=order_pass)
}
colorCount <- length(unique(mdf$ta1))
# The number of colors specified has to be the same number of genus listed in mouse1 (the number of columns in mouse1 and mouse2 have to be the same number of specified colors )
# labs(fill = "Genus") is added to change the legend. It's hard coded that "ta1" represents genus. So this code was added to manually change the legend
p = ggplot(mdf, aes_string(x = x_pass, y = y_pass, fill = fill_variable)) + geom_bar(stat = 'identity', position = 'stack', color = 'black') + theme(axis.text.x = element_text(angle = -90, hjust = 0)) + scale_fill_manual(values = randomColor(colorCount, hue = "random", luminosity = " ")) + labs(fill = "Genus") + ylab("Relative Abundance")
if (!is.null(facet_grid))
{
p <- p + facet_grid(fac_grid)
}
if (!is.null(title))
{
p <- p + ggtitle(title)
}
return(p)
}
custom_color_bar_plot(physeq, x_pass = 'Sample', y_pass = 'Abundance', fill_variable = 'ta1', order_pass = order_values)