% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/qqplot.R
\name{QQPlot}
\alias{QQPlot}
\title{QQ/PP plot}
\usage{
QQPlot(
  data,
  val,
  val_trans = NULL,
  type = c("qq", "pp"),
  split_by = NULL,
  split_by_sep = "_",
  band = NULL,
  line = list(),
  point = list(),
  fill_name = "Bands",
  band_alpha = 0.5,
  theme = "theme_this",
  theme_args = list(),
  palette = "Spectral",
  palcolor = NULL,
  palreverse = FALSE,
  facet_by = NULL,
  facet_scales = "fixed",
  facet_ncol = NULL,
  facet_nrow = NULL,
  facet_byrow = TRUE,
  aspect.ratio = 1,
  legend.position = waiver(),
  legend.direction = "vertical",
  title = NULL,
  subtitle = NULL,
  xlim = NULL,
  ylim = NULL,
  xlab = ifelse(type == "qq", "Theoretical Quantiles", "Probability Points"),
  ylab = ifelse(type == "qq", "Sample Quantiles", "Cumulative Probability"),
  combine = TRUE,
  nrow = NULL,
  ncol = NULL,
  byrow = TRUE,
  seed = 8525,
  axes = NULL,
  axis_titles = axes,
  guides = NULL,
  design = NULL,
  ...
)
}
\arguments{
\item{data}{A data frame.}

\item{val}{A character string naming the numeric column whose distribution
is compared against the theoretical distribution.}

\item{val_trans}{A transformation function applied to the \code{val}
column before plotting. For example, \code{log} or \code{sqrt}.
Default: \code{NULL} (no transformation).}

\item{type}{A character string specifying the plot type. Either
\code{"qq"} (quantile-quantile, the default) or \code{"pp"}
(probability-probability). Partial matching is supported.}

\item{split_by}{The column(s) to split data by and plot separately.
When \code{combine = TRUE}, the combined plot's data (\code{p$data})
contains the rows of every split, tagged with the \code{split_by}
column. For ggraph-based plots (e.g. \code{\link{Network}},
\code{\link{ClustreePlot}}), the node layout of every split is
exposed with the split column, and the original link rows (also
tagged with the \code{split_by} column) are available as the
\code{"edges"} attribute of the data.}

\item{split_by_sep}{The separator for multiple split_by columns. See \code{split_by}}

\item{band}{A list of arguments passed to \code{\link[qqplotr]{stat_qq_band}}
or \code{\link[qqplotr]{stat_pp_band}}, depending on \code{type}.
Set to \code{TRUE} or an empty list to use default arguments.
Set to \code{NULL} (the default) to suppress bands entirely.
To add multiple bands, provide a list of lists, each containing
arguments for one band (e.g. different \code{bandType} or
\code{distribution}). Each band can also include a custom
\code{mapping} aesthetic to control its fill colour legend entry.}

\item{line}{A list of arguments passed to \code{\link[qqplotr]{stat_qq_line}}
or \code{\link[qqplotr]{stat_pp_line}}, depending on \code{type}.
Default: \code{list()} (adds a reference line with default arguments).
Set to \code{NULL} to omit the line entirely.}

\item{point}{A list of arguments passed to \code{\link[qqplotr]{stat_qq_point}}
or \code{\link[qqplotr]{stat_pp_point}}, depending on \code{type}.
Default: \code{list()} (adds points with default arguments).
Set to \code{NULL} to omit points (not recommended).}

\item{fill_name}{A character string for the fill legend title used
when bands are present. Default: \code{"Bands"}.}

\item{band_alpha}{A numeric value in \code{[0, 1]} setting the
transparency of all bands. Individual bands can override this via
\code{alpha} inside the \code{band} argument list.
Default: \code{0.5}.}

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

\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{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{xlim}{A numeric vector of length 2 specifying the x-axis limits.
Default: \code{NULL} (use data range).}

\item{ylim}{A numeric vector of length 2 specifying the y-axis limits.
Default: \code{NULL} (use data range).}

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

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

\item{combine}{Whether to combine the plots into one when facet is FALSE. Default is TRUE.}

\item{nrow}{A numeric value specifying the number of rows in the facet.}

\item{ncol}{A numeric value specifying the number of columns in the facet.}

\item{byrow}{A logical value indicating whether to fill the plots by row.}

\item{seed}{The random seed to use. Default is 8525.}

\item{axes}{A string specifying how axes should be treated. Passed to \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}}.
Only relevant when \code{split_by} is used and \code{combine} is TRUE.
Options are:
\itemize{
\item 'keep' will retain all axes in individual plots.
\item 'collect' will remove duplicated axes when placed in the same run of rows or columns of the layout.
\item 'collect_x' and 'collect_y' will remove duplicated x-axes in the columns or duplicated y-axes in the rows respectively.
}}

\item{axis_titles}{A string specifying how axis titltes should be treated. Passed to \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}}.
Only relevant when \code{split_by} is used and \code{combine} is TRUE.
Options are:
\itemize{
\item 'keep' will retain all axis titles in individual plots.
\item 'collect' will remove duplicated titles in one direction and merge titles in the opposite direction.
\item 'collect_x' and 'collect_y' control this for x-axis titles and y-axis titles respectively.
}}

\item{guides}{A string specifying how guides should be treated in the layout. Passed to \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}}.
Only relevant when \code{split_by} is used and \code{combine} is TRUE.
Options are:
\itemize{
\item 'collect' will collect guides below to the given nesting level, removing duplicates.
\item 'keep' will stop collection at this level and let guides be placed alongside their plot.
\item 'auto' will allow guides to be collected if a upper level tries, but place them alongside the plot if not.
}}

