आर

2014-06-10 5 views
8

में एक क्षैतिज डेंड्रोग्राम के लिए क्लस्टर के चारों ओर वृक्ष काट और आयत, मैं R में एक डेंडरोग्राम के रूप में एक पदानुक्रमित क्लस्टरिंग के परिणामों को साजिश करने की कोशिश कर रहा हूं, जिसमें क्लस्टरों की पहचान करने वाले आयतों के साथ।आर

निम्नलिखित कोड एक लंबवत डेंड्रोग्राम के लिए चाल करता है, लेकिन एक क्षैतिज डेंडरोग्राम के लिए, (horiz=TRUE), आयताकार नहीं खींचे जाते हैं। क्षैतिज डेंडरोग्राम के लिए भी ऐसा करने का कोई तरीका है।

library("cluster") 
dst <- daisy(iris, metric = c("gower"), stand = FALSE) 
hca <- hclust(dst, method = "average") 
plot(as.dendrogram(hca), horiz = FALSE) 
rect.hclust(hca, k = 3, border = "red") 

इसके अलावा मैं पेड़ को वांछित दूरी मूल्य पर कटौती करने के लिए एक लाइन प्लॉट करना चाहता हूं। आर 0 में cutree फ़ंक्शन क्लस्टर को कैसे लौटाता है, लेकिन यह भी साजिश करना संभव है।

cutree(hca, k = 3) 

वांछित आउटपुट जो मैं ढूंढ रहा हूं वह इस तरह है।

dendrogram

यह कैसे आर में किया जाना?

+1

के बारे में क्या 'abline (v = 0.35)': रंग शाखाओं, सिर्फ मनोरंजन के लिए) की? – csgillespie

उत्तर

6

दोनों jlhoward और Backlin जवाब अच्छे हैं।

आप जो भी कोशिश कर सकते हैं वह dendextend पैकेज का उपयोग कर रहा है, जो वास्तव में इस तरह की चीज़ के लिए डिज़ाइन किया गया है। इसमें rect.dendrogram फ़ंक्शन है जो rect.hclust जैसा काम करता है, लेकिन एक क्षैतिज पैरामीटर के साथ (साथ ही रेक्ट के किनारे के स्थान पर कुछ और नियंत्रण)। प्रासंगिक ऊंचाई खोजने के लिए आप heights_per_k.dendrogram फ़ंक्शन का उपयोग कर सकते हैं (dendextendRcpp पैकेज का उपयोग करते समय बहुत तेज है)

यहां एक सरल उदाहरण है कि आप उपर्युक्त उदाहरणों के समान परिणाम कैसे प्राप्त करेंगे (एक अतिरिक्त बोनस के साथ)

install.packages("dendextend") 
install.packages("dendextendRcpp") 

library("dendextend") 
library("dendextendRcpp") 

# using piping to get the dend 
dend <- iris[,-5] %>% dist %>% hclust %>% as.dendrogram 

# plot + color the dend's branches before, based on 3 clusters: 
dend %>% color_branches(k=3) %>% plot(horiz=TRUE, main = "The dendextend package \n Gives extended functionality to R's dendrogram object") 

# add horiz rect 
dend %>% rect.dendrogram(k=3,horiz=TRUE) 

# add horiz (well, vertical) line: 
abline(v = heights_per_k.dendrogram(dend)["3"] + .6, lwd = 2, lty = 2, col = "blue") 

enter image description here

4

सिर्फ काम करवाने के लिए (हालांकि एक काफी बदसूरत तरीके से) तुम सिर्फ मैन्युअल rect.hclust में rect करने के लिए कॉल में निर्देशांक स्वैप कर सकते हैं:

rhc <- function (tree, k = NULL, which = NULL, x = NULL, h = NULL, border = 2, 
    cluster = NULL) 
{ 
    if (length(h) > 1L | length(k) > 1L) 
     stop("'k' and 'h' must be a scalar") 
    if (!is.null(h)) { 
     if (!is.null(k)) 
      stop("specify exactly one of 'k' and 'h'") 
     k <- min(which(rev(tree$height) < h)) 
     k <- max(k, 2) 
    } 
    else if (is.null(k)) 
     stop("specify exactly one of 'k' and 'h'") 
    if (k < 2 | k > length(tree$height)) 
     stop(gettextf("k must be between 2 and %d", length(tree$height)), 
      domain = NA) 
    if (is.null(cluster)) 
     cluster <- cutree(tree, k = k) 
    clustab <- table(cluster)[unique(cluster[tree$order])] 
    m <- c(0, cumsum(clustab)) 
    if (!is.null(x)) { 
     if (!is.null(which)) 
      stop("specify exactly one of 'which' and 'x'") 
     which <- x 
     for (n in seq_along(x)) which[n] <- max(which(m < x[n])) 
    } 
    else if (is.null(which)) 
     which <- 1L:k 
    if (any(which > k)) 
     stop(gettextf("all elements of 'which' must be between 1 and %d", 
      k), domain = NA) 
    border <- rep_len(border, length(which)) 
    retval <- list() 
    for (n in seq_along(which)) { 
     rect(
      ybottom = m[which[n]] + 0.66, 
      xright = par("usr")[3L], 
      ytop = m[which[n] + 1] + 0.33, 
      xleft = mean(rev(tree$height)[(k - 1):k]), 
      border = border[n]) 
     retval[[n]] <- which(cluster == as.integer(names(clustab)[which[n]])) 
    } 
    invisible(retval) 
} 

और जैसे आप rect.hclust बुलाया rhc फोन:

rhc(hca, k = 3, border = "red") 

enter image description here

3

यहाँ एक समाधान ggplot का उपयोग कर और ggdendro पैकेज। एक अतिरिक्त बोनस के रूप में, हम समूह द्वारा लेबल रंग कर सकते हैं ...

library(cluster) 
dst <- daisy(iris, metric = c("gower"), stand = FALSE) 
hca <- hclust(dst, method = "average") 
k  <- 3 
clust <- cutree(hca,k=k) # k clusters 

library(ggplot2) 
library(ggdendro)  # for dendro_data(...) 
dendr <- dendro_data(hca, type="rectangle") # convert for ggplot 
clust.df <- data.frame(label=rownames(iris), cluster=factor(clust)) 
dendr[["labels"]] <- merge(dendr[["labels"]],clust.df, by="label") 
rect <- aggregate(x~cluster,label(dendr),range) 
rect <- data.frame(rect$cluster,rect$x) 
ymax <- mean(hca$height[length(hca$height)-((k-2):(k-1))]) 

ggplot() + 
    geom_segment(data=segment(dendr), aes(x=x, y=y, xend=xend, yend=yend)) + 
    geom_text(data=label(dendr), aes(x, y, label=label, hjust=0, color=cluster), 
      size=3) + 
    geom_rect(data=rect, aes(xmin=X1-.3, xmax=X2+.3, ymin=0, ymax=ymax), 
      color="red", fill=NA)+ 
    geom_hline(yintercept=0.33, color="blue")+ 
    coord_flip() + scale_y_reverse(expand=c(0.2, 0)) + 
    theme_dendro()