Package 'dendroextras'

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-10-17 05:15:45 UTC
Source: https://github.com/jefferis/dendroextras

Help Index


Extra functions to cut, label and colour dendrogram clusters

Description

Extra functions to cut, label and colour dendrogram clusters

Cutting 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).

Colouring clusters

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.

Labels

dendroextras provides labels and labels<- methods to get and set the labels of cluster members.

See Also

dendrogram, hclust in stats package.


Colour sub-clusters of a tree (dendrogram/hclust) object

Description

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.

Usage

colour_clusters(d, k = NULL, h = NULL, col = rainbow,
  groupLabels = NULL)

color_clusters(d, k = NULL, h = NULL, col = rainbow, groupLabels = NULL)

Arguments

d

A dendrogram or hclust tree object

k

number of groups (passed to slice)

h

height at which to cut tree (passed to slice)

col

Function or vector of colours

groupLabels

If TRUE add numeric group label - see Details for options

Details

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.

Value

a tree object of class dendrogram.

Author(s)

jefferis

See Also

slice,cutree,dendrogram

Examples

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)

Find labels of hclust object (in dendrogram order)

Description

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.

Usage

## S3 method for class 'hclust'
labels(object, ...)

Arguments

object

hclust object from which to extract labels

...

Additional arguments (ignored)

Value

character vector of labels in dendrogram order

Author(s)

jefferis

See Also

labels,hclust

Examples

hc <- hclust(dist(USArrests), "ave")
dend <- as.dendrogram(hc)
stopifnot(all.equal(labels(hc),labels(dend)))

Set the labels of an object

Description

Set the labels of an object

Set the labels of a dendrogram

Usage

labels(x,...) <- value

labels(x,...) <- value

Arguments

x

Object on which to set labels

...

Additional parameters passed to specific methods

value

New labels

Value

object of class dendrogram

Author(s)

jefferis

See Also

dendrogram,labels

Examples

hc <- hclust(dist(USArrests), "ave")
dend <- as.dendrogram(hc)
labels(dend)<-abbreviate(labels(dend),minlength=2)

Return the leaf colours of a dendrogram

Description

Return the leaf colours of a dendrogram

Usage

leaf_colours(d, col_to_return = c("edge", "node", "label"))

Arguments

d

the dendrogram

col_to_return

Character scalar - kind of colour attribute to return

Details

The returned colours will be in dendrogram order.

Value

named character vector of colours, NA_character_ where missing

Author(s)

jefferis

See Also

slice,colour_clusters

Examples

d5=colour_clusters(hclust(dist(USArrests), "ave"),5)
leaf_colours(d5)

Set the leaf colours of a dendrogram

Description

Set the leaf colours of a dendrogram

Usage

set_leaf_colours(d, col, col_to_set = c("edge", "node", "label"))

set_leaf_colors(d, col, col_to_set = c("edge", "node", "label"))

Arguments

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

Author(s)

jefferis

See Also

slice,colour_clusters

Examples

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))

Cut a tree-like object into groups numbered in tree order

Description

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.

Usage

slice(x, k = NULL, h = NULL, ...)

Arguments

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

Value

a named vector with group memberships

Author(s)

jefferis

See Also

cutree,cut.dendrogram,rect.hclust

Examples

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)]