This function takes a vector with cluster labels and sorts it by frequency such that the most frequent cluster is the first one and so on.

sort_clusters(clusters, map_subset = NULL)



A vector with cluster labels.


A logical vector of length equal to clusters specifying which elements of clusters to use to determine the ranking of the clusters.


A factor of length equal to clusters where the levels are the new ordered clusters and the names of the factor are the original values from clusters.


## Build an initial set of cluster labels
clus <- letters[unlist(lapply(4:1, function(x) rep(x, x)))]

## In this case, it's a character vector
#> [1] "character"

## Sort them and obtain a factor
#> d d d d c c c b b a 
#> 1 1 1 1 2 2 2 3 3 4 
#> Levels: 1 2 3 4