\item{design}{Specification of the location of areas in the layout, passed to \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}}.
Only relevant when \code{split_by} is used and \code{combine} is TRUE. When specified, \code{nrow}, \code{ncol}, and \code{byrow} are ignored.
See \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}} for more details.}

\item{...}{Additional arguments.}
}
\value{
A \code{ggplot} object (single plot), a \code{patchwork} object
(combined split plots), or a named list of \code{ggplot} objects
(when \code{combine = FALSE}), each with \code{height} and
\code{width} attributes in inches.
}
\description{
Produces a quantile-quantile (QQ) plot or probability-probability (PP)
plot to compare the empirical distribution of a numeric variable against
a theoretical distribution (default: standard normal). The function
delegates to the \pkg{qqplotr} package for the underlying statistics
and rendering.

Key features:
\itemize{
\item \strong{QQ and PP modes} -- switch between quantile-quantile
and probability-probability displays via \code{type}.
\item \strong{Confidence bands} -- overlay one or more confidence bands
(pointwise, KS, Tukey simultaneous, or bootstrap) with custom fill
colours and alpha.
\item \strong{Reference line} -- a diagonal reference line (QQ) or
diagonal probability line (PP) for comparison.
\item \strong{Distribution fitting} -- compare against any
distribution supported by \pkg{qqplotr} (normal, exponential, uniform,
etc.) by passing \code{distribution} and \code{dparams} inside the
\code{band}, \code{line}, and \code{point} lists.
\item \strong{Detrending} -- enable \code{detrend = TRUE} inside the
argument lists to remove the reference line and visualise only
deviations (flat PP plot centred at zero).
\item \strong{Splitting} -- use \code{split_by} to produce separate
QQ/PP plots for different groups, combined into a single layout.
}
}
\section{split_by Workflow}{

When \code{split_by} is provided:
\enumerate{
\item \strong{Common arg validation} --
\code{\link{validate_common_args}()} checks the \code{seed} and
\code{facet_by} constraints.
\item \strong{Theme processing} -- \code{\link{process_theme}()}
resolves the \code{theme} string or function.
\item \strong{split_by column resolution} --
\code{\link{check_columns}()} validates the \code{split_by} column(s)
with \code{force_factor = TRUE}. Multiple columns are concatenated
with \code{split_by_sep}.
\item \strong{Data splitting} -- the data frame is split by
\code{split_by} levels (droplevels applied, level order preserved).
If \code{split_by} is \code{NULL}, the data is wrapped in a
single-element list with name \code{"..."}.
\item \strong{Per-split parameter resolution} --
\code{\link{check_palette}()}, \code{\link{check_palcolor}()}, and
\code{\link{check_legend}()} resolve per-split \code{palette},
\code{palcolor}, \code{legend.position}, and
\code{legend.direction}.
\item \strong{Dispatch per split} -- \code{\link{QQPlotAtomic}()} is
called for each split level. If \code{title} is a function, it
receives the split level name and generates a dynamic title;
otherwise the level name is used as the default title.
\item \strong{Combination} -- results are combined via
\code{\link{combine_plots}()} (when \code{combine = TRUE}) or
returned as a named list (when \code{combine = FALSE}).
}
}

\examples{
set.seed(8525)
data <- data.frame(norm = rnorm(100))

# Basic QQ plot with default confidence band
QQPlot(data, val = "norm", band = TRUE)

# Multiple confidence bands with custom fill labels
QQPlot(data, val = "norm", band = list(
    list(bandType = "ks", mapping = ggplot2::aes(fill = "KS"), alpha = 0.3),
    list(bandType = "ts", mapping = ggplot2::aes(fill = "TS")),
    list(bandType = "pointwise", mapping = ggplot2::aes(fill = "Normal")),
    list(bandType = "boot", mapping = ggplot2::aes(fill = "Bootstrap"))
), band_alpha = 0.6)

# Compare against exponential distribution
data(airquality, package = "datasets")
di <- "exp"
dp <- list(rate = 2)
QQPlot(airquality, val = "Ozone",
    band = list(distribution = di, dparams = dp),
    line = list(distribution = di, dparams = dp),
    point = list(distribution = di, dparams = dp)
)

# Detrended QQ plot: deviations from the reference line
de <- TRUE
QQPlot(airquality, val = "Ozone",
    band = list(distribution = di, dparams = dp, detrend = de),
    line = list(distribution = di, dparams = dp, detrend = de),
    point = list(distribution = di, dparams = dp, detrend = de)
)

# PP plot (probability-probability)
QQPlot(data, val = "norm", type = "pp", band = TRUE)

# PP plot with shifted/scaled normal distribution
dp <- list(mean = 2, sd = 2)
QQPlot(data, val = "norm", type = "pp",
    band = list(dparams = dp),
    point = list(dparams = dp))

# PP plot with custom intercept/slope line
QQPlot(data, val = "norm", type = "pp", band = TRUE,
    line = list(ab = c(.2, .5)))

# Detrended PP plot with axis limits
di <- "exp"
dp <- list(rate = .022)
de <- TRUE
QQPlot(airquality, val = "Ozone", type = "pp",
    band = list(distribution = di, detrend = de, dparams = dp),
    line = list(detrend = de),
    point = list(distribution = di, detrend = de, dparams = dp),
    ylim = c(-.5, .5)
)
}
