R:如何显示聚类矩阵热图(类似的颜色模式分组) [英] R: How do I display clustered matrix heatmap (similar color patterns are grouped)
问题描述
我有集群数据(kmeans / EM / DBscan ..),我想通过对同一个集群进行分组来创建一个热图。我希望类似的颜色模式能够在热图中分组,所以一般来说,它看起来像块对角线。
我尝试按簇号对数据进行排序并显示它,
k = kmeans(data,3)
d = data.frame(data)
d = data.frame(d,k $ cluster)
d = d [order(d $ k.cluster),]
heatmap(as.matrix (d))
但它仍然没有排序,看起来像这样的链接:
我可以在R中做这个吗?
我搜索了很多软件包并尝试了很多方法,但我仍然有一个问题。
非常感谢。
您可以使用 reshape2 和
ggplot2
如下:
图书馆(reshape2)
library(ggplot2)
#创建虚拟数据
set.seed(123)
df< - data.frame(
a =样品(1:5,1000,repl ace = TRUE),
b = sample(1:5,1000,replace = TRUE),
c = sample(1:5,1000,replace = TRUE)
)
#执行聚类
k < - kmeans(df,3)
#附加id和集群
dfc< - cbind(df,id = seq(nrow df)),cluster = k $ cluster)
#添加idsort,按集群顺序排列的id号
dfc $ idsort< - dfc $ id [order(dfc $ cluster)]
dfc $ idsort< - order(dfc $ idsort)
#使用reshape2 :: melt以长格式创建data.frame
dfm< - melt(dfc,id .vars = c(id,idsort))
ggplot(dfm,aes(x = variable,y = idsort))+ geom_tile(aes(fill = value))
I searched a lot of questions about heatmap throughout the site and packages, but I still have a problem.
I have clustered data (kmeans/EM/DBscan..), and I want to create a heatmap by grouping the same cluster. I want the similar color patterns to be grouped in the heatmap, so generally, it looks like a block-diagonal.
I tried to order the data by the cluster number and display it,
k = kmeans(data, 3)
d = data.frame(data)
d = data.frame(d, k$cluster)
d = d[order(d$k.cluster),]
heatmap(as.matrix(d))
but it is still not sorted and looks like this link:
But, I want it to be sorted by its cluster number and looked like this:
Can I do this in R?
I searched lots of packages and tried many ways, but I still have a problem.
Thanks a lot.
You can do this using reshape2
and ggplot2
as follows:
library(reshape2)
library(ggplot2)
# Create dummy data
set.seed(123)
df <- data.frame(
a = sample(1:5, 1000, replace=TRUE),
b = sample(1:5, 1000, replace=TRUE),
c = sample(1:5, 1000, replace=TRUE)
)
# Perform clustering
k <- kmeans(df, 3)
# Append id and cluster
dfc <- cbind(df, id=seq(nrow(df)), cluster=k$cluster)
# Add idsort, the id number ordered by cluster
dfc$idsort <- dfc$id[order(dfc$cluster)]
dfc$idsort <- order(dfc$idsort)
# use reshape2::melt to create data.frame in long format
dfm <- melt(dfc, id.vars=c("id", "idsort"))
ggplot(dfm, aes(x=variable, y=idsort)) + geom_tile(aes(fill=value))
这篇关于R:如何显示聚类矩阵热图(类似的颜色模式分组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!