Creates a standard Pharmacokinetic (PK) concentration box plot, summarizing
the distribution of concentrations at each timepoint and treatment group.
This function wraps ggplot2 calls to consistently format PK box plots,
handling log transformations, whisker definitions, and outlier detection.
gg_pkc_boxplot(
data,
time_var,
analyte_var,
group,
whisker = c("percentile", "tukey", "minmax"),
quantile_type = 1,
log_y = TRUE,
show_mean = TRUE,
outlier_size = 2,
id_var = NULL
)(data.frame)
The dataset containing PK data.
(tidy-select)
The time variable (x-axis).
(tidy-select)
The concentration/analyte variable (y-axis).
(tidy-select)
The grouping/treatment variable.
(string)
Method used to compute the box whiskers: "percentile" (5% and 95%
percentiles), "tukey" (1.5 * IQR beyond the hinges, the conventional
boxplot definition), or "minmax" (full data range, so no points are
flagged as outliers). Default is "percentile".
(integer)
Quantile algorithm type passed to stats::quantile(). Default is 1.
(logical)
Whether to apply log10 scale to the y-axis. Default is TRUE.
(logical)
Whether to overlay a mean marker on each box. Default is TRUE.
(numeric)
Size of the outlier point markers. Default is 2.
(tidy-select)
Optional subject/row identifier variable. When supplied, each outlier row
in the "ARD" attribute uses the subject ID as stat_label (matching
the template convention). Default is NULL (uses "outlier").
A ggplot object of class crane_gg_pkc_box with an "ARD"
attribute: a data.frame of summary statistics (n, mean, sd, and
whisker-specific stat names: p05/p25/p50/p75/p95 for
"percentile", min/p25/p50/p75/max for "minmax",
p25/p50/p75/iqr/lower_whisker/upper_whisker for "tukey")
plus one row per outlier point, grouped by time_var and group.
Retrieve with attr(p, "ARD").
gg_pkc_lineplot() for related functionalities.
# Prepare PK Data using the built-in Theoph dataset
df_pk <- Theoph
df_pk$Time_Nominal <- round(df_pk$Time)
# Filter to specific timepoints to keep the plot clean
df_pk <- df_pk[df_pk$Time_Nominal %in% c(0, 2, 4, 8, 24), ]
# Create a mock treatment group based on Dose
df_pk$Dose_Group <- ifelse(df_pk$Dose > 4.5, "High Dose", "Low Dose")
# Default Plot: percentile whiskers on a log10 y-axis
gg_pkc_boxplot(
data = df_pk,
time_var = Time_Nominal,
analyte_var = conc,
group = Dose_Group
)
#> Warning: Removed 9 rows with non-positive `conc` value for log scale.
# Tukey whiskers on a linear y-axis, without the mean marker
gg_pkc_boxplot(
data = df_pk,
time_var = Time_Nominal,
analyte_var = conc,
group = Dose_Group,
whisker = "tukey",
log_y = FALSE,
show_mean = FALSE
)