% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/dotplot.R
\name{DotPlot}
\alias{DotPlot}
\alias{LollipopPlot}
\title{Dot Plot, Scatter Plot, and Lollipop Plot}
\usage{
DotPlot(
  data,
  x,
  y,
  x_sep = "_",
  y_sep = "_",
  flip = FALSE,
  split_by = NULL,
  split_by_sep = "_",
  size_name = NULL,
  fill_name = NULL,
  fill_cutoff_name = NULL,
  add_bg = FALSE,
  bg_palette = "stripe",
  bg_palcolor = NULL,
  bg_alpha = 0.2,
  bg_direction = c("vertical", "horizontal", "v", "h"),
  size_by = NULL,
  fill_by = NULL,
  fill_cutoff = NULL,
  palreverse = FALSE,
  size_min = 1,
  size_max = 10,
  theme = "theme_this",
  theme_args = list(),
  palette = "Spectral",
  palcolor = NULL,
  alpha = 1,
  border_color = "black",
  border_size = 0.5,
  border_alpha = 1,
  lower_quantile = 0,
  upper_quantile = 0.99,
  lower_cutoff = NULL,
  upper_cutoff = NULL,
  facet_by = NULL,
  facet_scales = "fixed",
  facet_ncol = NULL,
  facet_nrow = NULL,
  facet_byrow = TRUE,
  x_text_angle = 0,
  seed = 8525,
  aspect.ratio = 1,
  legend.position = "right",
  legend.direction = "vertical",
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  keep_na = FALSE,
  keep_empty = FALSE,
  combine = TRUE,
  nrow = NULL,
  ncol = NULL,
  byrow = TRUE,
  axes = NULL,
  axis_titles = axes,
  guides = NULL,
  design = NULL,
  ...
)

LollipopPlot(
  data,
  x,
  y,
  y_sep = NULL,
  flip = FALSE,
  split_by = NULL,
  split_by_sep = "_",
  size_name = NULL,
  fill_name = NULL,
  fill_cutoff_name = NULL,
  size_by = NULL,
  fill_by = NULL,
  fill_cutoff = NULL,
  palreverse = FALSE,
  size_min = 1,
  size_max = 10,
  theme = "theme_this",
  theme_args = list(),
  palette = "Spectral",
  palcolor = NULL,
  alpha = 1,
  border_color = "black",
  border_size = 0.5,
  border_alpha = 1,
  lower_quantile = 0,
  upper_quantile = 0.99,
  lower_cutoff = NULL,
  upper_cutoff = NULL,
  facet_by = NULL,
  facet_scales = "fixed",
  facet_ncol = NULL,
  facet_nrow = NULL,
  facet_byrow = TRUE,
  x_text_angle = 0,
  seed = 8525,
  aspect.ratio = 1,
  legend.position = "right",
  legend.direction = "vertical",
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  keep_na = FALSE,
  keep_empty = FALSE,
  combine = TRUE,
  nrow = NULL,
  ncol = NULL,
  byrow = TRUE,
  axes = NULL,
  axis_titles = axes,
  guides = NULL,
  design = NULL,
  ...
)
}
\arguments{
\item{data}{A data frame.}

\item{x}{A character string naming the column for the x-axis. Must be a
numeric column (bars extend from 0 to the data value).}

\item{y}{A character string naming the column for the y-axis. Must be a
factor or character column (each level gets a lollipop bar).}

\item{x_sep}{A character string used to join multiple \code{x} column values
into a single factor level. Only used when x is non-numeric and multiple
columns are provided. Default: \code{"_"}.}

\item{y_sep}{A character string used to join multiple \code{y} column values
into a single factor level. Only used when y is non-numeric and multiple
columns are provided. Default: \code{"_"}.}

\item{flip}{A logical value. If \code{TRUE}, the x and y axes are swapped via
\code{coord_flip()}. Dimension calculation accounts for the flip.
Default: \code{FALSE}.}

\item{split_by}{The column(s) to split data by and generate separate plots
for each level. The split column is processed for \code{keep_na} /
\code{keep_empty} before splitting.}

\item{split_by_sep}{A character string used to concatenate multiple
\code{split_by} column values. Default: \code{"_"}.}

\item{size_name}{A character string for the size legend title. When
\code{NULL} (the default), the \code{size_by} column name is used.}

\item{fill_name}{A character string for the fill colour-bar legend title.
When \code{NULL} (the default), the \code{fill_by} column name is used.}

\item{fill_cutoff_name}{A character string for the fill cutoff legend title
(shown when \code{fill_cutoff} is active). Defaults to
\code{"<fill_by> <fill_cutoff>"}, e.g. \code{"mpg < 18"}.}

\item{add_bg}{A logical value. If \code{TRUE}, alternating background
stripes are drawn behind the points via \code{\link{bg_layer}()}. The
striped axis is determined by \code{bg_direction}. Requires the striped
axis to be non-numeric. Default: \code{FALSE}.}

\item{bg_palette}{A character string specifying the palette for the
background stripe colours. Passed to \code{\link{bg_layer}()}.
Default: \code{"stripe"}.}

\item{bg_palcolor}{A character vector of colours for the background stripes.
Passed to \code{\link{bg_layer}()}. When \code{NULL} (default), colours
are derived from \code{bg_palette}.}

\item{bg_alpha}{A numeric value in \code{[0, 1]} for the transparency of
the background stripes. Default: \code{0.2}.}

\item{bg_direction}{A character string specifying which axis receives the
alternating background stripes. \code{"vertical"} (default) stripes by x
levels; \code{"horizontal"} stripes by y levels. Abbreviations \code{"v"}
and \code{"h"} are also accepted.}

\item{size_by}{A character string naming a numeric column whose values
control dot size. When \code{NULL} (the default), the per-combination
observation count is computed automatically (via \code{dplyr::summarise(n =
  n())}) and used as the size variable. If \code{fill_by} is also present,
the first value of \code{fill_by} per combination is retained with a
warning. A single numeric value is also accepted and sets a constant dot
size (used by \code{ScatterPlot}).}

\item{fill_by}{A character string naming a numeric column whose values
control the fill colour of the dots (and lollipop inner bars). A
continuous gradient from \code{palette} is applied via
\code{scale_fill_gradientn()}. When \code{NULL} (the default), all dots
are filled with a single constant colour from the middle of the palette.}

\item{fill_cutoff}{A string expression specifying which values of
\code{fill_by} to grey out. Format: an operator followed by a number,
e.g. \code{"< 18"}, \code{"<= 18"}, \code{"> 18"}, or \code{">= 18"}.
Values matching the condition are set to \code{NA} and rendered in grey
(\code{"grey80"}), while the rest are coloured by the fill gradient. The
operator determines which side of the threshold is greyed out,
independent of \code{palreverse}. A numeric value is also accepted as
shorthand for \code{"<"} (e.g. \code{18} is equivalent to
\code{"< 18"}). Requires \code{fill_by} to be set.}

\item{palreverse}{A logical value indicating whether to reverse the palette. Default is FALSE.}

\item{size_min}{A numeric value for the smallest dot size in the
\code{scale_size(range = c(size_min, size_max))} range.
Default: \code{1}.}

\item{size_max}{A numeric value for the largest dot size in the
\code{scale_size(range = c(size_min, size_max))} range.
Default: \code{10}.}

\item{theme}{A character string or a theme class (i.e. ggplot2::theme_classic) specifying the theme to use.
Default is "theme_this".}

\item{theme_args}{A list of arguments to pass to the theme function.}

\item{palette}{A character string specifying the palette to use.
A named list or vector can be used to specify the palettes for different \code{split_by} values.}

\item{palcolor}{A character string specifying the color to use in the palette.
A named list can be used to specify the colors for different \code{split_by} values.
If some values are missing, the values from the palette will be used (palcolor will be NULL for those values).}

\item{alpha}{A numeric value specifying the transparency of the plot.}

\item{border_color}{Controls the dot border colour and lollipop outer-shadow
appearance:
\itemize{
\item \code{TRUE} — dot borders and lollipop inner bars follow the
\code{fill_by} gradient via \code{scale_color_gradientn()}; lollipop
outer shadow is black.
\item \code{"black"} (default) — constant black borders on dots and
black outer shadow on lollipop bars.
\item A colour string (e.g. \code{"red"}, \code{"#FF0000"}) — constant
colour for both dot borders and lollipop outer shadows.
\item \code{FALSE} — no dot borders and no lollipop outer shadow (the
inner coloured bars remain visible in lollipop mode).
}}

\item{border_size}{A numeric value for the stroke width of dot borders and
the base linewidth of lollipop bars. In lollipop mode, the outer shadow
uses \code{border_size * 4} and the inner bar uses \code{border_size * 2}.
Default: \code{0.5}.}

\item{border_alpha}{A numeric value in \code{[0, 1]} controlling the
transparency of dot borders and lollipop bar segments.
Default: \code{1}.}

\item{lower_quantile, upper_quantile}{Lower and upper quantiles for the continuous color/fill scale.
The actual cutoffs are determined by these quantiles when \code{lower_cutoff} and
\code{upper_cutoff} are \code{NULL}. Defaults: \code{lower_quantile = 0}, \code{upper_quantile = 0.99}.}

\item{lower_cutoff, upper_cutoff}{Explicit lower and upper cutoffs for the continuous color/fill scale.
When \code{NULL} (the default), the cutoffs are determined by \code{lower_quantile} and
\code{upper_quantile} via \code{\link[stats]{quantile}}. Values outside the
\code{[lower_cutoff, upper_cutoff]} range are clamped (winsorized) to the nearest cutoff value.}

\item{facet_by}{A character string specifying the column name of the data frame to facet the plot.
Otherwise, the data will be split by \code{split_by} and generate multiple plots
and combine them into one using \code{patchwork::wrap_plots}}

\item{facet_scales}{Whether to scale the axes of facets. Default is "fixed"
Other options are "free", "free_x", "free_y". See \code{ggplot2::facet_wrap}}

\item{facet_ncol}{A numeric value specifying the number of columns in the facet.
When facet_by is a single column and facet_wrap is used.}

\item{facet_nrow}{A numeric value specifying the number of rows in the facet.
When facet_by is a single column and facet_wrap is used.}

\item{facet_byrow}{A logical value indicating whether to fill the plots by row. Default is TRUE.}

\item{x_text_angle}{A numeric value specifying the angle of the x-axis text.}

\item{seed}{The random seed for reproducibility. Passed to
\code{validate_common_args()}. Default: \code{8525}.}

\item{aspect.ratio}{A numeric value specifying the aspect ratio of the plot.}

\item{legend.position}{A character string specifying the position of the legend.
if \code{waiver()}, for single groups, the legend will be "none", otherwise "right".}

\item{legend.direction}{A character string specifying the direction of the legend.}

\item{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.}

\item{subtitle}{A character string specifying the subtitle of the plot.}

\item{xlab}{A character string specifying the x-axis label.}

\item{ylab}{A character string specifying the y-axis label.}

\item{keep_na}{A logical value or a character to replace the NA values in the data.
It can also take a named list to specify different behavior for different columns.
If TRUE or NA, NA values will be replaced with NA.
If FALSE, NA values will be removed from the data before plotting.
If a character string is provided, NA values will be replaced with the provided string.
If a named vector/list is provided, the names should be the column names to apply the behavior to,
and the values should be one of TRUE, FALSE, or a character string.
Without a named vector/list, the behavior applies to categorical/character columns used on the plot,
for example, the \code{x}, \code{group_by}, \code{fill_by}, etc.}

\item{keep_empty}{One of FALSE, TRUE and "level". It can also take a named list to specify
different behavior for different columns. Without a named list, the behavior applies to the
categorical/character columns used on the plot, for example, the \code{x}, \code{group_by}, \code{fill_by}, etc.
\itemize{
\item{\code{FALSE} (default): Drop empty factor levels from the data before plotting.}
\item{\code{TRUE}: Keep empty factor levels and show them as a separate category in the plot.}
\item{\code{"level"}: Keep empty factor levels, but do not show them in the plot.
But they will be assigned colors from the palette to maintain consistency across multiple plots.
Alias: \code{levels}}
}}

\item{combine}{A logical value. If \code{TRUE} (the default), the list of
per-split plots is combined into a single \code{patchwork} object. If
\code{FALSE}, returns the raw list.}

\item{nrow, ncol, byrow}{Integers controlling the layout of combined plots via
\code{patchwork::wrap_plots()}. \code{byrow = TRUE} (default) fills the
layout row-wise.}

\item{axes, axis_titles}{Strings controlling how axes and axis titles are
handled across combined plots. Passed to \code{\link{combine_plots}()}.
See \code{?patchwork::wrap_plots} for options (\code{"keep"},
\code{"collect"}, \code{"collect_x"}, \code{"collect_y"}).}

\item{guides}{A string controlling guide collection across combined plots.
Passed to \code{\link{combine_plots}()}.}

\item{design}{A custom layout specification for combined plots. Passed to
\code{\link{combine_plots}()}. When specified, \code{nrow}, \code{ncol},
and \code{byrow} are ignored.}

\item{...}{Additional arguments.}
}
\value{
A \code{ggplot} object (single plot), a \code{patchwork} object
(when \code{combine = TRUE} with \code{split_by}), or a list of
\code{ggplot} objects (when \code{combine = FALSE}).

A \code{ggplot} object (single plot), a \code{patchwork} object
(when \code{combine = TRUE} with \code{split_by}), or a list of
\code{ggplot} objects (when \code{combine = FALSE}).
}
\description{
\code{DotPlot()} renders a matrix of filled circles (dot plot) where dot
size encodes one numeric variable and fill colour encodes another. Either
axis can be numeric or factor, enabling four layout combinations:

\itemize{
\item \strong{Both axes factor} — a classic dot matrix (e.g. genes × cell
types), where each cell is a dot whose size reflects expression magnitude
and whose colour reflects a summary statistic.
\item \strong{Both axes numeric} — a scatter plot, with dots positioned by
x/y coordinates, sized by a third variable, and coloured by a fourth.
\item \strong{One numeric, one factor} — a strip plot or (with
\code{lollipop = TRUE}) a lollipop chart.
}

\code{LollipopPlot()} is a convenience wrapper that sets
\code{lollipop = TRUE}, producing horizontal bars from the y-axis to each
data point, capped by filled dots. It expects a numeric \code{x} and a
factor/character \code{y}.

Key features:
\itemize{
\item \strong{Auto-count:} when \code{size_by = NULL}, the per-combination
observation count is computed automatically.
\item \strong{fill_cutoff:} values in \code{fill_by} matching a threshold
expression (e.g. \code{"< 18"}) are greyed out with a dedicated legend
entry.
\item \strong{Background stripes:} \code{add_bg = TRUE} draws alternating
background bands along the discrete axis for visual grouping.
\item \strong{Border modes:} \code{border_color} can track the fill
gradient (\code{TRUE}), use a constant colour (\code{"black"}), or be
suppressed (\code{FALSE}).
\item \strong{Colour scale trimming:} \code{lower_quantile} /
\code{upper_quantile} (or explicit \code{lower_cutoff} /
\code{upper_cutoff}) trim the continuous fill scale extremes.
}

\code{LollipopPlot()} is a convenience wrapper around \code{DotPlot()}
that sets \code{lollipop = TRUE}. It renders a horizontal bar extending
from the y-axis (\code{x = 0}) to each data point, capped by a filled dot.
The bar has a two-layer construction: an outer shadow (black or custom
colour) and an inner coloured segment that follows the \code{fill_by}
gradient. Dot size scales by \code{size_by} (or the per-combination
observation count when \code{size_by = NULL}).

Expects \code{x} to be a numeric column and \code{y} to be a factor or
character column.
}
\section{split_by Workflow (DotPlot)}{


When \code{split_by} is provided, the following pipeline executes:
\enumerate{
\item \strong{Column validation} — \code{check_columns()} resolves
\code{split_by} (force_factor, allow_multi, concat_multi).
\item \strong{NA / empty pre-processing} — \code{process_keep_na_empty()}
handles \code{keep_na} / \code{keep_empty} for the split column before
splitting, then removes the split column from the per-split
\code{keep_na}/\code{keep_empty} lists.
\item \strong{Data splitting} — splits \code{data} by \code{split_by}
levels (preserving factor level order).
\item \strong{Per-split palette / colour} — \code{check_palette()} and
\code{check_palcolor()} resolve per-split palette and colour overrides.
\item \strong{Per-split legend} — \code{check_legend()} resolves
\code{legend.position} and \code{legend.direction} per split.
\item \strong{Per-split title} — when \code{title} is a function, it
receives the default title (the split level name) and can return a
custom string; otherwise \code{title \%||\% split_level} is used.
\item \strong{Dispatch} — each split subset is passed to
\code{\link{DotPlotAtomic}} (with \code{lollipop = FALSE}).
\item \strong{Combination} — \code{\link{combine_plots}()} assembles the
list of plots via \code{patchwork::wrap_plots}, honouring
\code{nrow}/\code{ncol}/\code{byrow}/\code{design}.
}
}

