
Internal helper to create a 3D dimension reduction plot using plotly
Source:R/dimplot.R
DimPlotAtomic3D.RdRenders an interactive 3D scatter plot via plotly for dimension reduction
data with three ordination axes. Called automatically by DimPlotAtomic()
when length(dims) == 3.
The function supports two visualisation modes:
group_by — discrete group colouring with one trace per group, matching the 2D legend behaviour via
label_use.features — continuous colour scale with a
plotlycolorscale bar.
Graphs / networks are rendered as 3D line segments (with NA separators for
line breaks). Lineage curves are fitted via stats::loess() per lineage
column and drawn as smoothed paths. For datasets exceeding 100 000 points,
per-point hover text is disabled to reduce the JSON payload.
Usage
DimPlotAtomic3D(
data,
dims,
group_by = NULL,
features = NULL,
colorby,
colors = NULL,
feat_colors_value = NULL,
label_use = NULL,
labels_tb = NULL,
keep_empty_group = FALSE,
bg_color = "grey80",
color_name = "",
pt_size = NULL,
pt_alpha = 1,
show_stat = TRUE,
title = NULL,
subtitle = NULL,
xlab = NULL,
ylab = NULL,
label = FALSE,
label_insitu = FALSE,
label_size = 4,
label_fg = "white",
label_bg = "black",
highlight = NULL,
highlight_color = "black",
highlight_size = 1,
highlight_stroke = 0.8,
highlight_alpha = 1,
graph = NULL,
edge_size = c(0.05, 0.5),
edge_alpha = 0.1,
edge_color = "grey40",
lineages = NULL,
lineages_trim = c(0.01, 0.99),
lineages_span = 0.75,
lineages_palette = "Dark2",
lineages_palcolor = NULL,
palette = "Spectral",
palcolor = NULL,
palreverse = FALSE,
n_sampled = NULL
)Arguments
- data
A data frame.
- dims
A character vector of the column names to plot on the x, y (and optionally z) axes or a numeric vector of the column indices. When 3 dimensions are provided, a 3D interactive plot is created using plotly. Supported in 3D: group_by, features, labels, highlight, lineages, graph/network, show_stat, order. Not supported in 3D: add_mark, stat_by, add_density, velocity, hex, facet_by, raster.
- group_by
A character string of the column name to group the data by for discrete colouring. A character/factor column is expected. If multiple columns are provided, the columns will be concatenated with
group_by_sep.- features
A character vector of the column names to plot as features (continuous colouring). When multiple features are provided and
facet_byis not set, the data is pivoted to long format and faceted by feature name.- colorby
A character string naming the column used for colour mapping (either the
group_byfactor or thefeaturesnumeric column).- colors
A named character vector mapping group levels to hex colours. Only used in group_by mode.
- feat_colors_value
A numeric vector of the colour-scale anchor values for feature mode, as returned by
prepare_continuous_color_scale().- label_use
A character vector of formatted legend labels (with optional count annotations) used in group_by mode.
- labels_tb
A table of group counts for trace iteration in group_by mode.
- keep_empty_group
A logical value. If
TRUE, empty factor levels are preserved; traces with zero rows are still added to the legend.- bg_color
A character string specifying the colour used for NA-valued points and background context points drawn from other facets. Default is
"grey80".- color_name
A character string used as the title for the continuous colour bar in feature mode. Default is
"".- pt_size
A numeric value for point size, or a character string naming a numeric column in
datato map point sizes to. IfNULL(default), auto-calculated asmin(3000 / nrow(data), 0.6). Column-name mapping only applies to 2D plots and is ignored with a message whenraster = TRUE.- pt_alpha
A numeric value in
[0, 1]for the point transparency. Default is1.- show_stat
A logical value. If
TRUE(default), the number of points per group is shown in the legend labels and subtitle. Ignored whentheme = "theme_blank".- 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.
- label
A logical value. If
TRUE, group labels (numeric indices by default, or group names whenlabel_insitu = TRUE) are placed at the median coordinates of each group. Forced toTRUEwhenlabel_repelorlabel_insituis set.- label_insitu
A logical value. If
TRUE, the raw group names are placed at the group median coordinates instead of numeric indices. Forceslabel = TRUE. Default isFALSE.- label_size
A numeric value for the label text size. Passed to
ggrepel::geom_text_repel(). Default is4.- label_fg
A character string for the label text (foreground) colour. Default is
"white".- label_bg
A character string for the label background / outline colour. Default is
"black".- highlight
A specification for highlighted points:
NULL(default): no highlighting.TRUE: highlight all points (adds a dark outline around every point).A character string: a dplyr filter expression (e.g.,
"clusters == 'Ductal'").A character vector: row names to highlight.
A numeric vector: row indices to highlight.
- highlight_color
A character string for the colour of the outer highlight ring. Default is
"black".- highlight_size
A numeric value for the size of the inner (coloured) highlight point. Default is
1.- highlight_stroke
A numeric value for the thickness of the outer highlight ring (the difference between the outer ring size and
highlight_size). Default is0.8.- highlight_alpha
A numeric value in
[0, 1]for the transparency of highlighted points. Default is1.- graph
A specification for network / graph edges to overlay. Sources:
A character string starting with
"@"(e.g.,"@graph"): extracts the attribute named"graph"fromattributes(data).A
Graphobject (e.g., Seurat): coerced to dense matrix viaas.matrix().A
matrix,data.frame, ordgCMatrix: used directly as the adjacency matrix.Numeric indices or character column names: extracts columns from
data. Edges are drawn for non-zero, lower-triangle entries. Requiresdatato have row names matching the matrix dimnames.
- edge_size
A numeric vector of length 2 specifying the range
[min, max]forscale_linewidth_continuous(range = ...)applied to edge widths. Default isc(0.05, 0.5).- edge_alpha
A numeric value in
[0, 1]for the transparency of graph edges. Default is0.1.- edge_color
A character string for the colour of graph edges. Default is
"grey40".- lineages
A character vector of column names representing pseudotime / trajectory lineages. Each column is fitted with a LOESS smooth (
span = lineages_span, degree = 2) across the 2D embedding, after trimming the top and bottomlineages_trimquantiles. Only supported in 2D withoutfacet_by.- lineages_trim
A numeric vector of length 2 specifying the lower and upper quantile thresholds
[0, 1]for trimming lineage values before LOESS fitting. Default isc(0.01, 0.99).- lineages_span
A numeric value passed as
spantostats::loess()controlling the smoothness of the lineage curve. Smaller values follow the data more closely. Default is0.75.- lineages_palette
A character string naming the palette for lineage colours. Default is
"Dark2".- lineages_palcolor
A character vector of specific colours for lineage curves. Default is
NULL(auto-resolved fromlineages_palette).- palette
A character string specifying the palette to use. A named list or vector can be used to specify the palettes for different
split_byvalues.- palcolor
A character string specifying the color to use in the palette. A named list can be used to specify the colors for different
split_byvalues. If some values are missing, the values from the palette will be used (palcolor will be NULL for those values).- palreverse
A logical value indicating whether to reverse the palette. Default is FALSE.
- n_sampled
An integer indicating how many points were retained after down-sampling (displayed in the subtitle).
NULLif no sampling occurred.
Architecture
DimPlotAtomic3D executes the following steps:
plotly availability check — errors early if the
plotlypackage is not installed.Axis labels — defaults
xlab/ylab/zlabfromdims.Marker size scaling — multiplies
pt_sizeby 2 to convert from ggplot2 mm units to plotly pixel units.Large-data detection — disables per-point hover text when
nrow(data) > 1e5.Base plotly object —
plotly::plot_ly()initialisation.Graph / network edges (if
graphis provided) — resolves the graph source (@attribute, Graph object, matrix, data.frame, column indices, or column names), aligns row/column names withdata, sets upper-triangle and zero entries toNA, melts into an edge data frame, builds x/y/z coordinate vectors (interleaved with NA for line breaks), and addsscatter3dline traces.Main scatter traces:
group_by mode: iterates over
labels_tbentries, creating one trace per group with the resolved colour. NA groups usebg_color. Legend labels uselabel_use(with count annotations ifshow_stat = TRUE).features mode: builds a plotly-style colorscale from the continuous palette. NA points are rendered first in
bg_colorwithout a colour bar; non-NA points follow with ashowscale = TRUEcolour bar.
Highlight — if
highlightis specified, highlighted points are plotted as open circles with increased marker size (to account for stroke). SupportsTRUE(all points), a filter expression string, or a vector of row names / numeric indices.Lineages — each lineage column is validated, trimmed to
[lineages_trim[1], lineages_trim[2]]quantiles, ordered by value, and aloess(span = lineages_span, degree = 2)smooth is fitted per dimension. The smoothed curve is rendered as a 3D line trace.Labels — when
label = TRUEandgroup_byis set, group median coordinates are computed viaaggregate(). In 3D, plotly does not support text background/outline, solabel_bgis used directly as the text colour. Text size is scaled by 3× for plotly.Layout — subtitle is composited with an optional "Showing N sampled points" note. The full title is rendered as HTML (
<br><sup>...</sup>). The scene setsaspectratio = list(x = 1, y = 1, z = 1)and axis titles.