heatmap.2中设置距离矩阵和聚类方法
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览一下
heatmap.2
的代码,我相当确定默认是使用dist
,而它的默认设置又是使用欧几里得距离。您尝试传递
distfun = dist(method = 'euclidean')
不起作用的原因是distfun
(和hclustfun
) 应该简单地是函数的名称。因此,如果您想更改默认值并传递参数,您需要编写一个像这样的包装函数:正如我所提到的,我相当确定
heatmap.2
默认情况下使用欧几里德距离,但类似解决方案可用于改变所使用的距离函数:Glancing at the code for
heatmap.2
I'm fairly sure that the default is to usedist
, 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 thatdistfun
(andhclustfun
) 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: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: