rows_picking <- c(1:5, 25:30)
dend <- (iris[rows_picking,-5]*10) %>% dist %>% hclust %>% as.dendrogram
odd_numbers <- rows_picking %% 2
cols <- c("gold", "grey")[odd_numbers+1]
# scale is off
plot(dend)
colored_bars(dend, cols)
# move and scale a bit
plot(dend)
colored_bars(dend, cols, y_shift = -1,
rowLabels = "Odd\n numbers")
# Now let's cut the tree and add that info to the plot:
k2 <- cutree(dend, k = 2)
cols2 <- c("#0082CE", "#CC476B")[k2]
plot(dend)
# sadly, the shift paramteres need to be handled manually...
colored_bars(dend, cbind(cols2, cols), y_shift = -1,
rowLabels = c("2 clusters", "Odd numbers"),
text_shift = 1)
# let's add clusters color
# notice how we need to play with the colors a bit
# this is because color_branches places colors from
# left to right. Which means we need to give colored_bars
# the colors of the items so that ofter sorting they would be
# from left to right. Here is how it can be done:
the_k <- 3
library(colorspace)
cols3 <- rainbow_hcl(the_k, c=90, l=50)
dend %>% update("branches_k_color", k = the_k, with = cols3) %>% plot
kx <- cutree(dend, k = the_k)
ord <- order.dendrogram(dend)
kx <- sort_levels_values(kx[ord])
kx <- kx[match(seq_along(ord), ord)]
colored_bars(dend, cbind(cols3[kx], cols2, cols),
y_shift = -1, y_scale = 1.4,
rowLabels = c("3 clusters", "2 clusters", "Odd numbers"),
text_shift = 1)Run the code above in your browser using DataLab