heatmap.2中设置距离矩阵和聚类方法

发布于 2024-11-26 07:29:24 字数 828 浏览 2 评论 0原文

heatmap.2 默认使用 dist 计算距离矩阵,使用 hclust 进行聚类。 现在有人知道如何设置 dist 使用欧几里德方法和 hclust 使用质心方法吗? 我在下面提供了一个可编译的代码示例。 我尝试过: distfun = dist(method = "euclidean"), 但这行不通。有什么想法吗?

library("gplots")
library("RColorBrewer")

test <- matrix(c(79,38.6,30.2,10.8,22,
81,37.7,28.4,9.7,19.9,
82,36.2,26.8,9.8,20.9,
74,29.9,17.2,6.1,13.9,
81,37.4,20.5,6.7,14.6),ncol=5,byrow=TRUE)
colnames(test) <- c("18:0","18:1","18:2","18:3","20:0")
rownames(test) <- c("Sample 1","Sample 2","Sample 3", "Sample 4","Sample 5")
test <- as.table(test)
mat=data.matrix(test)

heatmap.2(mat,
dendrogram="row",
Rowv=TRUE,
Colv=NULL,
distfun = dist,
hclustfun = hclust,
xlab = "Lipid Species",
ylab = NULL,
colsep=c(1),
sepcolor="black",
key=TRUE,
keysize=1,
trace="none",
density.info=c("none"),
margins=c(8, 12),
col=bluered
)

heatmap.2 defaults to dist for calculating the distance matrix and hclust for clustering.
Does anyone now how I can set dist to use the euclidean method and hclust to use the centroid method?
I provided a compilable code sample bellow.
I tried: distfun = dist(method = "euclidean"),
but that doesn't work. Any ideas?

library("gplots")
library("RColorBrewer")

test <- matrix(c(79,38.6,30.2,10.8,22,
81,37.7,28.4,9.7,19.9,
82,36.2,26.8,9.8,20.9,
74,29.9,17.2,6.1,13.9,
81,37.4,20.5,6.7,14.6),ncol=5,byrow=TRUE)
colnames(test) <- c("18:0","18:1","18:2","18:3","20:0")
rownames(test) <- c("Sample 1","Sample 2","Sample 3", "Sample 4","Sample 5")
test <- as.table(test)
mat=data.matrix(test)

heatmap.2(mat,
dendrogram="row",
Rowv=TRUE,
Colv=NULL,
distfun = dist,
hclustfun = hclust,
xlab = "Lipid Species",
ylab = NULL,
colsep=c(1),
sepcolor="black",
key=TRUE,
keysize=1,
trace="none",
density.info=c("none"),
margins=c(8, 12),
col=bluered
)

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

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

发布评论

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

评论(1

不羁少年 2024-12-03 07:29:24

浏览一下 heatmap.2 的代码,我相当确定默认是使用 dist,而它的默认设置又是使用欧几里得距离。

您尝试传递 distfun = dist(method = 'euclidean') 不起作用的原因是 distfun (和 hclustfun) 应该简单地是函数的名称。因此,如果您想更改默认值并传递参数,您需要编写一个像这样的包装函数:

heatmap.2(...,hclustfun = function(x) hclust(x,method = 'centroid'),...)

正如我所提到的,我相当确定 heatmap.2 默认情况下使用欧几里德距离,但类似解决方案可用于改变所使用的距离函数:

heatmap.2(...,distfun = function(x) dist(x,method = 'euclidean'),...)

Glancing at the code for heatmap.2 I'm fairly sure that the default is to use dist, and it's default is in turn to use euclidean distances.

The reason your attempt at passing distfun = dist(method = 'euclidean') didn't work is that distfun (and hclustfun) are supposed to simply be name of functions. So if you want to alter defaults and pass arguments you need to write a wrapper function like this:

heatmap.2(...,hclustfun = function(x) hclust(x,method = 'centroid'),...)

As I mentioned, I'm fairly certain that heatmap.2 is using euclidean distances by default, but a similar solution can be used to alter the distance function used:

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