Title: | Extra Functions to Cut, Label and Colour Dendrogram Clusters |
---|---|
Description: | Provides extra functions to manipulate dendrograms that build on the base functions provided by the 'stats' package. The main functionality it is designed to add is the ability to colour all the edges in an object of class 'dendrogram' according to cluster membership i.e. each subtree is coloured, not just the terminal leaves. In addition it provides some utility functions to cut 'dendrogram' and 'hclust' objects and to set/get labels. |
Authors: | Gregory Jefferis [aut, cre] |
Maintainer: | Gregory Jefferis <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.2.3 |
Built: | 2024-11-16 05:04:54 UTC |
Source: | https://github.com/jefferis/dendroextras |
Extra functions to cut, label and colour dendrogram clusters
dendroextras
provides the slice
function as an
alternative to the base cut
function. In contrast to
cut
, slice
returns group membership in dendrogram
order i.e. the first element in the group vector that is returned will be
the leftmost member of the leftmost cluster (cluster #1).
dendroextras
provides colour_clusters
to colour all of
the edges forming clusters cut by height or number of groups. You can also
set and retrieve the leaf colours (i.e. the terminal nodes) using
set_leaf_colours
and leaf_colours
.
dendroextras
provides labels
and
labels<-
methods to get and set the labels of cluster
members.
dendrogram, hclust
in stats
package.
The distinctive feature of this function is to colour both the
terminal leaves of a cluster and the edges leading to those leaves.
The edgePar attribute of nodes will be augmented by a new list item col.
The groups will be defined by a call to slice
using the k or h
parameters.
colour_clusters(d, k = NULL, h = NULL, col = rainbow, groupLabels = NULL) color_clusters(d, k = NULL, h = NULL, col = rainbow, groupLabels = NULL)
colour_clusters(d, k = NULL, h = NULL, col = rainbow, groupLabels = NULL) color_clusters(d, k = NULL, h = NULL, col = rainbow, groupLabels = NULL)
d |
A |
k |
number of groups (passed to |
h |
height at which to cut tree (passed to |
col |
Function or vector of colours |
groupLabels |
If TRUE add numeric group label - see Details for options |
If groupLabels=TRUE
then numeric group labels will added to
each cluster. If a vector is supplied then these entries will be used as
the group labels. If a function is supplied then it will be passed a
numeric vector of groups (e.g. 1:5) and must return the formatted group
labels.
a tree object of class dendrogram.
jefferis
d5=colour_clusters(hclust(dist(USArrests), "ave"),5) plot(d5) d5g=colour_clusters(hclust(dist(USArrests), "ave"),5,groupLabels=TRUE) plot(d5g) d5gr=colour_clusters(hclust(dist(USArrests), "ave"),5,groupLabels=as.roman) plot(d5gr)
d5=colour_clusters(hclust(dist(USArrests), "ave"),5) plot(d5) d5g=colour_clusters(hclust(dist(USArrests), "ave"),5,groupLabels=TRUE) plot(d5g) d5gr=colour_clusters(hclust(dist(USArrests), "ave"),5,groupLabels=as.roman) plot(d5gr)
NB will return labels in dendrogram order, not in the
order of the original labels retained in object$labels
ususally corresponding to the row or column names of
the dist
object provided to hclust
.
## S3 method for class 'hclust' labels(object, ...)
## S3 method for class 'hclust' labels(object, ...)
object |
hclust object from which to extract labels |
... |
Additional arguments (ignored) |
character vector of labels in dendrogram order
jefferis
hc <- hclust(dist(USArrests), "ave") dend <- as.dendrogram(hc) stopifnot(all.equal(labels(hc),labels(dend)))
hc <- hclust(dist(USArrests), "ave") dend <- as.dendrogram(hc) stopifnot(all.equal(labels(hc),labels(dend)))
Set the labels of an object
Set the labels of a dendrogram
labels(x,...) <- value labels(x,...) <- value
labels(x,...) <- value labels(x,...) <- value
x |
Object on which to set labels |
... |
Additional parameters passed to specific methods |
value |
New labels |
object of class dendrogram
jefferis
hc <- hclust(dist(USArrests), "ave") dend <- as.dendrogram(hc) labels(dend)<-abbreviate(labels(dend),minlength=2)
hc <- hclust(dist(USArrests), "ave") dend <- as.dendrogram(hc) labels(dend)<-abbreviate(labels(dend),minlength=2)
Return the leaf colours of a dendrogram
leaf_colours(d, col_to_return = c("edge", "node", "label"))
leaf_colours(d, col_to_return = c("edge", "node", "label"))
d |
the dendrogram |
col_to_return |
Character scalar - kind of colour attribute to return |
The returned colours will be in dendrogram order.
named character vector of colours, NA_character_ where missing
jefferis
d5=colour_clusters(hclust(dist(USArrests), "ave"),5) leaf_colours(d5)
d5=colour_clusters(hclust(dist(USArrests), "ave"),5) leaf_colours(d5)
Set the leaf colours of a dendrogram
set_leaf_colours(d, col, col_to_set = c("edge", "node", "label")) set_leaf_colors(d, col, col_to_set = c("edge", "node", "label"))
set_leaf_colours(d, col, col_to_set = c("edge", "node", "label")) set_leaf_colors(d, col, col_to_set = c("edge", "node", "label"))
d |
the dendrogram |
col |
Single colour or named character vector of colours. When NA no colour will be set. |
col_to_set |
Character scalar - kind of colour attribute to set |
jefferis
d5=colour_clusters(hclust(dist(USArrests), "ave"),5) dred=set_leaf_colours(d5,'red','edge') stopifnot(isTRUE(all(leaf_colours(dred)=='red'))) d52=set_leaf_colours(d5,leaf_colours(d5),'edge') stopifnot(all.equal(d5,d52))
d5=colour_clusters(hclust(dist(USArrests), "ave"),5) dred=set_leaf_colours(d5,'red','edge') stopifnot(isTRUE(all(leaf_colours(dred)=='red'))) d52=set_leaf_colours(d5,leaf_colours(d5),'edge') stopifnot(all.equal(d5,d52))
In comparison with cutree, the groups are numbered from left to right as per the tree when plotted in its standard horizontal form. Note also that the return value will have the leaves sorted in dendrogram order.
slice(x, k = NULL, h = NULL, ...)
slice(x, k = NULL, h = NULL, ...)
x |
tree like object |
k |
an integer scalar with the desired number of groups |
h |
numeric scalar with height where the tree should be cut |
... |
Additional parameters passed to methods |
a named vector with group memberships
jefferis
cutree,cut.dendrogram,rect.hclust
hc <- hclust(dist(USArrests), "ave") # return groups, leaves ordered by dendrogram slice(hc,k=5) # return groups, leaves ordered as originally passed to hclust slice(hc,k=5)[order(hc$order)]
hc <- hclust(dist(USArrests), "ave") # return groups, leaves ordered by dendrogram slice(hc,k=5) # return groups, leaves ordered as originally passed to hclust slice(hc,k=5)[order(hc$order)]