r:使用dendextend为叶子和矩形分配标签到树形图?

时间:2016-04-20 14:10:29

标签: r cluster-analysis k-means dendrogram dendextend

我想绘制带有7列(2因子,5个数字)的数据帧data的树状图。第一列包含7个不同国家的名称,在下面的列中,我收集了表征每个国家的不同参数(如人口,GDP等)的数据。在最后一列中,因子变量指定了相应国家/地区所属的大陆。

这是数据

structure(list(Country = structure(c(5L, 4L, 7L, 2L, 1L, 6L, 
3L), .Label = c("Brazil", "Chile", "China", "France", "Germany", 
"India", "Netherlands"), class = "factor"), GDP = c(0.46, 0.57, 
0.75, 0.56, 0.28, 0.88, 1), Population = c(0.18, 0.09, 0.54, 
0.01, 0.02, 0.17, 0.84), Birth.rate = c(87.21, 18.34, 63.91, 
14.21, 5.38, 51.19, 209.26), Income = c(43.89, 18.23, 63.91, 
12.3, 0.1, 14.61, 160.82), Savings = c(43.32, 0.11, 0, 1.91, 
5.29, 36.58, 50.38), Continent = structure(c(2L, 2L, 2L, 3L, 
3L, 1L, 1L), .Label = c("Asia", "Europe", "South America"), class = "factor")), .Names = c("Country", 
"GDP", "Population", "Birth.rate", "Income", "Savings", "Continent"
), class = "data.frame", row.names = c(NA, -7L))

现在我想要获得的树形图应该具有以下特征:

  1. 假标签应根据大陆会员身份着色
  2. 叶子应根据各自的国家(非数字)进行标记
  3. 群集周围应该有矩形
  4. 我已经尝试了dendextend包,可以在https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html#setting-a-dendrograms-branches找到,但上述特征中的2.和3.似乎不能同时协同工作。我的代码看起来像这样(在将data标准化为norm

    之后
    #color codes for continents
    regionCodes = c(rep("Europe",3), rep("South America", 2), rep("Asia",2), )
    rownames(data) = make.unique(regionCodes)
    colorCodes = c(Europe="blue", South America="yellow", Asia="red")
    
    #dendrogram generation and plot
    dc = as.dendrogram(hclust(dist(norm), method="complete"))
    labels_colors(dc) = colorCodes[regionCodes][order.dendrogram(dc)]
    labels(dc) = data$Country
    labels_cex(dc) = .7
    dc %>% plot
    dc %>% rect.dendrogram(k=4, border = 8, lty = 5, lwd = 2)
    

    但它会产生以下错误

      

    数据错误$ Country:'closure'类型的对象不是可子集化的

    你能帮助我吗?

0 个答案:

没有答案