3

I understand how to add colored bars to a dendextend plot, but when I set horiz=T the bar is plotted on the wrong axis.

The example from the Introduction to dendextend shows how to add colored bars to a plot:

dend15 <- c(1:5) %>% dist %>% hclust(method = "average") %>% as.dendrogram

is_odd <- ifelse(labels(dend15) %% 2, 2,3)
is_345 <- ifelse(labels(dend15) > 2, 3,4)
is_12 <- ifelse(labels(dend15) <= 2, 3,4)
k_3 <- cutree(dend15,k = 3, order_clusters_as_data = FALSE) 
# The FALSE above makes sure we get the clusters in the order of the
# dendrogram, and not in that of the original data. It is like:
# cutree(dend15, k = 3)[order.dendrogram(dend15)]
the_bars <- cbind(is_odd, is_345, is_12, k_3)
the_bars[the_bars==2] <- 8

dend15 %>% plot(horiz=F)
colored_bars(colors = the_bars, dend = dend15)

When I execute the above code I see this plot:

colored_bars plot good example

However, when I type

dend15 %>% plot(horiz=T) 
colored_bars(colors = the_bars, dend = dend15)

this is what I see:

colored_bars plot bad example

Is it possible to move the colored bars to the axis next to the leaf nodes of the tree?

  • 1
    The colored_bars function would need to be updated. I don't think I'll get to it - but if someone will - I will gladly add it to the package. (one would like to add a colored_bars(horiz = TRUE) parameter that would adjust things. – Tal Galili Nov 13 '15 at 14:58

1 Answers1

1

Maybe this chunk of code (take from here) could help you:

par(mar = c(4,1,1,12))
plot(dend15, horiz = TRUE)
colored_bars(dend15, colors = the_bars, horiz = TRUE)

Good luck!

jgarces
  • 519
  • 5
  • 17