\section{split_by Workflow (LollipopPlot)}{


Same pipeline as \code{DotPlot} above, but dispatches to
\code{\link{DotPlotAtomic}} with \code{lollipop = TRUE}.
}

\examples{
\donttest{
mtcars <- datasets::mtcars
mtcars$carb <- factor(mtcars$carb)
mtcars$gear <- factor(mtcars$gear)

# --- Basic dot plot (factor × factor, size + fill) ---
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18")
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "> 18")

# --- Background stripes ---
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18", add_bg = TRUE)
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18", add_bg = TRUE,
        bg_direction = "h")

# --- Faceting ---
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18", facet_by = "cyl")
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18", facet_by = "cyl",
        facet_scales = "free_x")

# --- split_by ---
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18", split_by = "cyl")
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18", split_by = "cyl",
        palette = list("4" = "Set1", "6" = "Paired", "8" = "Reds"))

# --- Scatter plot (both axes numeric) ---
DotPlot(mtcars, x = "qsec", y = "drat", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18",
        fill_cutoff_name = "Small mpgs")

# --- keep_na and keep_empty ---
mtcars$carb[mtcars$carb == "1"] <- NA
mtcars$gear[mtcars$gear == "3"] <- NA
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", fill_cutoff = "< 18",
        keep_na = TRUE, keep_empty = TRUE)

