If you're doing supervised k-means clustering, you could do something like the following:
output_dir_prefix <- "results"
kclusters <- c(2, 3, 4, 6, 8, 10, 15, 20, 25, 50, 100)
for (kcluster in kclusters) {
print(kcluster)
dir.create(file.path(output_dir_prefix, kcluster))
clustering <- kmeans(data, kcluster)
for (i in seq(1, kcluster, 1)) {
print(paste(kcluster, i, sep=":"))
out_fn <- paste(output_dir_prefix, kcluster, paste(i, ".mtx", sep=""), sep="/")
body <- data[clustering$cluster == i,]
write.table(body, file=out_fn, quote=FALSE, sep="\t", row.names=FALSE, col.names=FALSE, append=TRUE)
}
}
This gives submatrices of results from running k-means clustering on your data
over k clusters in kclusters
.
You could use matrix2png
on each clustering submatrix i.mtx
file, in order to generate a heatmap visualization for that submatrix.
If you're doing unsupervised, hierarchical clustering, you could use cutree
to cut a tree into segments at a specified tree height. You might visually inspect the clustering to decide on the height, or use some sensibly arbitrary heuristic. Examples of this are described in answers to another Biostars question.
I edited the title of this post so it is easy to find it in future by others.
What function are you using ? There are many ways of doing heatmaps in R. Depending on the function, the easiest way may be to use the same clustering function with the same parameters as used in your heatmap function, e.g. heatmap() uses hclust() by default. Note that for hierachical clustering, you would need to cut the tree to get clusters.