Skip to content

Instantly share code, notes, and snippets.

@markziemann
Created July 9, 2020 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markziemann/67cd231fa450c082b88beb3d5d29fd98 to your computer and use it in GitHub Desktop.
Save markziemann/67cd231fa450c082b88beb3d5d29fd98 to your computer and use it in GitHub Desktop.
This gist demonstrates how to perform unsupervised hierarchical clustering
library(RColorBrewer)
library(gplots)
# Generate some random data
N_SAMPLES=20
N_GENES=30
x<- matrix(data = rnorm(600), nrow = N_GENES, ncol = N_SAMPLES)
rownames(x) <- paste("genes",1:N_GENES)
colnames(x) <- paste("sample",1:N_SAMPLES)
head(x)
cl<-as.dist(1-cor(t(x), method="spearman"))
hr <- hclust(cl , method="complete")
# need to optimise the cluster size - depends on your project
mycl <- cutree(hr, h=max(hr$height/1.3))
clusterCols <- brewer.pal(length(unique(mycl)),"Paired")
myClusterSideBar <- clusterCols[mycl]
colfunc <- colorRampPalette(c("blue", "white", "red"))
write.table(mycl,file="GeneClusters1.txt",quote=F,sep="\t")
# create heatmap
heatmap.2(x, main="Gene Clustering 1", Rowv=as.dendrogram(hr),
dendrogram="both", scale="none", col = colfunc(25), trace="none",
RowSideColors= myClusterSideBar, margins = c(5,5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment