1

data我想为具有 7 列(2 因子,5 num)的数据框绘制树状图。第一列包含 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. 簇周围应该有矩形

我已经尝试了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)

但它会产生以下错误

data$Country 中的错误:“闭包”类型的对象不是子集

你能帮助我吗?

4

0 回答 0