0

I created a dendrogram using the 'recluster.cons' function of the recluster package. I would like to know how to color the branches of the dendrogram by group resulting from this function.

tree <- recluster.cons(sp2, p=1)$cons # sp2 is a presence-absence matrix
plot(tree, direction="downwards")

Here is the current dendrogram:

Dendogram generated

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
KJuliete
  • 7
  • 4
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 10 '20 at 21:11

1 Answers1

2

You need to define how many clusters you want to get from the clustering (like cutree), and then using dendextend seems like an easier option. First I simulate a dataset that might look like yours:

library(recluster)
set.seed(222)
testdata = lapply(1:3,function(i){
truep = runif(200)
replicate(7,rbinom(200,size=1,prob=truep))
})
testdata = t(do.call(cbind,testdata))
rownames(testdata) = paste0(rep(letters[1:3],each=7),rep(1:7,3))

We plot it, 3 clusters of sites because it was simulated as such:

tree <- recluster.cons(sp2, p=1)$cons # sp2 is a presence-absence matrix
plot(tree,direction="downwards")

enter image description here

Then colour it:

dendextend
dend <- color_branches(as.dendrogram(tree),k=3)
plot(dend)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72