2

ここに私のデータがあります

df<- structure(list(name = structure(c(2L, 12L, 1L, 16L, 14L, 10L, 
9L, 5L, 15L, 4L, 8L, 13L, 7L, 6L, 3L, 11L), .Label = c("All", 
"Bab", "boro", "bra", "charli", "delta", "few", "hora", "Howe", 
"ist", "kind", "Kiss", "myr", "No", "TT", "where"), class = "factor"), 
    value = c(1.251, -1.018, -1.074, -1.137, 1.018, 1.293, 1.022, 
    -1.008, 1.022, 1.252, -1.005, 1.694, -1.068, 1.396, 1.646, 
    1.016)), .Names = c("name", "value"), class = "data.frame", row.names = c(NA, 
-16L))

ここで私は何をしますか

d <- dist(as.matrix(df$value),method = "euclidean")
#compute cluster membership
hcn <- hclust(d,method = "ward.D2")
plot(hcn)

そして、それは私が次のように欲しいものを私に与えますここに画像の説明を入力

ここでは、すべてのグループが黒色で表示されており、樹状図は明確ではありません。私が望むのは、各グループの色を変更し、番号の代わりに縦に名前を使用することです。最後に、hclust( . " ward.D2") x ラベルと y ラベルを必要に応じて変更しながら

4

2 に答える 2

4

次のようなタスクを目的とした dendextend パッケージを使用できます。

# install the package:

if (!require('dendextend')) install.packages('dendextend'); ライブラリ('dendextend')

## Example:
dend <- as.dendrogram(hclust(dist(USArrests), "ave"))
d1=color_branches(dend,k=5, col = c(3,1,1,4,1))
plot(d1) # selective coloring of branches :)
d2=color_branches(d1,k=5) # auto-coloring 5 clusters of branches.
plot(d2)
# More examples are in ?color_branches

ここに画像の説明を入力

次の URL の「使用法」セクションで、パッケージのプレゼンテーションとビネットで多くの例を見ることができます: https://github.com/talgalili/dendextend

または、次も使用できます。

dendraply を使用する必要があります。

例えば:

# Generate data
set.seed(12345)
desc.1 <- c(rnorm(10, 0, 1), rnorm(20, 10, 4))
desc.2 <- c(rnorm(5, 20, .5), rnorm(5, 5, 1.5), rnorm(20, 10, 2))
desc.3 <- c(rnorm(10, 3, .1), rnorm(15, 6, .2), rnorm(5, 5, .3))

data <- cbind(desc.1, desc.2, desc.3)

# Create dendrogram
d <- dist(data) 
hc <- as.dendrogram(hclust(d))

# Function to color branches
colbranches <- function(n, col)
  {
  a <- attributes(n) # Find the attributes of current node
  # Color edges with requested color
  attr(n, "edgePar") <- c(a$edgePar, list(col=col, lwd=2))
  n # Don't forget to return the node!
  }

# Color the first sub-branch of the first branch in red,
# the second sub-branch in orange and the second branch in blue
hc[[1]][[1]] = dendrapply(hc[[1]][[1]], colbranches, "red")
hc[[1]][[2]] = dendrapply(hc[[1]][[2]], colbranches, "orange")
hc[[2]] = dendrapply(hc[[2]], colbranches, "blue")

# Plot
plot(hc)

この情報は次の場所から取得します:色付きの枝を持つ樹形図を作成するには?

于 2016-08-11T09:11:48.410 に答える
1

代わりに、グループの周りに四角形を描くことができます.5つのグループがあるとしましょう( k = 5):

# plot dendogram
plot(hcn)

# then draw dendogram with red borders around the 5 clusters 
rect.hclust(hcn, k = 5, border = "red")

ここに画像の説明を入力


編集:

x 軸のラベルを削除し、数字の代わりに名前を追加します。

plot(hcn, xlab = NA, sub = NA, labels = df$name)
rect.hclust(hcn, k = 5, border = "red")

ここに画像の説明を入力

于 2016-08-11T09:54:51.480 に答える