code for heatmap dot 2 for tutorial that seems to work 11-13-2014d1018
2014-11-13code for heatmap dot 2 for tutorial that seems to work 11-13-2014d1018
code from here http://www.molecularecologist.com/2013/08/making-heatmaps-with-r-for-microbiome-analysis/
library(gplots) # for heatmap.2
# to install packages from Bioconductor:
source("http://bioconductor.org/biocLite.R")
biocLite("Heatplus") # annHeatmap or annHeatmap2
library(Heatplus)
# load the vegan package for hierachical clustering if you want to use distance functions not specified in dist.
library(vegan)
# load the RColorBrewer package for better colour options
library(RColorBrewer)
all.data <- read.csv("C:/Users/Kurt Whittemore/Desktop/temp/TableS3-phylum.csv") # load the data
dim(all.data)
row.names(all.data) <- all.data$sample
all.data <- all.data, -1
data.prop <- all.data/rowSums(all.data)
data.prop1:3, 1:3
# colorRampPalette is in the RColorBrewer package. This creates a colour palette that shades from light yellow to red in RGB space with 100 unique colours
scaleyellowred <- colorRampPalette(c("lightyellow", "red"), space = "rgb")(100)
heatmap(as.matrix(data.prop), Rowv = NA, Colv = NA, col = scaleyellowred)
# calculate the Bray-Curtis dissimilarity matrix on the full dataset:
data.dist <- vegdist(data.prop, method = "bray")
# Do average linkage hierarchical clustering. Other options are 'complete' or 'single'. You'll need to choose the one that best fits the needs of your situation and your data.
row.clus <- hclust(data.dist, "aver")
# make the heatmap with Rowv = as.dendrogram(row.clus)
heatmap(as.matrix(data.prop), Rowv = as.dendrogram(row.clus), Colv = NA, col = scaleyellowred, margins = c(10, 3))
# you have to transpose the dataset to get the genera as rows
data.dist.g <- vegdist(t(data.prop), method = "bray")
col.clus <- hclust(data.dist.g, "aver")
# make the heatmap with Rowv = as.dendrogram(row.clus)
heatmap(as.matrix(data.prop), Rowv = as.dendrogram(row.clus), Colv = as.dendrogram(col.clus), col = scaleyellowred, margins = c(10, 3))
# There are 12 samples in the dataset, so let's assign them randomly to one of two fictional categories
var1 <- round(runif(n = 12, min = 1, max = 2)) # this randomly samples from a uniform distribution and rounds the result to an integer value
# change the 1s and 2s to the names of colours:
var1 <- replace(var1, which(var1 == 1), "deepskyblue")
var1 <- replace(var1, which(var1 == 2), "magenta")
heatmap.2(as.matrix(data.prop),Rowv = as.dendrogram(row.clus), Colv = as.dendrogram(col.clus), col = scaleyellowred, RowSideColors = var1 # this puts in the annotation for the samples margins = c(10, 3)))
heatmap.2(as.matrix(data.prop), Rowv = as.dendrogram(row.clus), Colv = as.dendrogram(col.clus), col = scaleyellowred, RowSideColors = var1, margins = c(11, 5), trace = "none", density.info = "none", xlab = "genera", ylab = "Samples", main = "Heatmap example", lhei = c(2, 8)) # this makes the colour-key legend a little thinner