% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/roccurve.R
\name{ROCCurveAtomic}
\alias{ROCCurveAtomic}
\title{Atomic ROC curve (internal)}
\usage{
ROCCurveAtomic(
  data,
  truth_by,
  score_by,
  pos_label = NULL,
  group_by = NULL,
  group_by_sep = "_",
  group_name = NULL,
  x_axis_reverse = FALSE,
  percent = FALSE,
  ci = NULL,
  n_cuts = 0,
  cutoffs_at = NULL,
  cutoffs_labels = NULL,
  cutoffs_accuracy = 0.01,
  cutoffs_pt_size = 5,
  cutoffs_pt_shape = 4,
  cutoffs_pt_stroke = 1,
  cutoffs_labal_fg = "black",
  cutoffs_label_size = 4,
  cutoffs_label_bg = "white",
  cutoffs_label_bg_r = 0.1,
  show_auc = c("auto", "none", "legend", "plot"),
  auc_accuracy = 0.01,
  auc_size = 4,
  increasing = TRUE,
  theme = "theme_this",
  theme_args = list(),
  palette = "Spectral",
  palcolor = NULL,
  palreverse = FALSE,
  alpha = 1,
  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,
  xlab = ifelse(x_axis_reverse, "Specificity", "1 - Specificity"),
  ylab = "Sensitivity",
  ...
)
}
\arguments{
\item{data}{A data frame with the truth and score columns. See
\url{https://CRAN.R-project.org/package=plotROC}
for the expected format.}

\item{truth_by}{A character string naming the column that contains the true
class labels (binary outcome, 0/1 or TRUE/FALSE).}

\item{score_by}{A character vector of column names containing the predicted
scores (classifier output values). When multiple columns are provided, each
column becomes a separate ROC curve grouped by a \code{.group} identifier.
When multiple columns are used, \code{group_by} must be NULL.}

\item{pos_label}{A character string specifying the positive class label in
\code{truth_by}. When NULL (default), the labels are handled by the
\code{plotROC} package: if \code{truth_by} is a factor, the last level is
used; otherwise it is coerced to a factor with a warning.}

\item{group_by}{A character vector of column names to group the ROC curve by.
Each unique combination of group values renders a separate ROC curve.
When \code{score_by} contains multiple columns, \code{group_by} must be
NULL because the score columns themselves define the groups. Multiple
\code{group_by} columns are concatenated with \code{group_by_sep}.}

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

\item{group_name}{A character string to use as the legend title for the
ROC curve groups. When NULL (default), the \code{group_by} column name
is used.}

\item{x_axis_reverse}{A logical value. If TRUE, the x-axis is reversed (from
1 to 0), displaying specificity instead of 1 - specificity. The x-axis
label automatically changes to \code{"Specificity"}. Default: FALSE.}

\item{percent}{A logical value. If TRUE, the x and y axes are displayed as
percentages (0 to 100). Default: FALSE.}

\item{ci}{A list of arguments passed to \code{plotROC::geom_rocci()} to add
confidence intervals to the ROC curve. When NULL (default), no confidence
intervals are drawn. Example: \code{ci = list(sig.level = 0.05)}.}

\item{n_cuts}{An integer specifying the number of evenly-spaced quantile-based
cutoff points to annotate on the ROC curve. Quantiles are computed from the
\code{score_by} distribution. Default: \code{0} (no quantile cutoffs).
Ignored when \code{cutoffs_at} is non-NULL.}

\item{cutoffs_at}{A vector of user-supplied cutoff values to annotate as
points on the ROC curve. When non-NULL, overrides \code{n_cuts}. Accepts
raw numeric score thresholds and/or named method strings from the
\code{\link[OptimalCutpoints]{optimal.cutpoints}} package for automatic
optimal cutoff identification. Both \code{cutoffs_at} and
\code{cutoffs.labels} are passed to \code{plotROC::geom_roc()}.
Supported method values are:
\itemize{
\item \code{"CB"} (cost-benefit method);
\item \code{"MCT"} (minimises Misclassification Cost Term);
\item \code{"MinValueSp"} (a minimum value set for Specificity);
\item \code{"MinValueSe"} (a minimum value set for Sensitivity);
\item \code{"ValueSe"} (a value set for Sensitivity);
\item \code{"MinValueSpSe"} (a minimum value set for Specificity and Sensitivity);
\item \code{"MaxSp"} (maximises Specificity);
\item \code{"MaxSe"} (maximises Sensitivity);
\item \code{"MaxSpSe"} (maximises Sensitivity and Specificity simultaneously);
\item \code{"MaxProdSpSe"} (maximises the product of Sensitivity and Specificity);
\item \code{"ROC01"} (minimises distance between ROC plot and point (0,1));
\item \code{"SpEqualSe"} (Sensitivity = Specificity);
\item \code{"Youden"} (Youden Index);
\item \code{"MaxEfficiency"} (maximises Efficiency/Accuracy);
\item \code{"Minimax"} (minimises the most frequent error);
\item \code{"MaxDOR"} (maximises Diagnostic Odds Ratio);
\item \code{"MaxKappa"} (maximises Kappa Index);
\item \code{"MinValueNPV"} (a minimum value set for Negative Predictive Value);
\item \code{"MinValuePPV"} (a minimum value set for Positive Predictive Value);
\item \code{"ValueNPV"} (a value set for Negative Predictive Value);
\item \code{"ValuePPV"} (a value set for Positive Predictive Value);
\item \code{"MinValueNPVPPV"} (a minimum value set for Predictive Values);
\item \code{"PROC01"} (minimises distance between PROC plot and point (0,1));
\item \code{"NPVEqualPPV"} (Negative Predictive Value = Positive Predictive Value);
\item \code{"MaxNPVPPV"} (maximises Positive and Negative Predictive Values simultaneously);
\item \code{"MaxSumNPVPPV"} (maximises the sum of the Predictive Values);
\item \code{"MaxProdNPVPPV"} (maximises the product of Predictive Values);
\item \code{"ValueDLR.Negative"} (a value set for Negative Diagnostic Likelihood Ratio);
\item \code{"ValueDLR.Positive"} (a value set for Positive Diagnostic Likelihood Ratio);
\item \code{"MinPvalue"} (minimises p-value of the Chi-squared test);
\item \code{"ObservedPrev"} (closest value to observed prevalence);
\item \code{"MeanPrev"} (closest value to the mean of the test values);
\item \code{"PrevalenceMatching"} (predicted prevalence equals observed prevalence).
}}

\item{cutoffs_labels}{A character vector of user-supplied labels for the
cutoff points. Must be the same length as \code{cutoffs_at}. When NULL,
labels are generated automatically (score value or method name).}

\item{cutoffs_accuracy}{A numeric value controlling the rounding precision of
automatically generated cutoff labels. Default: \code{0.01}.}

\item{cutoffs_pt_size}{A numeric value specifying the size of the cutoff
point markers. Default: \code{5}.}

\item{cutoffs_pt_shape}{A numeric value specifying the shape of the cutoff
point markers. Default: \code{4} (cross).}

\item{cutoffs_pt_stroke}{A numeric value specifying the stroke width of the
cutoff point markers. Default: \code{1}.}

\item{cutoffs_labal_fg}{A character string specifying the text colour of
the cutoff labels. Default: \code{"black"}.}

\item{cutoffs_label_size}{A numeric value specifying the font size of the
cutoff labels. Default: \code{4}.}

\item{cutoffs_label_bg}{A character string specifying the background colour
of the cutoff labels. Default: \code{"white"}.}

\item{cutoffs_label_bg_r}{A numeric value specifying the background radius
of the cutoff labels (passed to \code{ggrepel::geom_text_repel()}).
Default: \code{0.1}.}

\item{show_auc}{A character string specifying the display mode for AUC values:
\itemize{
\item \code{"auto"} (default): Automatically determine the position. When
there is a single group or \code{facet_by} is provided, AUC is placed on
the plot; otherwise AUC is placed in the legend.
\item \code{"none"}: Do not display AUC values.
\item \code{"legend"}: Display AUC values in the legend labels.
\item \code{"plot"}: Display AUC values as text on the plot.
}}

\item{auc_accuracy}{A numeric value controlling the rounding precision of
AUC values in labels. Default: \code{0.01}.}

\item{auc_size}{A numeric value specifying the font size of AUC labels when
displayed on the plot. Default: \code{4}.}

\item{increasing}{A logical value. If TRUE (default), higher scores indicate
the positive class; if FALSE, lower scores indicate the positive class.
Controls the direction of comparison in the ROC analysis.}

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

\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{xlab}{A character string specifying the x-axis label.}

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

\item{...}{Additional arguments.}
}
\value{
A \code{ggplot} object with \code{height} and \code{width}
attributes (in inches) attached, plus \code{attr(p, "auc")} and
\code{attr(p, "cutoffs")} data frames.
}
\description{
Core implementation for drawing a single Receiver Operating Characteristic
(ROC) curve. This is the internal workhorse behind the exported
\code{\link{ROCCurve}} function. It takes a \strong{single} data frame (no
\code{split_by} support) and returns a \code{ggplot} object.
}
\details{
The function produces an ROC curve using \code{plotROC::geom_roc()}, with
the following capabilities:
\itemize{
\item \strong{Multiple classifiers} — \code{score_by} accepts multiple
column names, automatically pivoting them into a grouped format so
several prediction scores can be compared on a single plot.
\item \strong{AUC calculation} — area under the curve is computed via
\code{plotROC::calc_auc()} and displayed either on the plot or in the
legend, controlled by \code{show_auc}.
\item \strong{Cutoff annotation} — user-specified cutoffs (numeric score
thresholds or named optimal-cutoff methods from the
\code{OptimalCutpoints} package) are rendered as markers with labels,
using \code{ggrepel::geom_text_repel()} for label placement.
\item \strong{Confidence intervals} — optional ROC confidence bands via
\code{plotROC::geom_rocci()}.
\item \strong{Axis flexibility} — supports reversed x-axis (displaying
specificity) and percent-scaled axes.
}
}
\section{Architecture}{

\enumerate{
\item \strong{show_auc resolution} — \code{match.arg()} resolves
\code{show_auc} to one of \code{"auto"}, \code{"none"}, \code{"legend"},
or \code{"plot"}.
\item \strong{Column validation} — \code{\link{check_columns}()} validates
\code{truth_by} (single column), \code{score_by} (multiple allowed), and
\code{group_by} (factor, multi-column concatenated). An error is raised if
\code{group_by} is provided alongside multiple \code{score_by} columns.
\item \strong{Positive label encoding} — Converts \code{truth_by} to binary
numeric (0/1) with three paths:
\itemize{
\item \code{pos_label} provided: re-factor with \code{pos_label} as the
last level, then convert.
\item \code{truth_by} is a factor: warn that the last level is treated
as positive, then convert.
\item Non-numeric, non-factor: coerce to factor, warn, then convert.
}
\item \strong{Multi-score_by expansion} — When \code{score_by} contains
multiple columns, \code{tidyr::pivot_longer()} reshapes into a single
\code{.score} column with a \code{.group} identifier, which becomes the
\code{group_by} variable.
\item \strong{ggplot dispatch} — Selects \code{gglogger::ggplot} or
\code{ggplot2::ggplot} based on
\code{getOption("plotthis.gglogger.enabled")}.
\item \strong{Dummy group insertion} — When \code{group_by = NULL}, creates
a synthetic \code{..group} column (constant \code{""}) so the curve still
renders. The legend is suppressed (\code{"none"}).
\item \strong{Auto AUC placement} — When \code{show_auc = "auto"},
single-group or faceted plots place AUC on the plot; multi-group plots
place it in the legend.
\item \strong{Base ROC geometry} — \code{plotROC::geom_roc()} with
\code{aes(d = truth, m = score, color = group)}, controlling direction
via the \code{increasing} parameter.
\item \strong{AUC calculation} — Temporarily facets via
\code{\link{facet_plot}()}, then computes per-group (and per-facet) AUC
values via \code{plotROC::calc_auc()}.
\item \strong{Cutoff computation} — \code{get_cutoffs_data()} combines
\code{group_by} and \code{facet_by} columns into a \code{.cat} identifier
and computes per-category cutoff data, supporting both numeric thresholds
and named \code{OptimalCutpoints} methods.
\item \strong{Cutoff rendering} — When cutoff data is non-NULL, splits the
\code{.cat} column back into group/facet columns and adds
\code{geom_point()} (markers) and \code{geom_text_repel()} (labels) with
configurable size, shape, stroke, colour, and background styling.
\item \strong{Confidence intervals} — When \code{ci} is non-NULL,
\code{plotROC::geom_rocci()} is added with the provided arguments.
\item \strong{AUC display} — Three modes:
\itemize{
\item \code{"plot"}: \code{geom_text()} places AUC labels at a corner
position determined by \code{increasing} and \code{x_axis_reverse}.
\item \code{"legend"}: AUC values are appended to the
\code{scale_color_manual()} labels (with per-facet prefixes when
faceting is active).
\item \code{"none"}: group level names are used as-is.
}
\item \strong{Diagonal reference} — \code{geom_abline()} draws the
no-discrimination line (y = x, or y = -x when x-axis is reversed) as a
dashed grey line.
\item \strong{Color scale} — \code{scale_color_manual()} assigns
palette-derived colours via \code{\link{palette_this}()}, with
AUC-augmented labels when \code{show_auc = "legend"}.
\item \strong{Axis formatting} — Percent labels on y-axis (and x-axis)
when \code{percent = TRUE}. X-axis reversed (1 to 0) when
\code{x_axis_reverse = TRUE}, changing the axis label to
\code{"Specificity"}.
\item \strong{Labels and theme} — \code{labs()} sets title, subtitle, x,
and y labels. The theme, aspect ratio, legend position/direction, and
dashed grid lines are applied.
\item \strong{Dimension calculation} — \code{\link{calculate_plot_dimensions}()}
computes \code{height} and \code{width} attributes from
\code{base_height = 4.5}, aspect ratio, and legend metrics.
\item \strong{Attribute storage} — \code{auc} and \code{cutoffs} data
frames are stored as \code{attr(p, "auc")} and
\code{attr(p, "cutoffs")} for retrieval by the exported wrapper.
\item \strong{Faceting} — \code{\link{facet_plot}()} wraps the plot with
\code{facet_wrap} / \code{facet_grid} if \code{facet_by} is provided.
}
}

\keyword{internal}
