3

Here is my data

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

here what I do

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

and it gives me what I want as follows enter image description here

Here all groups are shown by black color and the dendrogram is not that clear what I want is to change the color of each group and also use the name in vertical instead the number and finally I want to be able to remo the hclust(."ward.D2") while change the x label and y label as I want

Tal Galili
  • 24,605
  • 44
  • 129
  • 187
nik
  • 2,500
  • 5
  • 21
  • 48

2 Answers2

6

You could use the dendextend package, aimed for tasks such as this:

# install the package:

if (!require('dendextend')) install.packages('dendextend'); library('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

enter image description here

You can see many examples in the presentations and vignettes of the package, in the "usage" section in the following URL: https://github.com/talgalili/dendextend

Or you can use also:

You should use dendrapply.

For instance:

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

I get this information from: How to create a dendrogram with colored branches?

Community
  • 1
  • 1
Enrique
  • 842
  • 1
  • 9
  • 21
  • I like your answer ! however the function you use is different so please give me sometime to go through it – nik Aug 11 '16 at 10:17
  • Hi Nik, I am the author of dendextend. The package has several vignettes. You can look at this part for coloring branches: https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html#setting-a-dendrograms-branches – Tal Galili Aug 11 '16 at 13:16
1

We could instead draw rectangles around groups, let's say there are 5 groups(k = 5):

# 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


EDIT:

Remove x axis label, and add names instead of numbers:

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

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • I liked your answer. however, is it possible at least to make the boxes with different color, remove the hclust(."ward.D2") and show the words instead the values ? – nik Aug 11 '16 at 10:12
  • @nik updated the answer, you can change the colours of the boxes for example to green as: `border = "green"` – zx8754 Aug 11 '16 at 10:23
  • I appreciate your answer already. however, is it possible to change the font of the label because with my real data, all mixed and I am not able to see what is what. – nik Aug 11 '16 at 10:30