Creates a standard Pharmacokinetic (PK) concentration-time profile plot. This function wraps ggplot2 calls to consistently format PK profiles, handling log transformations, various summary statistics, and variability measures.

gg_pkc_lineplot(
  data,
  time_var,
  analyte_var,
  group,
  stat = c("mean", "median"),
  variability = c("sd", "se", "ci", "iqr", "none"),
  conf_level = 0.95,
  log_y = TRUE,
  lloq = NA_real_,
  x_breaks = NULL,
  errorbar_width = 0.45,
  dodge_width = 0.2
)

Arguments

data

(data.frame)
The dataset containing PK data.

time_var

(tidy-select)
The time variable (x-axis).

analyte_var

(tidy-select)
The concentration/analyte variable (y-axis).

group

(tidy-select)
The grouping/treatment variable.

stat

(string)
Primary summary statistic: "mean" or "median". Default is "mean".

variability

(string)
Variability measure: "sd", "se", "ci", "iqr", or "none". Default is "sd".

conf_level

(numeric)
Confidence level for error bars when variability = "ci" (default: 0.95).

log_y

(logical)
Whether to apply log10 scale to the y-axis. Default is TRUE.

lloq

(numeric or NULL)
Lower Limit of Quantification. Default is NA_real_.

x_breaks

(numeric, function, or NULL)
Breaks for a numeric x-axis, passed to ggplot2::scale_x_continuous(). The default NULL puts a break on every observed timepoint, which is readable over a short window but not over a long one. Supply a vector (e.g. seq(0, 600, by = 100)) or a break function (e.g. scales::breaks_pretty()) for wider time ranges. Ignored when time_var is a factor.

errorbar_width

(numeric)
Width of the horizontal caps on the variability error bars. Default is 0.45. Set to 0 for no caps. Ignored when variability = "none".

dodge_width

(numeric)
Horizontal separation between groups at each timepoint. Default is 0.2. Increase if error bar caps are wider than the dodge width and overlap.

Value

A ggplot object of class crane_gg_pkc with an "ARD" attribute: a data.frame of summary statistics (N, the primary stat, and variability-specific bound names: mean_plus_sd/mean_minus_sd for "sd", mean_plus_se/mean_minus_se for "se", upper_ci/lower_ci for "ci", q75/q25 for "iqr", no bounds for "none") grouped by time_var and group. Retrieve with attr(p, "ARD").

See also

annotate_pkc_df() for related functionalities.

Examples

# 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 table 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")

# Linear Scale Example (Baseline 0 is included)
gg_pkc_lineplot(
  data = df_pk,
  time_var = Time_Nominal,
  analyte_var = conc,
  group = Dose_Group,
  stat = "mean",
  variability = "sd",
  log_y = FALSE
)


# Log Scale Example (Filter out 0s first to avoid log(0) warnings)
df_pk |>
  dplyr::filter(conc > 0) |>
  gg_pkc_lineplot(
    time_var = Time_Nominal,
    analyte_var = conc,
    group = Dose_Group,
    stat = "mean",
    variability = "se",
    log_y = TRUE,
    lloq = 2.0
  )


# Title, subtitle, axes labels and legend position customization
gg_pkc_lineplot(
  data = df_pk,
  time_var = Time_Nominal,
  analyte_var = conc,
  group = Dose_Group,
  stat = "mean",
  variability = "sd",
  log_y = FALSE
) +
  ggplot2::labs(
    x = "Nominal time (hr)",
    y = "Concentration (ng/mL)",
    title = "Title",
    subtitle = "Subtitle"
  ) +
  ggplot2::theme(
    legend.position = "top"
  )