Adjust the Positions of nodes/leaves in the Dendrogram

adjust_dend_by_x(dend, leaf_pos = 1:nobs(dend)-0.5)

Arguments

dend

A dendrogram object.

leaf_pos

A vector of positions of leaves. The value can also be a unit object.

Details

The positions of nodes stored as x attribute are recalculated based on the new positions of leaves.

By default, the position of leaves are at 0.5, 1.5, ..., n-0.5.

Examples

m = matrix(rnorm(100), 10) dend = as.dendrogram(hclust(dist(m))) dend = adjust_dend_by_x(dend, sort(runif(10))) str(dend)
#> --[dendrogram w/ 2 branches and 10 members at h = 5.73] #> |--[dendrogram w/ 2 branches and 3 members at h = 4.1] #> | |--leaf 10 ( x = 0.00802 ) #> | `--[dendrogram w/ 2 branches and 2 members at h = 2.38] #> | |--leaf 1 ( x = 0.0191 ) #> | `--leaf 9 ( x = 0.125 ) #> `--[dendrogram w/ 2 branches and 7 members at h = 5.03] #> |--[dendrogram w/ 2 branches and 2 members at h = 3.62] #> | |--leaf 3 ( x = 0.151 ) #> | `--leaf 8 ( x = 0.219 ) #> `--[dendrogram w/ 2 branches and 5 members at h = 4.11] #> |--[dendrogram w/ 2 branches and 2 members at h = 2.6] #> | |--leaf 4 ( x = 0.261 ) #> | `--leaf 5 ( x = 0.335 ) #> `--[dendrogram w/ 2 branches and 3 members at h = 3.39] #> |--leaf 2 ( x = 0.428 ) #> `--[dendrogram w/ 2 branches and 2 members at h = 2.2] #> |--leaf 6 ( x = 0.619 ) #> `--leaf 7 ( x = 0.69 )
dend = adjust_dend_by_x(dend, unit(1:10, "cm")) str(dend)
#> --[dendrogram w/ 2 branches and 10 members at h = 5.73] #> |--[dendrogram w/ 2 branches and 3 members at h = 4.1] #> | |--leaf 10 ( x = 1cm ) #> | `--[dendrogram w/ 2 branches and 2 members at h = 2.38] #> | |--leaf 1 ( x = 2cm ) #> | `--leaf 9 ( x = 3cm ) #> `--[dendrogram w/ 2 branches and 7 members at h = 5.03] #> |--[dendrogram w/ 2 branches and 2 members at h = 3.62] #> | |--leaf 3 ( x = 4cm ) #> | `--leaf 8 ( x = 5cm ) #> `--[dendrogram w/ 2 branches and 5 members at h = 4.11] #> |--[dendrogram w/ 2 branches and 2 members at h = 2.6] #> | |--leaf 4 ( x = 6cm ) #> | `--leaf 5 ( x = 7cm ) #> `--[dendrogram w/ 2 branches and 3 members at h = 3.39] #> |--leaf 2 ( x = 8cm ) #> `--[dendrogram w/ 2 branches and 2 members at h = 2.2] #> |--leaf 6 ( x = 9cm ) #> `--leaf 7 ( x = 10cm )