% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/densityplot.R
\name{DensityHistoPlotAtomic}
\alias{DensityHistoPlotAtomic}
\title{Atomic density/histogram plot}
\usage{
DensityHistoPlotAtomic(
  data,
  x,
  group_by = NULL,
  group_by_sep = "_",
  group_name = NULL,
  xtrans = "identity",
  ytrans = "identity",
  type = c("density", "histogram"),
  bins = NULL,
  binwidth = NULL,
  flip = FALSE,
  keep_na = FALSE,
  keep_empty = FALSE,
  add_bars = FALSE,
  bar_height = 0.025,
  bar_alpha = 1,
  bar_width = 0.1,
  position = "identity",
  use_trend = FALSE,
  add_trend = FALSE,
  trend_alpha = 1,
  trend_linewidth = 0.8,
  trend_pt_size = 1.5,
  trend_skip_zero = FALSE,
  palette = "Paired",
  palcolor = NULL,
  palreverse = FALSE,
  alpha = 0.5,
  theme = "theme_this",
  theme_args = list(),
  aspect.ratio = 1,
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  expand = c(bottom = 0, left = 0, right = 0),
  facet_by = NULL,
  facet_scales = "fixed",
  facet_ncol = NULL,
  facet_nrow = NULL,
  facet_byrow = TRUE,
  legend.position = ifelse(is.null(group_by), "none", "right"),
  legend.direction = "vertical",
  ...
)
}
\arguments{
\item{data}{A data frame.}

\item{x}{A character string specifying the column name for the x-axis values.
A numeric column is expected.}

\item{group_by}{A character string specifying the column(s) to group the data
by. Multiple columns are concatenated with \code{group_by_sep}. Each group
receives a distinct fill and outline colour.}

\item{group_by_sep}{A character string used to join multiple \code{group_by}
column values into a single factor level. Default: \code{"_"}.}

\item{group_name}{A character string used as the legend title for the
\code{group_by} aesthetic. When \code{NULL} (default), the (possibly concatenated)
\code{group_by} column name is used.}

\item{xtrans}{A character string specifying the transformation applied to
the x-axis. Passed to \code{ggplot2::scale_x_continuous(transform = ...)}.
Supported values include \code{"identity"} (default), \code{"log10"}, \code{"log2"},
\code{"sqrt"}, \code{"reverse"}, etc.}

\item{ytrans}{A character string specifying the transformation applied to
the y-axis. Passed to \code{ggplot2::scale_y_continuous(transform = ...)}.
Used by \code{trend_skip_zero} to correctly interpolate across zero bins on
a transformed scale. Default: \code{"identity"}.}

\item{type}{A character string specifying the plot type. \code{"density"} (default)
renders \code{geom_density()}; \code{"histogram"} renders \code{geom_histogram()} with
optional trend overlays.}

\item{bins}{A numeric value specifying the number of bins for the histogram.
Ignored when \code{type = "density"}. Defaults to \code{30} when neither \code{bins} nor
\code{binwidth} is provided.}

\item{binwidth}{A numeric value specifying the width of individual bins for
the histogram. Ignored when \code{type = "density"}. Takes precedence over
\code{bins} when both are set.}

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

\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{add_bars}{A logical value. If \code{TRUE}, a data-distribution rug is
drawn along the y = 0 axis using \code{geom_linerange()}. Each group's bars
are vertically offset to avoid overlap.}

\item{bar_height}{A numeric value specifying the height (in data units,
relative to the maximum y) of the rug bars added by \code{add_bars}.
The actual pixel height scales with \code{max_y}. Default: \code{0.025}.}

\item{bar_alpha}{A numeric value in \verb{[0, 1]} for the transparency of the
rug bars. Default: \code{1}.}

\item{bar_width}{A numeric value passed as the \code{linewidth} aesthetic of
\code{geom_linerange()}. Controls the thickness of each rug tick.
Default: \code{0.1}.}

\item{position}{A character string specifying the position adjustment for
the bars or density curves. Default: \code{"identity"}, which shows the
actual count / density per group (unlike \code{ggplot2}'s default \code{"stack"}).
Other options: \code{"stack"}, \code{"dodge"}, \code{"fill"}.}

\item{use_trend}{A logical value. If \code{TRUE}, the histogram bars are replaced
entirely by a trend line (points + connecting line). Only applies when
\code{type = "histogram"}.}

\item{add_trend}{A logical value. If \code{TRUE}, a trend line is overlaid on
top of the histogram bars. Only applies when \code{type = "histogram"}.}

\item{trend_alpha}{A numeric value in \verb{[0, 1]} controlling the transparency
of the trend points and line. Default: \code{1}.}

\item{trend_linewidth}{A numeric value for the thickness of the trend line.
Default: \code{0.8}.}

\item{trend_pt_size}{A numeric value for the size of the trend points.
Default: \code{1.5}.}

\item{trend_skip_zero}{A logical value. If \code{TRUE}, bins with zero count are
set to \code{NA} before the trend line is computed, and \code{zoo::na.approx()} is
used to interpolate across the gaps — producing a continuous curve even
when some bins are empty. Requires \code{ytrans} to be correctly specified.
Only applies when \code{type = "histogram"} and \code{use_trend} or \code{add_trend} is
active.}

\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{palreverse}{A logical value indicating whether to reverse the palette. Default is FALSE.}

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

\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{aspect.ratio}{A numeric value specifying the aspect ratio of the plot.}

\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{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 \url{https://ggplot2.tidyverse.org/reference/expansion.html}}

\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{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{...}{Additional arguments.}
}
\description{
Core implementation for density and histogram plots. This is the internal workhorse
dispatched by both the \code{DensityPlot()} and \code{Histogram()} public wrappers. It
renders a grouped density curve or histogram, with optional data-distribution
bars along the y=0 axis, trend-line interpolation (histogram only), and full
faceting support.

The function supports two plotting modes selected by \code{type}:
\itemize{
\item \strong{density} — renders \code{ggplot2::geom_density()} for each group.
\item \strong{histogram} — renders \code{ggplot2::geom_histogram()} with optional trend
overlays (\code{use_trend}, \code{add_trend}), including zero-skip interpolation
(\code{trend_skip_zero}) that uses \code{zoo::na.approx()} to bridge gaps where a
bin has zero observations under a transformed y-axis.
}

When \code{add_bars = TRUE}, a data rug is drawn along the bottom of the plot using
\code{ggplot2::geom_linerange()}. Each group's bars are offset vertically so they
stack without overlapping.
}
\section{Architecture}{

\strong{DensityHistoPlotAtomic} executes the following steps:
\enumerate{
\item \strong{ggplot dispatch} — selects \code{gglogger::ggplot} or \code{ggplot2::ggplot}
based on \code{getOption("plotthis.gglogger.enabled")}.
\item \strong{Type check} — \code{match.arg(type, c("density", "histogram"))}.
\item \strong{Expansion normalization} — \code{norm_expansion()} converts the \code{expand}
vector into CSS-padding-style x/y components.
\item \strong{Column resolution} — \code{check_columns()} validates \code{x}, \code{group_by}
(force_factor, allow_multi, concat_multi), and \code{facet_by}.
\item \strong{Default group} — when \code{group_by} is \code{NULL}, a synthetic \code{.group}
factor with a single empty-string level is created so the colour-mapping
pipeline runs uniformly.
\item \strong{Histogram bin default} — if \code{type = "histogram"} and neither \code{bins}
nor \code{binwidth} is set, \code{bins = 30} with a message.
\item \strong{Add-bars pre-calculation} — when \code{add_bars = TRUE}:
\itemize{
\item For density: max y = \code{max(density(x)$y) * 1.5}.
\item For histogram: max y = max bin count from \code{cut(x, s)}.
\item Computes \code{.ymin} and \code{.ymax} per row, offset by \code{bar_height * max_y}
for each group so rugs stack without colliding.
}
\item \strong{NA / empty handling} — \code{process_keep_na_empty()} filters data and
\code{keep_empty} values are extracted for group and facet dimensions.
\item \strong{Palette resolution} — \code{palette_this()} maps group levels to colours.
\item \strong{Base ggplot + scales} — initialises \code{ggplot(data, aes(x, fill, color))},
then adds \code{scale_fill_manual()} / \code{scale_color_manual()}. When
\code{keep_empty_group} is \code{TRUE}, \code{drop = FALSE}, \code{breaks}, and \code{limits}
are set to preserve empty factor levels.
\item \strong{Geometry layer}:
\itemize{
\item \emph{Histogram (no trend)}: \code{geom_histogram(alpha, bins, binwidth, position)}.
\item \emph{Histogram (use_trend / add_trend)}: adds \code{stat_bin(geom = "point")}
for trend points + \code{stat_bin(geom = "line")} for the trend curve.
When \code{trend_skip_zero = TRUE}, an \code{after_stat()} expression sets zero
counts to \code{NA}, transforms y, applies \code{zoo::na.approx()} per
\code{..group..}, and inverts — producing a continuous trend that
interpolates over empty bins.
\item \emph{Density}: \code{geom_density(alpha, position)}.
}
\item \strong{Add-bars geometry} — if \code{add_bars = TRUE}, \code{geom_linerange()} draws
vertical ticks at the pre-computed \code{.ymin} / \code{.ymax} positions.
\item \strong{Scales, theme, labels} — x/y continuous scales with transforms,
theme applied via \code{do_call(theme, theme_args)}, and axis / title
labels (default y-lab: "Count" for histogram, "Density" for density).
\item \strong{Flip} — optional \code{coord_flip()}.
\item \strong{Dimension calculation} — \code{calculate_plot_dimensions(base_height = 3.5, aspect.ratio, legend, flip)} sets \code{height} / \code{width} attributes.
\item \strong{Faceting} — \code{facet_plot()} applies \code{facet_grid} / \code{facet_wrap}.
}
}

\keyword{internal}
