R:如何显示聚类矩阵热图(相似颜色图案分组)

发布于 2024-11-01 15:24:01 字数 714 浏览 1 评论 0原文

我在整个网站和软件包中搜索了很多有关热图的问题,但仍然有问题。
我有集群数据(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))
but it is still not sorted and looks like this link:enter image description here
But, I want it to be sorted by its cluster number and looked like this:enter image description here
Can I do this in R?
I searched lots of packages and tried many ways, but I still have a problem.
Thanks a lot.

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:enter image description here
But, I want it to be sorted by its cluster number and looked like this:enter image description here
Can I do this in R?
I searched lots of packages and tried many ways, but I still have a problem.
Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酉 2024-11-08 15:24:01

您可以使用 reshape2ggplot2 执行此操作,如下所示:

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))

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))

enter image description here

壹場煙雨 2024-11-08 15:24:01

如果您不需要树状图和后续排序,则应将 RowvColv 设置为 NA。顺便说一句,您还应该进行缩放。使用 Andrie 的 df :

heatmap(as.matrix(df)[order(k$cluster),],Rowv=NA,Colv=NA,scale="none",labRow=NA)

在此处输入图像描述

事实上,整个热图基于 image()< /代码>。您可以使用 image 来构建完全符合您想要的绘图。 Heatmap在内部使用layout(),因此很难设置边距。对于图像,您可以执行以下操作:

myHeatmap <- function(x,ord,xlab="",ylab="",main="My Heatmap",
                      col=heat.colors(5), ...){
    op <- par(mar=c(3,0,2,0)+0.1)
    on.exit(par(op))
    nc <- NCOL(x)
    nr <- NROW(x)
    labCol <- names(x)

    x <- t(x[ord,])
    image(1L:nc, 1L:nr, x, xlim = 0.5 + c(0, nc), ylim = 0.5 +
        c(0, nr), axes = FALSE, xlab=xlab, ylab=ylab, main=main,
        col=col,...)

    axis(1, 1L:nc, labels = labCol, las = 2, line = -0.5, tick = 0)
    axis(2, 1L:nr, labels = NA, las = 2, line = -0.5, tick = 0)
}

library(RColorBrewer)
myHeatmap(df,order(k$cluster),col=brewer.pal(5,"BuGn"))

生成侧面边距较少的绘图。您还可以操作轴、颜色……您绝对应该看看 RColorBrewer 包

(这个自定义函数基于热图使用的内部绘图顺便说一句,为了说明而进行了简化并获得摆脱所有树状图的东西)

在此处输入图像描述

You should set Rowv and Colv to NA if you don't want the dendrograms and the subseuent ordering. BTW, You should also put of the scaling. Using the df of Andrie :

heatmap(as.matrix(df)[order(k$cluster),],Rowv=NA,Colv=NA,scale="none",labRow=NA)

enter image description here

In fact, this whole heatmap is based on image(). You can hack away using image to construct a plot exactly like you want. Heatmap is using layout() internally, so it will be diffucult to set the margins. With image you could do eg :

myHeatmap <- function(x,ord,xlab="",ylab="",main="My Heatmap",
                      col=heat.colors(5), ...){
    op <- par(mar=c(3,0,2,0)+0.1)
    on.exit(par(op))
    nc <- NCOL(x)
    nr <- NROW(x)
    labCol <- names(x)

    x <- t(x[ord,])
    image(1L:nc, 1L:nr, x, xlim = 0.5 + c(0, nc), ylim = 0.5 +
        c(0, nr), axes = FALSE, xlab=xlab, ylab=ylab, main=main,
        col=col,...)

    axis(1, 1L:nc, labels = labCol, las = 2, line = -0.5, tick = 0)
    axis(2, 1L:nr, labels = NA, las = 2, line = -0.5, tick = 0)
}

library(RColorBrewer)
myHeatmap(df,order(k$cluster),col=brewer.pal(5,"BuGn"))

To produce a plot that has less margins on the side. You can also manipulate axes, colors, ... You should definitely take a look at the RColorBrewerpackage

(This custom function is based on the internal plotting used by heatmap btw, simplified for the illustration and to get rid of all the dendrogram stuff)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文