[英]Horizontal Dendrogram with Labels in R
我遇到了一個問題,我可以繪制帶有標簽的垂直樹狀圖,但在水平時不能添加標簽。
我的數據如下所示:
Company Industry1 Industry2 Industry3
Google 3% 5% 6%
Apple 2% 6% 1%
當我導入數據時,第一列包含我的標簽,但行僅為1、2、3等。
所以我的代碼顯示為:數據源稱為Cluster_D
labs = Cluster_D[, 1]
Industry <- Cluster_D
rownames(Industry) <- labs$`Company`
D.Industry <- dist(scale(round(Industry[, -1], 3)), method = "euclidean")
H.Industry <- hclust(D.Industry, method = "ward.D")
plot(H.Industry, labels = Cluster_D$`Company`)
因此,我將標簽分配給變量“ Labs”,然后將數據放入另一個變量“ Industry”。一旦我繪制了數據並傳遞了Labels,我便獲得了所需簇的圖表,該圖表垂直於標簽工作。 ....但
我不知道如何使此圖表翻轉為水平並保持標簽名稱。 我嘗試使用as.dendrogram
函數,該函數允許我使用as.dendrogram
horiz=true
但是我無法保留標簽,因為它會還原為1、2、3等。
誰能向我解釋我如何才能糾正自己? 我曾經使用過Statistica,在進行層次結構聚類時沒有任何問題,我正在嘗試使用R。我覺得分配標簽應該超級容易,但我不知道如何。
我嘗試使用下面的方法,但是圖表貼錯了標簽(ABC順序)。
F.Industries <- as.dendrogram(H.Industry)
labels(F.Industries) <- paste(as.character(Cluster_D[,1]))
plot(F.Industries, horiz = TRUE)
根據PAR的要求:
數據-我在IBM中又增加了一列:
z <- read.table(text = "Company Industry1 Industry2 Industry3
Google 3% 5% 6%
Apple 2% 6% 1%
IBM 7% 4% 2%", header = T)
當我嘗試:
scale(round(z[, -1], 3))
#output
Error in Math.data.frame(list(Industry1 = c(2L, 1L, 3L), Industry2 = c(2L, :
non-numeric variable in data frame: Industry1Industry2Industry3
意味着您提供的樣本數據並不代表您自己的數據。
轉換為數字:
z = data.frame("Company" = z[,1], apply(z[,-1], 2, function(x) as.numeric(gsub("%", "", x))))
行名是葉子的標簽
rownames(z) <- z[,1]
D.Industry <- dist(scale(z[, -1]), method = "euclidean")
H.Industry <- hclust(D.Industry, method = "ward.D")
plot(as.dendrogram(H.Industry), horiz = T)
一個人可以用mar
來調整邊距
par(mar=c(2, 0, 0, 8))
plot(as.dendrogram(H.Industry), horiz = T)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.