2016-08-11 18 views
2

에서 각 그룹에 대해 dendrogram은의 색상을 변경하려면 enter image description here방법 클러스터 여기

다음과 같이 내가 원하는 내가

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

을 무엇을 내 데이터 여기

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)) 

이며 나를 준다

여기에 모든 그룹이 검은 색으로 표시되며 굳이 그램은 내가 원하는 것은 각 그룹의 색상을 변경하고 숫자 대신 세로로 이름을 사용하는 것입니다. hclust를 remo 할 수 있어야합니다 (. "ward.D2") 나는

답변

4

당신은 이와 같은 작업을 위해 목표로 dendextend 패키지를 사용할 수 있습니다 원하는대로 X 라벨 및 y 레이블을 변경하는 동안! (

# install the package: 

경우 (필요 'dendextend ')) install.packages ('dendextend '); https://github.com/talgalili/dendextend

또는 다음 URL에서 "사용"섹션에서 라이브러리 패키지의 프리젠 테이션 및 네트에서 많은 예를 볼 수 있습니다

## 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 

enter image description here

('dendextend'), 다음을 사용할 수도 있습니다 :

dendrapply를 사용해야합니다. 예를 들어

:

# 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) 

나는이 정보 얻을 :

# plot dendogram 
plot(hcn) 

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

enter image description here : How to create a dendrogram with colored branches?

+0

내가 당신의 대답처럼! 그러나 당신이 사용하는 기능이 다르므로 언젠가 그것을 통해 가보겠습니다. – nik

+0

안녕하세요, 저는 덴덴티스트의 저자입니다. 패키지에는 몇 개의 비 네트가 있습니다. 당신은이 부분을 색칠하는 부분을 볼 수 있습니다 : https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html#setting-a-dendrograms-branches –

1

을 우리는 그룹의 주위에 대신 draw rectangles,의 5 개 그룹 (k = 5)이있는 가정 해 봅시다 수


편집 :

제거 x 축 라벨, 숫자 대신 이름을 추가 :

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

enter image description here

+0

나는 당신의 대답을 좋아했습니다. 그러나 적어도 다른 색으로 상자를 만들고 hclust (.ward.D2)를 제거하고 단어 대신 값을 표시 할 수 있습니까? – nik

+0

@nik이 답변을 업데이트했습니다. 예를 들어 초록색으로 상자 색상을 변경할 수 있습니다.'border = "green"' – zx8754

+0

답변을 주셔서 감사합니다.그러나 실제 데이터로 인해 모든 것이 혼합되어 있고 무엇이 무엇인지를 볼 수 없으므로 레이블의 글꼴을 변경할 수 있습니다. – nik