本文介绍了R用热图绘制kmeans聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用kmeans聚类一个矩阵,并能够将其绘制为热图.听起来很琐碎,我已经看过很多这样的情节.我曾尝试过搜索around,但找不到解决方法.
I would like to cluster a matrix with kmeans, and be able to plot it as heatmap. It sounds quite trivial, and I have seen many plots like this. I have tried to google atround, but can't find a way round it.
我希望能够在此图上绘制面板A或B之类的东西.假设我有一个包含250行和5列的矩阵.我不想聚集列,只是行.
I'd like to be able to plot something like panel A or B on this figure.Let say I have a matrix with 250 rows and 5 columns. I don't want to cluster the columns, just the rows.
m = matrix(rnorm(25), 250, 5)
km = kmeans(m, 10)
然后,如何绘制这10个簇作为热图?您的评论和帮助绝对值得欢迎.
Then how do I plot those 10 clusters as a heatmap ? You comments and helps is more than welcome.
谢谢.
推荐答案
类似以下的方法应该起作用:
Something like the following should work:
set.seed(100)
m = matrix(rnorm(10), 100, 5)
km = kmeans(m, 10)
m2 <- cbind(m,km$cluster)
o <- order(m2[, 6])
m2 <- m2[o, ]
library(pheatmap) # I like esoteric packages!
library(RColorBrewer)
pheatmap(m2[,1:5], cluster_rows=F,cluster_cols=F, col=brewer.pal(10,"Set3"),border_color=NA)
这篇关于R用热图绘制kmeans聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!