---
title: PKPG06
subtitle: Boxplot of Metabolite to Parent Ratios by Treatment
---
------------------------------------------------------------------------
{{< include ../../_utils/envir_hook.qmd >}}
```{r setup, echo = FALSE, warning = FALSE, message = FALSE}
library(dplyr)
library(ggplot2)
library(tidyr)
library(tern)
library(nestcolor)
adpp <- random.cdisc.data::cadpp
# Filter NAs
adpp <- adpp %>%
filter(PPSPEC != "NA" & PARAM != "NA" & PPCAT != "NA") %>%
filter(PARAMCD == "CMAX" | PARAMCD == "AUCIFO", AVISITN == 1)
# filter data by PPCAT and calculate ratio
anl_x <- adpp %>%
filter(PPCAT %in% c("Metabolite Drug X", "Plasma Drug X")) %>%
pivot_wider(
id_cols = c(USUBJID, ACTARM, PARAMCD, AVISIT),
names_from = PPCAT,
values_from = AVAL
) %>%
dplyr::mutate(ratio = `Metabolite Drug X` / `Plasma Drug X`) %>%
filter(!is.na(ratio), ratio != Inf)
anl_y <- adpp %>%
filter(PPCAT %in% c("Metabolite Drug Y", "Plasma Drug Y")) %>%
pivot_wider(
id_cols = c(USUBJID, ACTARM, PARAMCD, AVISIT),
names_from = PPCAT,
values_from = AVAL
) %>%
dplyr::mutate(ratio = `Metabolite Drug Y` / `Plasma Drug Y`) %>%
filter(!is.na(ratio), ratio != Inf)
# functions to calculate custom quantiles and outliers
quantiles <- function(x) {
quant <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
names(quant) <- c("ymin", "lower", "middle", "upper", "ymax")
quant
}
outliers <- function(x) {
return(x < quantile(x, 0.05) | x > quantile(x, 0.95))
}
```
```{r include = FALSE}
webr_code_labels <- c("setup")
```
{{< include ../../_utils/webr_no_include.qmd >}}
## Output
::::::: panel-tabset
## Plot with Whiskers at ±1.5 <br/> Times Inter-Quartile Range
#### Drug X Boxplot
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r plot1, test = list(plot_v1 = "plot")}
plot <- ggplot(anl_x, aes(x = PARAMCD, y = ratio, fill = ACTARM)) +
geom_boxplot(outlier.size = 2) +
stat_boxplot(geom = "errorbar") +
stat_summary(
geom = "point",
fun = "mean",
col = "black",
size = 5,
shape = 8,
position = position_dodge(0.75)
) +
geom_text(
data = . %>% dplyr::group_by(PARAMCD, ACTARM) %>% dplyr::filter(ratio %in% boxplot.stats(ratio)$out),
aes(x = PARAMCD, y = ratio, label = USUBJID, col = ACTARM),
size = 3,
hjust = -0.2,
position = position_dodge(0.75),
show.legend = FALSE
) +
labs(
title = "Boxplot of Metabolite to Parent Ratios by Treatment",
subtitle = paste0(
"Analyte: Plasma Drug X, Metabolite Drug X ",
"\nPK Parameter: ",
as.character(paste(unique(anl_x$PARAMCD), collapse = ", ")),
"\nVisit: ",
as.character((unique(anl_x$AVISIT)))
),
caption = "Program: \nOutput:",
x = "Parameter",
y = "Metabolite to Parent Ratio"
) +
theme(plot.caption = element_text(hjust = 0)) +
theme_nest()
# PKPG06
plot
```
```{r include = FALSE}
webr_code_labels <- c("plot1")
```
{{< include ../../_utils/webr.qmd >}}
:::
#### Drug Y Boxplot
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r plot2, test = list(plot_v2 = "plot")}
plot <- ggplot(anl_y, aes(x = PARAMCD, y = ratio, fill = ACTARM)) +
geom_boxplot(outlier.size = 2) +
stat_boxplot(geom = "errorbar") +
stat_summary(
geom = "point",
fun = "mean",
col = "black",
size = 5,
shape = 8,
position = position_dodge(0.75)
) +
geom_text(
data = . %>% dplyr::group_by(PARAMCD, ACTARM) %>% dplyr::filter(ratio %in% boxplot.stats(ratio)$out),
aes(x = PARAMCD, y = ratio, label = USUBJID, color = ACTARM),
size = 3,
hjust = -0.2,
position = position_dodge(0.75),
show.legend = FALSE
) +
labs(
title = "Boxplot of Metabolite to Parent Ratios by Treatment",
subtitle = paste0(
"Analyte: Plasma Drug Y, Metabolite Drug Y ",
"\nPK Parameter: ",
as.character(paste(unique(anl_y$PARAMCD), collapse = ", ")),
"\nVisit: ",
as.character((unique(anl_y$AVISIT)))
),
caption = "Program: \nOutput:",
x = "Parameter",
y = "Metabolite to Parent Ratio"
) +
theme(plot.caption = element_text(hjust = 0)) +
theme_nest()
# result
plot
```
```{r include = FALSE}
webr_code_labels <- c("plot2")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Plot with Whiskers at <br/> Minimum and Maximum Values
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r plot3, test = list(plot_v3 = "plot")}
# whiskers are formed with the minimum and maximum values
plot <- ggplot(anl_x, aes(x = PARAMCD, y = ratio, fill = ACTARM)) +
geom_boxplot(outlier.size = 0) +
stat_boxplot(geom = "errorbar", coef = NULL) +
stat_summary(geom = "point", fun = "mean", col = "black", size = 5, shape = 8, position = position_dodge(0.75)) +
labs(
title = "Boxplot of Metabolite to Parent Ratios by Treatment",
subtitle = paste0(
"Analyte: Plasma Drug X, Metabolite Drug X ",
"\nPK Parameter: ",
as.character(paste(unique(anl_x$PARAMCD), collapse = ", ")),
"\nVisit: ",
as.character((unique(anl_x$AVISIT)))
),
caption = "Program:\nOutput:",
x = "Parameter",
y = "Metabolite to Parent Ratio"
) +
theme(plot.caption = element_text(hjust = 0)) +
theme_nest()
# result
plot
```
```{r include = FALSE}
webr_code_labels <- c("plot3")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Plot with Whiskers at <br/> 5th and 95th Percentiles
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r plot4, test = list(plot_v4 = "plot")}
anl_x_without_outliers <- anl_x %>%
dplyr::group_by(PARAMCD, ACTARM) %>%
dplyr::mutate(outlier = ifelse(outliers(ratio), ratio, as.numeric(NA)))
plot <- ggplot(anl_x, aes(PARAMCD, ratio, fill = ACTARM, label = USUBJID)) +
stat_summary(
fun.data = quantiles, geom = "boxplot",
position = position_dodge(1)
) +
stat_summary(
geom = "point",
fun = "mean",
col = "black",
size = 5,
shape = 8,
position = position_dodge(1)
) +
stat_summary(
fun.data = quantiles, geom = "errorbar",
position = position_dodge(1)
) +
geom_point(
data = anl_x_without_outliers,
aes(x = PARAMCD, y = outlier),
na.rm = TRUE,
size = 2,
position = position_dodge(1),
show.legend = FALSE
) +
geom_text(
data = anl_x_without_outliers,
aes(x = PARAMCD, y = outlier, label = USUBJID, color = ACTARM),
na.rm = TRUE,
size = 3,
hjust = -0.2,
vjust = 1,
position = position_dodge(1),
show.legend = FALSE
) +
labs(
title = "Boxplot of Metabolite to Parent Ratios by Treatment",
subtitle = paste0(
"Analyte: Plasma Drug X, Metabolite Drug X ",
"\nPK Parameter: ",
as.character(paste(unique(anl_x$PARAMCD), collapse = ", ")),
"\nVisit: ",
as.character((unique(anl_x$AVISIT)))
),
caption = "Program: \nOutput:",
x = "Parameter",
y = "Metabolite to Parent Ratio"
) +
theme(plot.caption = element_text(hjust = 0)) +
theme_nest()
# result
plot
```
```{r include = FALSE}
webr_code_labels <- c("plot4")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Data Setup
```{r setup}
#| code-fold: show
```
:::::::
{{< include ../../_utils/save_results.qmd >}}
{{< include ../../repro.qmd >}}