# --- Border customization ---
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", border_color = "red", border_size = 2)
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", border_color = TRUE, border_size = 1.5,
        border_alpha = 0.5)
DotPlot(mtcars, x = "carb", y = "gear",
        fill_by = "mpg", border_color = FALSE)

# --- Colour scale trimming ---
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", lower_quantile = 0.05, upper_quantile = 0.95)
DotPlot(mtcars, x = "carb", y = "gear", size_by = "wt",
        fill_by = "mpg", lower_cutoff = 15, upper_cutoff = 25)
}
\donttest{
mtcars <- datasets::mtcars

# --- Basic lollipop ---
LollipopPlot(mtcars, x = "qsec", y = "drat", size_by = "wt",
             fill_by = "mpg")

# --- Faceting ---
LollipopPlot(mtcars, x = "qsec", y = "drat", size_by = "wt",
             fill_by = "mpg", fill_cutoff = "< 18", facet_by = "cyl",
             facet_scales = "free_y")

# --- split_by ---
LollipopPlot(mtcars, x = "qsec", y = "drat", size_by = "wt",
             split_by = "vs", palette = list("0" = "Reds", "1" = "Blues"))

# --- Border customization ---
LollipopPlot(mtcars, x = "qsec", y = "drat", size_by = "wt",
             fill_by = "mpg", border_color = "red", border_size = 2)
LollipopPlot(mtcars, x = "qsec", y = "drat", size_by = "wt",
             fill_by = "mpg", border_color = TRUE, border_size = 1.5,
             border_alpha = 0.5)
LollipopPlot(mtcars, x = "qsec", y = "drat",
             fill_by = "mpg", border_color = FALSE)
}
}
