| ClustreePlotAtomic | R Documentation |
Core implementation for clustree (clustering tree) plots. This internal
function renders a tree-based visualisation showing how cluster assignments
change across increasing clustering resolutions. It is called by the
exported ClustreePlot() function.
ClustreePlotAtomic(
data,
prefix,
flip = FALSE,
alpha = 0.85,
palette = "Paired",
palcolor = NULL,
palreverse = FALSE,
edge_palette = "Spectral",
edge_palcolor = NULL,
aspect.ratio = 1,
legend.position = "right",
legend.direction = "vertical",
title = NULL,
subtitle = NULL,
xlab = NULL,
ylab = NULL,
expand = c(0.1, 0.1),
theme = "theme_this",
theme_args = list(),
...
)
data |
A data frame. |
prefix |
A character string specifying the common prefix of the
resolution columns in |
flip |
A logical value. If |
alpha |
A numeric value in |
palette |
A character string specifying the palette to use.
A named list or vector can be used to specify the palettes for different |
palcolor |
A character string specifying the color to use in the palette.
A named list can be used to specify the colors for different |
palreverse |
A logical value indicating whether to reverse the palette. Default is FALSE. |
edge_palette |
A character string specifying the palette name for
the edge colour gradient. Edges are coloured by the number of
transitioning cells between clusters at adjacent resolutions, using
|
edge_palcolor |
A character vector of custom colours for the edge
colour gradient. When |
aspect.ratio |
A numeric value specifying the aspect ratio of the plot. |
legend.position |
A character string specifying the position of the legend.
if |
legend.direction |
A character string specifying the direction of the legend. |
title |
A character string specifying the title of the plot. A function can be used to generate the title based on the default title. This is useful when split_by is used and the title needs to be dynamic. |
subtitle |
A character string specifying the subtitle of the plot. |
xlab |
A character string specifying the x-axis label. |
ylab |
A character string specifying the y-axis label. |
expand |
The values to expand the x and y axes. It is like CSS padding. When a single value is provided, it is used for both axes on both sides. When two values are provided, the first value is used for the top/bottom side and the second value is used for the left/right side. When three values are provided, the first value is used for the top side, the second value is used for the left/right side, and the third value is used for the bottom side. When four values are provided, the values are used for the top, right, bottom, and left sides, respectively. You can also use a named vector to specify the values for each side. When the axis is discrete, the values will be applied as 'add' to the 'expansion' function. When the axis is continuous, the values will be applied as 'mult' to the 'expansion' function. See also https://ggplot2.tidyverse.org/reference/expansion.html |
theme |
A character string or a theme class (i.e. ggplot2::theme_classic) specifying the theme to use. Default is "theme_this". |
theme_args |
A list of arguments to pass to the theme function. |
... |
Additional arguments passed to |
The function expects a data frame containing columns named with a common
prefix followed by numeric resolution values (e.g. "res_0.1",
"res_0.3", "res_0.5"). Each column represents cluster
assignments at a given resolution, and the tree visualises how cells (rows)
transition between clusters as resolution increases.
The core layout is produced via clustree::clustree(), which computes
a ggraph layout (default: "sugiyama") linking clusters across
adjacent resolutions. Nodes represent clusters and edges represent cell
transitions; edge width and colour encode the number of transitioning cells.
Cluster labels are overlaid on each node via ggplot2::geom_text().
Key features:
Resolution-level node colouring — each resolution level
receives a distinct colour from palette via
ggplot2::scale_color_manual().
Edge gradient — edges are coloured by transition count
using ggraph::scale_edge_color_gradientn() with the
edge_palette gradient.
Flip support — flip = TRUE places resolutions on
the x-axis with cluster IDs as y-axis row labels for left-to-right
reading.
Automatic dimension calculation — plot height and width are sized based on the number of resolutions, number of clusters, and legend configuration, with different formulae for flipped vs. non-flipped layouts.
A ggplot object with height and width
attributes.
ClustreePlotAtomic executes the following steps:
Column selection — selects all columns whose names start
with prefix via dplyr::starts_with().
NA filtering — removes rows with any missing values via
stats::complete.cases().
Expansion normalisation — norm_expansion()
parses the expand argument for continuous x and y axes.
Empty data guard — stops with an informative error if no columns match the prefix or no complete rows remain.
Resolution parsing — extracts the suffix after
prefix from each column name and strips leading separators
("_" or ".") if present. Values are converted to numeric
and sorted. Column names are rewritten to use the original prefix for
consistent clustree processing.
Cluster counting — computes the number of unique clusters
at the highest resolution (max_clusters), used for dimension
calculation in the non-flipped layout.
Argument merging — builds a call to
clustree::clustree() by merging user-supplied ... with
sensible defaults for node_alpha, edge_width,
show_axis, layout ("sugiyama"),
node_size_range (c(6, 12)), node_text_size
(3), and node_text_colour ("black").
clustree execution — calls
gglogger::register(clustree::clustree) with the merged arguments
to produce the base ggplot object.
Node labels — overlays ggplot2::geom_text() with
the cluster label from the clustree output, using the configured
text size and colour.
Resolution colour scale — applies
ggplot2::scale_color_manual() with one colour per resolution
level using palette_this(). The colour legend is suppressed (the
edge gradient legend serves as the primary guide).
Edge colour scale — applies
ggraph::scale_edge_color_gradientn() with the
edge_palette palette, encoding transition counts via a
colour-bar guide with black frame and ticks.
Theme and labels — applies the selected theme and sets
title, subtitle, x-axis label, and y-axis label (default
y-label is the prefix with trailing separator stripped).
Flip branch (flip = TRUE):
Resolutions are placed on the x-axis (horizontal); clusters become y-axis rows.
Width scales with max(3, 0.5 + nres * 1.0); height
scales with max(3, 0.5 + max_clusters * 0.5) (or via
aspect.ratio).
scale_y_reverse() + coord_flip() reorients the
layout so resolutions read left-to-right.
Horizontal grid lines are dashed grey80; y-axis text and ticks are suppressed.
Non-flip branch (flip = FALSE, default):
Resolutions are on the y-axis (rows); clusters spread across the x-axis (columns).
Width scales with max(3, 0.5 + max_clusters * 0.5);
height scales with max(3, 0.5 + nres * 1.0)
(or via aspect.ratio).
coord_cartesian(clip = "off") and
scale_y_continuous() show resolution labels on the y-axis.
Vertical grid lines are dashed grey80; x-axis text and ticks are suppressed.
Dimension storage — calculate_plot_dimensions()
computes height and width attributes, stored on the
ggplot object.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.