如何在R中获得聚集聚类的热图?

问题描述

我正在尝试获取聚集聚类的热图,并且不知道如何在R中实现。但是,我确实成功完成了与此相关的事情。这是我做的代码

用于运行分层聚类的方法,凝聚方法

# Compute with agnes 
hc_agnes <- agnes(dt_wd,method = "complete")

但是,我已经获得了热力图,但没有达到凝聚力。

    library(pheatmap)
    pheatmap(t(dt_wd[-1]),cutree_cols = 4)

这是上面代码输出

enter image description here

是否可以通过将hc_agnes(聚集层次聚类)传递给热图来实现相同的目的?

类似于我的数据的虚假数据在下面。

test_data <- structure(list(comorbidities = c("ast","dia","dia2","hd","ht","kd","ld","lc","ob"),chest_pain = c(4.85168018539977,2.51430173864274,5.01953418482344,6.44361833952912,5.3312101910828,8.81563126252505,9.89830508474576,9.37901331245106,5.23059042305904
),chills = c(25.3337195828505,8.34716769489624,30.2712246431255,27.1462205700124,21.2420382165605,28.2545090180361,32.1440677966102,22.7697729052467,21.1301720130172),cough = c(47.5816917728853,22.4245653393158,48.4079639368896,50.1945477075589,46.8216560509554,50.2985971943888,52.271186440678,56.1292090837901,45.3747094374709
),diarrhoea = c(23.8991888760139,7.90802019068985,21.1119459053343,24.0396530359356,22.2802547770701,22.0440881763527,24.5762711864407,21.6131558339859,23.6866573686657),fatigue = c(57.2132097334878,19.3494111048794,50.3756574004508,55.0185873605948,50.4585987261146,54.1082164328657,52.5423728813559,53.7979639780736,49.8760266542693
),headache = c(52.0278099652376,16.2647223780146,42.4492862509391,49.5662949194548,46.0891719745223,47.8957915831663,50.8474576271186,45.9671104150352,49.8062916472958),itchy_eyes = c(14.0787949015064,3.47728547392036,11.419984973704,13.7546468401487,13.031847133758,12.2244488977956,14.1949152542373,11.6679718089272,15.2254765225477
),joint_pain = c(6.37311703360371,1.90689848569826,5.3343350864012,6.07187112763321,5.73248407643312,7.21442885771543,8.89830508474576,6.734534064213,4.68774213544088),loss_appetite = c(7.4449594438007,3.36511497476164,7.77610818933133,8.42627013630731,7.49044585987261,7.81563126252505,9.11016949152542,7.04776820673453,7.78707577870758
),loss_smell_taste = c(28.2444959443801,10.8244531688166,29.3388429752066,29.1201982651797,27.7197452229299,31.062124248497,26.0593220338983,25.2936570086139,32.9226716256005),muscle_ache = c(42.6709154113557,14.301738642737,38.580015026296,45.3531598513011,40.3057324840764,45.4909819639279,47.6694915254237,38.2145653876273,39.7024639702464
),nasal_congestion = c(41.2804171494786,10.7683679192372,33.5462058602554,38.0421313506815,36.3949044585987,37.6753507014028,34.5338983050847,39.389193422083,40.8337207500387),nausea_vomiting = c(7.4449594438007,4.03813796971396,6.57400450788881,9.29368029739777,6,10.0200400801603,12.0762711864407,8.53563038371182,5.48582054858205),shortness_breath = c(33.9223638470452,7.23499719573752,17.3553719008264,27.1375464684015,18.9808917197452,26.6533066132265,25.8474576271186,35.1605324980423,18.0613668061367
),sore_throat = c(45.4519119351101,14.0773976444195,39.2561983471074,47.9553903345725,42.7898089171975,44.2885771543086,48.0932203389831,42.1299921691464,44.0570277390361),sputum = c(25.1158748551564,7.17891194615816,18.9706987227648,23.2961586121437,18.2420382165605,22.8456913827655,24.7881355932203,30.7752545027408,17.7591817759182
),temperature = c(25.7531865585168,16.0403813796971,35.6498873027799,30.9789343246592,28.8535031847134,36.87374749499,27.0164447924824,26.9177126917713)),row.names = c("ast",class = "data.frame")

使用的库:

library(cluster)
library(plotly)
library(fpc)
library(dendextend)
library(factoextra)
library(FactomineR)
library(NbClust)

解决方法

因此,当我为此苦苦挣扎时,答案很简单。库pheatmap是一个很好的工具,可用于传递聚集聚类所需的参数。

在这里:

加载库:

 library(pheatmap)

输入聚集聚类所需的参数,特别是聚类方法=“ ward”。

pheatmap(t(test_data[-1]),cluster_rows = TRUE,cluster_cols = TRUE,clustering_distance_cols = "euclidean",clustering_distance_rows = "euclidean",clustering_method = "ward",cutree_cols = 4)

实际上,可视化效果很好:

[![在此处输入图片描述] [1]] [1]