Skip to content

Instantly share code, notes, and snippets.

@kcha
Created July 30, 2014 19:53
Show Gist options
  • Save kcha/90101c43d2ab6ebb8fb2 to your computer and use it in GitHub Desktop.
Save kcha/90101c43d2ab6ebb8fb2 to your computer and use it in GitHub Desktop.
Hierarchical clustering example
# Sample code for performing hierarchical clustering
# install.packages("gplots")
library(gplots)
library(RColorBrewer)
##### Use iris dataset ####
# See ?iris for more info
data <- as.matrix(iris[,1:4])
dr <- as.dist(1 - cor(t(data), method="spearman", use="pairwise.complete.obs"))
dc <- as.dist(1 - cor(data, method="spearman", use="pairwise.complete.obs"))
hr <- hclust(dr, method="complete")
hc <- hclust(dc, method="complete")
#### Plot heatmap ####
mycolors <- brewer.pal(9, "YlOrRd")
mycolhr <- rainbow(3) # generate 3 colors
names(mycolhr) <- unique(iris$Species)
mycolhr <- mycolhr[iris$Species]
mycolhc <- gray.colors(4)
pdf("test.pdf")
heatmap.2(data,
Rowv=as.dendrogram(hr), Colv=as.dendrogram(hc),
RowSideColors=mycolhr, ColSideColors=mycolhc,
col=mycolors, trace="none", density.info="none",
cexRow=0.6, cexCol=0.8, keysize=1,
margins=c(5,10),
labRow=iris$Species, scale="none",
sepwidth=c(0,0)
)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment