% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/boxviolinplot.R
\name{BoxViolinPlotAtomic}
\alias{BoxViolinPlotAtomic}
\title{Atomic Box / Violin / Bar / Beeswarm plot (internal)}
\usage{
BoxViolinPlotAtomic(
  data,
  x,
  x_sep = "_",
  y = NULL,
  base = c("box", "violin", "bar", "none"),
  in_form = c("long", "wide"),
  sort_x = NULL,
  flip = FALSE,
  keep_empty = FALSE,
  keep_na = FALSE,
  group_by = NULL,
  group_by_sep = "_",
  group_name = NULL,
  paired_by = NULL,
  x_text_angle = ifelse(isTRUE(flip), 0, 45),
  step_increase = 0.1,
  position_dodge_preserve = "total",
  fill_mode = ifelse(!is.null(group_by), "dodge", "x"),
  palreverse = FALSE,
  symnum_args = NULL,
  theme = "theme_this",
  theme_args = list(),
  palette = "Paired",
  palcolor = NULL,
  alpha = 1,
  aspect.ratio = NULL,
  legend.position = "right",
  legend.direction = "vertical",
  add_point = FALSE,
  pt_color = if (isTRUE(add_beeswarm)) NULL else "grey30",
  pt_size = NULL,
  pt_alpha = 1,
  y_nbreaks = 4,
  jitter_width = NULL,
  jitter_height = 0,
  stack = FALSE,
  y_max = NULL,
  y_min = NULL,
  y_trans = "identity",
  y_brackets = NULL,
  add_beeswarm = FALSE,
  beeswarm_method = "swarm",
  beeswarm_cex = 1,
  beeswarm_priority = "ascending",
  beeswarm_dodge = 0.9,
  add_box = FALSE,
  box_color = "black",
  box_width = 0.1,
  box_ptsize = 2.5,
  add_errorbar = "SEM",
  errorbar_color = "grey20",
  errorbar_width = 0.4,
  errorbar_linewidth = 0.6,
  add_trend = FALSE,
  trend_color = NULL,
  trend_linewidth = 1,
  trend_ptsize = 2,
  add_stat = NULL,
  stat_name = NULL,
  stat_color = "black",
  stat_size = 1,
  stat_stroke = 1,
  stat_shape = 25,
  add_bg = FALSE,
  bg_palette = "stripe",
  bg_palcolor = NULL,
  bg_alpha = 0.2,
  add_line = NULL,
  line_color = "red2",
  line_width = 0.6,
  line_type = 2,
  highlight = NULL,
  highlight_color = "red2",
  highlight_size = 1,
  highlight_alpha = 1,
  comparisons = NULL,
  ref_group = NULL,
  pairwise_method = "wilcox.test",
  multiplegroup_comparisons = FALSE,
  multiple_method = "kruskal.test",
  sig_label = "p.format",
  sig_labelsize = 3.5,
  hide_ns = FALSE,
  facet_by = NULL,
  facet_scales = "fixed",
  facet_ncol = NULL,
  facet_nrow = NULL,
  facet_byrow = TRUE,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  seed = 8525,
  ...
)
}
\arguments{
\item{data}{A data frame.}

\item{x}{A character string of column name(s) for the x-axis.
Character/factor columns are expected.  Multiple columns are
concatenated with \code{x_sep}.}

\item{x_sep}{A character string to join multiple \code{x} columns.
Default \code{"_"}.}

\item{y}{A character string of the numeric column for the y-axis.
Not required when \code{in_form = "wide"} (data values are taken from
the \code{x} columns).}

\item{base}{A character string specifying the primary plot type:
\code{"box"} (box plot), \code{"violin"} (violin plot), \code{"bar"}
(mean bars with optional error bars), or \code{"none"} (no primary
geometry, used by beeswarm plots).}

\item{in_form}{A character string: \code{"long"} (default) or
\code{"wide"}.  In wide form, \code{x} columns are pivoted to long
format.}

\item{sort_x}{An R expression string (e.g., \code{"mean(y)"}) to order
x-axis categories.  Default \code{NULL} keeps the original order.
When \code{keep_empty_x} is \code{TRUE}, empty levels are placed last.}

\item{flip}{Logical; if \code{TRUE}, swap the x and y axes.}

\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{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{group_by}{A character vector of column name(s) to dodge the
boxes/violins by.  Multiple columns are concatenated with
\code{group_by_sep}.}

\item{group_by_sep}{A character string to separate concatenated
\code{group_by} columns.  Default \code{"_"}.}

\item{group_name}{A character string for the dodge legend title.}

\item{paired_by}{A character string naming a column that identifies
paired observations.  Forces \code{add_point = TRUE} and connects
paired observations with lines.}

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

\item{step_increase}{Fractional step increase for stacking significance
brackets when multiple comparisons exist.}

\item{position_dodge_preserve}{Passed to
\code{\link[ggplot2]{position_dodge}()}: \code{"total"} preserves the
overall group width; \code{"single"} preserves individual element width.}

\item{fill_mode}{A character string controlling fill colour mapping:
\code{"dodge"} (fill by \code{group_by}, discrete),
\code{"x"} (fill by x-axis categories, discrete),
\code{"mean"} or \code{"median"} (fill by pre-computed statistic,
continuous gradient).}

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

\item{symnum_args}{A list of arguments passed to
\code{\link[stats]{symnum}} for symbolic p-value coding.}

\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{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{add_point}{Logical; add jittered or beeswarm points to the plot.}

\item{pt_color}{Colour of the points.  When \code{add_beeswarm = TRUE}
and \code{pt_color} is \code{NULL}, points are coloured by the fill
variable.}

\item{pt_size}{Numeric size of the points.  Default computed from
data size: \code{min(3000 / nrow(data), 0.6)}.}

\item{pt_alpha}{Numeric transparency of the points.}

\item{y_nbreaks}{Integer number of y-axis breaks.}

\item{jitter_width}{Numeric width of the jitter.  Defaults to
\code{0.5}, but set to \code{0} when \code{paired_by} is provided.}

\item{jitter_height}{Numeric height of the jitter.  Default \code{0}.}

\item{stack}{Logical; stack facetted panels in a compact layout with
shared strip labels.}

\item{y_max, y_min}{Numeric y-axis limits, or quantile notation strings
(e.g., \code{"q95"} for the 95th percentile, \code{"q5"} for the
5th percentile).}

\item{y_trans}{A character string for y-axis transformation
(e.g., \code{"log10"}).}

\item{y_brackets}{Numeric y-axis position for significance brackets
(or p-value labels for multiple comparisons).  If NULL, the brackets are placed above the maximum y-value.}

\item{add_beeswarm}{Logical; use \code{ggbeeswarm::geom_beeswarm()} for
non-overlapping point layout instead of jitter.  Requires the
\code{ggbeeswarm} package.}

\item{beeswarm_method}{Beeswarm layout method: \code{"swarm"},
\code{"compactswarm"}, \code{"hex"}, \code{"square"}, or
\code{"center"}.}

\item{beeswarm_cex}{Numeric scaling for point spacing.  Larger values
spread points more.}

\item{beeswarm_priority}{Point layout priority: \code{"ascending"},
\code{"descending"}, \code{"density"}, or \code{"random"}.}

\item{beeswarm_dodge}{Numeric dodge width for beeswarm points when
\code{group_by} is provided.  Default \code{0.9}.}

\item{add_box}{Logical; overlay a box plot on the primary geometry.
Mutually exclusive with \code{base = "box"} and \code{base = "bar"}.}

\item{box_color}{Colour of the overlaid box plot outline and fill.}

\item{box_width}{Width of the overlaid box plot.}

\item{box_ptsize}{Size of the median point in the overlaid box plot.}

\item{add_errorbar}{Type of error bars for bar plots (\code{base = "bar"}):
\code{"SEM"} (standard error of the mean, default), \code{"SD"}
(standard deviation), \code{"CI"} or \code{"CI95"} (95\\% confidence
interval), or \code{"none"}.  Silently ignored for non-bar bases.}

\item{errorbar_color}{Colour of the error bar lines and caps.}

\item{errorbar_width}{Width of the error bar caps.}

\item{errorbar_linewidth}{Line width of the error bars.}

\item{add_trend}{Logical; add trend lines connecting group medians.}

\item{trend_color}{Colour of the trend line.  When \code{NULL} and
\code{group_by} is present, lines are coloured per group.}

\item{trend_linewidth}{Width of the trend line.}

\item{trend_ptsize}{Size of the trend line points.}

\item{add_stat}{A summary function (e.g., \code{mean}, \code{median}) to
display as a point with a shape legend entry.}

\item{stat_name}{Legend title for the stat summary shape.}

\item{stat_color}{Colour of the stat summary point.}

\item{stat_size}{Size of the stat summary point.}

\item{stat_stroke}{Stroke width of the stat summary point.}

\item{stat_shape}{Shape (an integer) for the stat summary point.  Uses
\code{scale_shape_identity()} so the shape is rendered directly.}

\item{add_bg}{Logical; add alternating background stripes.}

\item{bg_palette}{Palette for the background stripes.}

\item{bg_palcolor}{Custom colours for the background stripes.}

\item{bg_alpha}{Alpha transparency for the background stripes.}

\item{add_line}{A numeric y-intercept for a horizontal reference line.}

\item{line_color}{Colour of the reference line.}

\item{line_width}{Width of the reference line.}

\item{line_type}{Linetype of the reference line.}

\item{highlight}{A specification of points to highlight: \code{TRUE}
(all), a numeric index vector, a logical expression string, or a
character vector of row names.}

\item{highlight_color}{Colour of highlighted points.}

\item{highlight_size}{Size of highlighted points.}

\item{highlight_alpha}{Alpha of highlighted points.}

\item{comparisons}{A logical value (\code{TRUE} for all pairs) or a list
of two-element vectors specifying pairwise comparisons.  Only available
when \code{fill_mode = "dodge"} (i.e., \code{group_by} is present).}

\item{ref_group}{A character string specifying the reference group for
comparisons.}

\item{pairwise_method}{Method for pairwise tests.  Default
\code{"wilcox.test"}.}

\item{multiplegroup_comparisons}{Logical; perform an omnibus test
(e.g., Kruskal-Wallis) across all groups.}

\item{multiple_method}{Method for the omnibus test.  Default
\code{"kruskal.test"}.}

\item{sig_label}{Label format for significance annotations.  For
pairwise comparisons: \code{"p.format"}, \code{"p.signif"}, or a
\pkg{glue} template (e.g., \code{"p = {p}"}).  For multiple-group
tests: \code{"p.format"} or \code{"p.signif"}.}

\item{sig_labelsize}{Size of the significance label text.}

\item{hide_ns}{Logical; hide non-significant comparison labels.}

\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{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{seed}{The random seed to use. Default is 8525.}

\item{...}{Additional arguments.}
}
\value{
A \code{ggplot} object, possibly faceted, with \code{height}
and \code{width} attributes (in inches) attached.
}
\description{
Core implementation for drawing box plots, violin plots, bar plots (mean
± error bars), or beeswarm plots.  This is the workhorse behind
\code{\link{BoxPlot}}, \code{\link{ViolinPlot}}, and
\code{\link{BeeswarmPlot}} — it takes a \strong{single} data frame (no
\code{split_by} support) and returns a \code{ggplot} object.

The \code{base} parameter selects the primary geometry:
\itemize{
\item \code{"box"} — \code{geom_boxplot()}
\item \code{"violin"} — \code{geom_violin()}
\item \code{"bar"} — \code{stat_summary(fun = mean, geom = "col")}
with optional error bars (SEM, SD, or CI).
\item \code{"none"} — no primary geometry (used by
\code{\link{BeeswarmPlot}} to draw beeswarm points alone).
}
}
\section{Architecture}{

\enumerate{
\item \strong{Wide-to-long conversion} — when \code{in_form = "wide"},
data is pivoted via \code{tidyr::pivot_longer()}.  The
\code{keep_na} / \code{keep_empty} lists are filtered to the new
column names.
\item \strong{Column resolution} — \code{x}, \code{y}, \code{group_by},
\code{facet_by}, and \code{paired_by} are validated via
\code{\link{check_columns}}.
\item \strong{NA / empty-level handling} — per-column
\code{keep_empty} settings are extracted for \code{x},
\code{group_by}, and \code{facet_by} independently.
\item \strong{Beeswarm validation} — if \code{add_beeswarm = TRUE},
the \code{ggbeeswarm} package is required.  Beeswarm is disabled
(with a warning) when \code{paired_by} is provided.
\item \strong{Paired data validation} — structural checks ensure each
(\code{x}, \code{paired_by}) combination has exactly 2
observations (one per group when \code{group_by} is present).
Paired observations force \code{add_point = TRUE}.
\item \strong{Summary statistics} — \code{.y_mean} and
\code{.y_median} are pre-computed per (\code{x}, \code{group_by},
\code{facet_by}) for use in trend lines and fill modes.
\item \strong{y-axis limits} — \code{y_max} / \code{y_min} accept
numeric values or quantile notation (\code{"q95"},
\code{"q5"}).  For bar plots, the limit is extended upward by
the error bar extent.
\item \strong{Highlight} — \code{highlight} can be \code{TRUE} (all
points), a numeric index vector, a logical expression string, or
a character vector of row names.
\item \strong{sort_x} — an R expression string (e.g.,
\code{"mean(y)"}) evaluated per x-level to reorder categories.
\item \strong{Flip transformation} — when \code{flip = TRUE}, factor
levels are reversed and \code{aspect.ratio} is inverted.
\item \strong{Base geometry} — the primary geom is added:
\code{geom_boxplot()}, \code{geom_violin()}, or
\code{stat_summary(fun = mean, geom = "col")}.  Error bars
(SEM / SD / CI) are layered on bar plots via a custom
\code{stat_summary(fun.data = ...)}.
\item \strong{Fill mode} — \code{fill_mode} controls colour mapping:
\itemize{
\item \code{"dodge"} — fill by \code{group_by} (discrete).
\item \code{"x"} — fill by x-axis categories (discrete).
\item \code{"mean"} / \code{"median"} — fill by pre-computed
mean/median (continuous gradient).
}
\item \strong{Box overlay} — when \code{add_box = TRUE} on a non-box
base, a box plot is overlaid via \code{ggnewscale::new_scale_fill()}
with a white fill/black outline.
\item \strong{Statistical comparisons} — two pathways:
\itemize{
\item \strong{Pairwise} (\code{comparisons}) — uses
\code{ggpubr::geom_pwc()} with automatic or explicit
comparison pairs.  Data is preprocessed to avoid test
failures from zero-variance or all-NA groups.
\item \strong{Multiple-group} (\code{multiplegroup_comparisons})
— uses \code{ggpubr::stat_compare_means()} for omnibus
tests (e.g., Kruskal-Wallis).
}
After comparison layers are added, \code{y_max_use} is expanded to
accommodate significance brackets.
\item \strong{Points} — jittered points (\code{geom_point()} with
\code{position_jitterdodge}) or beeswarm points
(\code{ggbeeswarm::geom_beeswarm()}).  Paired observations
add connecting lines (\code{geom_line()}) between matched
subjects.
\item \strong{Trend lines} — \code{stat_summary(fun = first)} draws
lines connecting group medians.  When \code{trend_color} is
\code{NULL} and \code{group_by} is present, lines are coloured
per group.
\item \strong{Reference lines} — \code{geom_hline()} at the specified
y-intercept.
\item \strong{Stat summary points} — a custom \code{stat_summary()}
point layer displaying a user-specified summary statistic
(e.g., mean) with a shape legend entry.
\item \strong{Stack layout} — when \code{stack = TRUE}, facets are
arranged with shared strip labels and negative panel spacing for
a compact stacked appearance.
\item \strong{Dimension calculation} — \code{calculate_plot_dimensions()}
accounts for the number of x-levels × dodge groups, flip state,
and stack layout adjustments.  Minimum dimensions are enforced
from label character widths.
\item \strong{Faceting} — \code{\link{facet_plot}()} wraps the result
with the appropriate strip position based on flip/stack state.
}
}

\keyword{internal}
