Ordering by keeping defined groups together
Source:R/group_constrained_order.R
group_constrained_order.RdThis function modifies the order of a vector so that specified groups of elements remain together. This is useful for keeping specific dendrogram leaves together. The location of each group block is determined by the earliest member of that group in original vector. For elements not involved in any group, their relative order is unchanged.
Usage
group_constrained_order(x, groups, group_order = c("groups", "original"))Examples
set.seed(123)
# data
mat <- matrix(rnorm(8 * 4), nrow = 8)
rownames(mat) <- LETTERS[1:8]
# hierarchical clustering
hc <- hclust(dist(mat))
plot(hc, main = "Original dendrogram")
# groups of leaves to keep together (may not be adjacent in dendrogram)
groups <- list(
g1 = c("B", "F"),
g2 = c("C", "D", "E")
)
# Dendrogram original order
orig_order <- hc$labels[hc$order]
orig_order
#> [1] "C" "F" "H" "B" "E" "A" "D" "G"
# Get group constrained order
new_order <-
group_constrained_order(x = orig_order,
groups = groups,
group_order = "original")
new_order
#> [1] "C" "E" "D" "F" "B" "H" "A" "G"
# Reorder dendrogram labels
hc2 <- hc
hc2$order <- match(new_order, hc$labels)
plot(hc2, main = "Dendrogram with grouped leaves")