树形图:按特征分组

时间:2017-04-20 15:45:42

标签: r tree dendrogram

我过去在各种在线教程(例如https://rstudio-pubs-static.s3.amazonaws.com/33876_1d7794d9a86647ca90c4f182df93f0e8.html

之后制作了树状图

目标:按Exposures分组Category,并在树/树形图中将其可视化(例如ExpAExpD将组合在一起因为它们都是HeavyMetals

背景:我正在尝试制作以下树木/树状图:

Exposures = c("ExpA","ExpB", "ExpC","ExpD","ExpE", "ExpF")
Category = c("HeavyMetal","Hormone", "Pesticide", "HeavyMetal", "Pesticide", "Hormone" )
dataframe = data.frame(Exposures,Category)

我尝试了以下内容:

hp = hclust(dist(dataframe))
plot(hp, labels = Exposures)

但是我收到一条错误消息:

Error in hclust(dist(dataframe1)) : 
  NA/NaN/Inf in foreign function call (arg 11)
In addition: Warning message:
In dist(dataframe1) : NAs introduced by coercion

我认为错误信息是因为我没有任何数字数据,但我不确定。

问题:有没有办法从类似于上述数据框的数据框中制作树/树形图结构,并考虑到上述目标?

谢谢!

1 个答案:

答案 0 :(得分:1)

install.packages('cluster')
library('cluster')
dissimilarity <- daisy(dataframe)
hc = hclust(dissimilarity)
plot(hc, labels = Exposures)