I am creating an hclust
object manually (i.e. creating a list with the required slots, then changing its class to hclust
). The merging pattern, heights of bifurcations, ordering of leaf nodes and labels of leaf nodes are known. My goal (and means of testing) is to plot the resulting dendrogram. I am unable to create a plottable hclust
object with my parameters.
The components of an hclust
object are described in the hclust
function documentation
here (see section Value).
The following is a reproducible chunk of R
code I use to generate and plot my dendrogram.
tree <- list()
tree$merge <- matrix(c( -1, -7, # row 1
-2, -6, # row 2
-3, -12, # row 3
-4, -14, # row 4
-5, -8, # row 5
-9, -11, # row 6
-13, -20, # row 7
-15, -19, # row 8
1, 8, # row 9
2, 5, # row 10
3, 6, # row 11
2, -18, # row 12
1, 3, # row 13
2, 4, # row 14
-10, 7, # row 15
-16, -17, # row 16
1, 2, # row 17
15, 16, # row 18
1, 15), # row 19
ncol = 2,
byrow = TRUE)
tree$height <- c(0.06573653, 0.06573653, 0.06573653, 0.06573653, 0.06573653, 0.06573653, 0.06573653, 0.06573653, 0.11167131, 0.11167131, 0.11167131, 0.12832304, 0.17304035, 0.17304035, 0.17304035, 0.17304035, 0.22965349, 0.22965349, 0.23334799)
tree$labels <- as.character(1:20)
tree$order <- c(1, 7, 15, 19, 3, 12, 9, 11, 2, 6, 5, 8, 18, 4, 14, 13, 20, 10, 16, 17)
class(tree) <- "hclust"
plot(tree)
Each row of the tree$merge
matrix corresponds to a bifurcation. Negative integers refer to the indices of a leaf nodes, whereas positive integers refer to existing clusters by row indices in tree$merge
.
Running the code results in the following error message.
Error in plot.hclust(tree) : 'merge' matrix has invalid contents
A sketch of the intended result is pictured below, with the heights
values marked by additional dotted lines. (The drawing is not to scale.)