'Extract cluster from dendogram

I have a tree and I cut it into several clusters at a certain height.

I generate 5 clusters with cutree(), but they are not sorted according their position on the tree (e.g left to right) I want to know which cluster correspond to which branch of the tree. Can someone help me?

here a simple code:

data = USArrests
out = pheatmap(data)
plot(out$tree_row)
abline(h=80, col="red", lty=2, lwd=2)

enter image description here

clusters = sort(cutree(out$tree_row, h=80))

max(clusters)
 
clusters.dt = setDT(data.frame(group = as.numeric(clusters) , state = names(clusters), stringsAsFactors=FALSE))
clusters.dt = split(clusters.dt$"state",clusters.dt$"group", )
> clusters.dt
$`1`
 [1] "Alabama"        "Alaska"         "Arizona"        "California"     "Delaware"       "Illinois"       "Louisiana"      "Maryland"       "Michigan"       "Mississippi"    "Nevada"         "New Mexico"     "New York"       "South Carolina"

$`2`
 [1] "Arkansas"      "Colorado"      "Georgia"       "Massachusetts" "Missouri"      "New Jersey"    "Oklahoma"      "Oregon"        "Rhode Island"  "Tennessee"     "Texas"         "Virginia"      "Washington"    "Wyoming"

$`3`
 [1] "Connecticut"  "Idaho"        "Indiana"      "Kansas"       "Kentucky"     "Montana"      "Nebraska"     "Ohio"         "Pennsylvania" "Utah"

$`4`
[1] "Florida"        "North Carolina"

$`5`
 [1] "Hawaii"        "Iowa"          "Maine"         "Minnesota"     "New Hampshire" "North Dakota"  "South Dakota"  "Vermont"       "West Virginia" "Wisconsin"


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source