1 |
#' Helper for identifying any `LLOQ` and `ULOQ` values in `LBSTRESC`. Outcome drives |
|
2 |
#' horizontal line functionality display and legend labeling along with display |
|
3 |
#' of values in footnote. |
|
4 |
#' |
|
5 |
#' @details Biomarker Sciences would like to have `LLOQ` and `ULOQ` values available for |
|
6 |
#' reference in the visualizations. This also aids in setting the data constraint |
|
7 |
#' ranges when `goshawk` functions are run from `teal.goshawk` `UI`. |
|
8 |
#' |
|
9 |
#' @param loqs_data (`data frame`)\cr `loqs_data` data set containing assay data with potential `LOQ` values |
|
10 |
#' |
|
11 |
#' @import dplyr |
|
12 |
#' @keywords internal |
|
13 |
#' |
|
14 |
#' @examples |
|
15 |
#' goshawk:::h_identify_loq_values(loqs_data = goshawk::rADLB, flag_var = "LOQFL") |
|
16 |
h_identify_loq_values <- function(loqs_data, flag_var) { |
|
17 | ! |
ifelse( |
18 | ! |
!grep("PARAM", names(loqs_data)), |
19 | ! |
stop("Assay dataset must include variable PARAM to use the caption_loqs_label function."), |
20 | ! |
1 |
21 |
) |
|
22 | ! |
ifelse( |
23 | ! |
!grep("LBSTRESC", names(loqs_data)), |
24 | ! |
stop("Assay dataset must include variable LBSTRESC to use the caption_loqs_label function."), |
25 | ! |
1 |
26 |
) |
|
27 | ||
28 |
# filter for records only relevant to loq. |
|
29 |
# get LLOQ value |
|
30 | ! |
lloq <- loqs_data %>% |
31 | ! |
filter(!!sym(flag_var) == "Y") %>% |
32 | ! |
select("PARAM", "LBSTRESC") %>% |
33 | ! |
filter(grepl("<", .data$LBSTRESC, fixed = FALSE)) %>% |
34 | ! |
mutate(LLOQC = .data$LBSTRESC, LLOQN = as.numeric(gsub("[^0-9.-]", "", .data$LBSTRESC))) %>% |
35 | ! |
group_by(.data$PARAM) %>% |
36 | ! |
slice(1) %>% |
37 | ! |
ungroup() %>% |
38 | ! |
select(-"LBSTRESC") |
39 | ||
40 | ||
41 |
# get ULOQ value |
|
42 | ! |
uloq <- loqs_data %>% |
43 | ! |
filter(!!sym(flag_var) == "Y") %>% |
44 | ! |
select("PARAM", "LBSTRESC") %>% |
45 | ! |
filter(grepl(">", .data$LBSTRESC, fixed = FALSE)) %>% |
46 | ! |
mutate(ULOQC = .data$LBSTRESC, ULOQN = as.numeric(gsub("[^0-9.-]", "", .data$LBSTRESC))) %>% |
47 | ! |
group_by(.data$PARAM) %>% |
48 | ! |
slice(1) %>% |
49 | ! |
ungroup() %>% |
50 | ! |
select(-"LBSTRESC") |
51 | ||
52 | ||
53 |
# return LOQ data |
|
54 | ! |
loq_values <- merge(lloq, uloq, by = "PARAM", all = TRUE) |
55 | ! |
if (nrow(loq_values) == 0) { |
56 | ! |
loq_values <- data.frame( |
57 | ! |
PARAM = names(table(droplevels(as.factor(loqs_data$PARAM)))), |
58 | ! |
LLOQC = NA, |
59 | ! |
LLOQN = NA, |
60 | ! |
ULOQC = NA, |
61 | ! |
ULOQN = NA |
62 |
) |
|
63 |
} |
|
64 | ||
65 | ! |
attr(loq_values[["PARAM"]], "label") <- "Parameter" |
66 | ! |
attr(loq_values[["LLOQC"]], "label") <- "Lower Limit of Quantitation (C)" |
67 | ! |
attr(loq_values[["LLOQN"]], "label") <- "Lower Limit of Quantitation" |
68 | ! |
attr(loq_values[["ULOQC"]], "label") <- "Upper Limit of Quantitation (C)" |
69 | ! |
attr(loq_values[["ULOQN"]], "label") <- "Upper Limit of Quantitation" |
70 | ||
71 | ! |
return(loq_values) |
72 |
} |
|
73 | ||
74 |
#' Add footnote to identify `LLOQ` and `ULOQ` values identified from data |
|
75 |
#' |
|
76 |
#' @param loqs_data (`data frame`)\cr `loqs_data` data set containing assay data with potential `LOQ` values |
|
77 |
#' |
|
78 |
#' @import dplyr |
|
79 |
#' @keywords internal |
|
80 |
#' |
|
81 |
#' @examples |
|
82 |
#' caption_label <- goshawk:::h_caption_loqs_label(loqs_data = goshawk::rADLB, flag_var = "LOQFL") |
|
83 |
h_caption_loqs_label <- function(loqs_data, flag_var) { |
|
84 | ! |
loq_values <- h_identify_loq_values(loqs_data, flag_var) |
85 | ||
86 | ! |
lloqc <- ifelse(is.na(loq_values$LLOQC), "NA", as.character(loq_values$LLOQC)) |
87 | ! |
uloqc <- ifelse(is.na(loq_values$ULOQC), "NA", as.character(loq_values$ULOQC)) |
88 | ||
89 |
# create caption |
|
90 | ! |
caption_loqs_label <- paste0( |
91 | ! |
"Limits of quantification read from study data for ", |
92 | ! |
loqs_data$PARAM, |
93 | ! |
": LLOQ is ", |
94 | ! |
lloqc, |
95 | ! |
", ULOQ is ", |
96 | ! |
uloqc |
97 |
) |
|
98 | ||
99 | ! |
return(caption_loqs_label) |
100 |
} |
|
101 | ||
102 |
#' Check that argument is a valid color |
|
103 |
#' |
|
104 |
#' Checks if the argument can be converted to valid RGB values space. See `grDevices::col2rgb`. |
|
105 |
#' |
|
106 |
#' @inheritParams checkmate::checkCharacter |
|
107 |
#' @param color (`character`)\cr |
|
108 |
#' Valid color convertible to RGB scale by [grDevices::col2rgb()] |
|
109 |
#' |
|
110 |
#' @inherit checkmate::checkCharacter return |
|
111 |
#' @keywords internal |
|
112 |
check_color <- function(color, |
|
113 |
min.len = NULL, # nolint |
|
114 |
max.len = NULL, # nolint |
|
115 |
any.missing = TRUE, # nolint |
|
116 |
all.missing = TRUE, # nolint |
|
117 |
len = NULL, |
|
118 |
null.ok = FALSE) { # nolint |
|
119 | ! |
string_check <- checkmate::check_character( |
120 | ! |
color, |
121 | ! |
min.len = min.len, max.len = max.len, any.missing = any.missing, len = len, null.ok = null.ok |
122 |
) |
|
123 | ! |
if (!isTRUE(string_check)) { |
124 | ! |
return(string_check) |
125 |
} |
|
126 | ||
127 | ! |
res <- sapply(color, function(col) { |
128 | ! |
tryCatch( |
129 | ! |
is.matrix(grDevices::col2rgb(col)), |
130 | ! |
error = function(e) FALSE |
131 |
) |
|
132 |
}) |
|
133 | ||
134 | ! |
if (any(!res)) { |
135 | ! |
"Must be a valid color, convertible to rgb by 'col2rgb'" |
136 |
} else { |
|
137 | ! |
TRUE |
138 |
} |
|
139 |
} |
|
140 | ||
141 |
#' @rdname check_color |
|
142 |
assert_color <- checkmate::makeAssertionFunction(check_color) |
1 |
#' Function to create a scatter plot. |
|
2 |
#' |
|
3 |
#' @description |
|
4 |
#' `r lifecycle::badge("deprecated")` |
|
5 |
#' |
|
6 |
#' `g_scatterplot()` is deprecated. Please use |
|
7 |
#' [g_correlationplot()] instead. Default plot displays scatter facetted by |
|
8 |
#' visit with color attributed treatment arms and symbol attributed `LOQ` values. |
|
9 |
#' |
|
10 |
#' @param label text string to used to identify plot. |
|
11 |
#' @param data `ADaM` structured analysis laboratory data frame e.g. `ADLB`. |
|
12 |
#' @param param_var name of variable containing biomarker codes e.g. `PARAMCD`. |
|
13 |
#' @param param biomarker to visualize e.g. `IGG`. |
|
14 |
#' @param xaxis_var name of variable containing biomarker results displayed on X-axis e.g. `BASE`. |
|
15 |
#' @param yaxis_var name of variable containing biomarker results displayed on Y-axis e.g. `AVAL`. |
|
16 |
#' @param trt_group name of variable representing treatment group e.g. `ARM`. |
|
17 |
#' @param visit name of variable containing nominal visits e.g. `AVISITCD`. |
|
18 |
#' @param loq_flag_var name of variable containing `LOQ` flag e.g. `LOQFL`. |
|
19 |
#' @param unit name of variable containing biomarker unit e.g. `AVALU`. |
|
20 |
#' @param xlim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the x-axis |
|
21 |
#' if the default limits are not suitable. |
|
22 |
#' @param ylim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the y-axis |
|
23 |
#' if the default limits are not suitable. |
|
24 |
#' @param color_manual vector of colors applied to treatment values. |
|
25 |
#' @param shape_manual vector of symbols applied to `LOQ` values. |
|
26 |
#' @param facet_ncol number of facets per row. |
|
27 |
#' @param facet set layout to use treatment facetting. |
|
28 |
#' @param facet_var variable to use for treatment facetting. |
|
29 |
#' @param reg_line include regression line and annotations for slope and coefficient in |
|
30 |
#' visualization. Use with facet = TRUE. |
|
31 |
#' @param hline y-axis value to position a horizontal line. |
|
32 |
#' @param vline x-axis value to position a vertical line. |
|
33 |
#' @param rotate_xlab 45 degree rotation of x-axis label values. |
|
34 |
#' @param font_size font size control for title, x-axis label, y-axis label and legend. |
|
35 |
#' @param dot_size plot dot size. |
|
36 |
#' @param reg_text_size font size control for regression line annotations. |
|
37 |
#' |
|
38 |
#' @author Nick Paszty (npaszty) paszty.nicholas@gene.com |
|
39 |
#' @author Balazs Toth (tothb2) toth.balazs@gene.com |
|
40 |
#' |
|
41 |
#' @details Regression uses `deming` model. |
|
42 |
#' |
|
43 |
#' @export |
|
44 |
#' |
|
45 |
#' @examples |
|
46 |
#' # Example using ADaM structure analysis dataset. |
|
47 |
#' |
|
48 |
#' library(stringr) |
|
49 |
#' |
|
50 |
#' # original ARM value = dose value |
|
51 |
#' arm_mapping <- list( |
|
52 |
#' "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination" |
|
53 |
#' ) |
|
54 |
#' color_manual <- c("150mg QD" = "#000000", "Placebo" = "#3498DB", "Combination" = "#E74C3C") |
|
55 |
#' # assign LOQ flag symbols: circles for "N" and triangles for "Y", squares for "NA" |
|
56 |
#' shape_manual <- c("N" = 1, "Y" = 2, "NA" = 0) |
|
57 |
#' |
|
58 |
#' ADLB <- rADLB |
|
59 |
#' var_labels <- lapply(ADLB, function(x) attributes(x)$label) |
|
60 |
#' ADLB <- ADLB %>% |
|
61 |
#' mutate(AVISITCD = case_when( |
|
62 |
#' AVISIT == "SCREENING" ~ "SCR", |
|
63 |
#' AVISIT == "BASELINE" ~ "BL", |
|
64 |
#' grepl("WEEK", AVISIT) ~ |
|
65 |
#' paste( |
|
66 |
#' "W", |
|
67 |
#' trimws( |
|
68 |
#' substr( |
|
69 |
#' AVISIT, |
|
70 |
#' start = 6, |
|
71 |
#' stop = str_locate(AVISIT, "DAY") - 1 |
|
72 |
#' ) |
|
73 |
#' ) |
|
74 |
#' ), |
|
75 |
#' TRUE ~ NA_character_ |
|
76 |
#' )) %>% |
|
77 |
#' mutate(AVISITCDN = case_when( |
|
78 |
#' AVISITCD == "SCR" ~ -2, |
|
79 |
#' AVISITCD == "BL" ~ 0, |
|
80 |
#' grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)), |
|
81 |
#' TRUE ~ NA_real_ |
|
82 |
#' )) %>% |
|
83 |
#' # use ARMCD values to order treatment in visualization legend |
|
84 |
#' mutate(TRTORD = ifelse(grepl("C", ARMCD), 1, |
|
85 |
#' ifelse(grepl("B", ARMCD), 2, |
|
86 |
#' ifelse(grepl("A", ARMCD), 3, NA) |
|
87 |
#' ) |
|
88 |
#' )) %>% |
|
89 |
#' mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>% |
|
90 |
#' mutate(ARM = factor(ARM) %>% |
|
91 |
#' reorder(TRTORD)) |
|
92 |
#' attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]] |
|
93 |
#' |
|
94 |
#' g_scatterplot( |
|
95 |
#' label = "Scatter Plot", |
|
96 |
#' data = ADLB, |
|
97 |
#' param_var = "PARAMCD", |
|
98 |
#' param = c("ALT"), |
|
99 |
#' xaxis_var = "BASE", |
|
100 |
#' yaxis_var = "AVAL", |
|
101 |
#' trt_group = "ARM", |
|
102 |
#' visit = "AVISITCD", |
|
103 |
#' loq_flag_var = "LOQFL", |
|
104 |
#' unit = "AVALU", |
|
105 |
#' color_manual = color_manual, |
|
106 |
#' shape_manual = shape_manual, |
|
107 |
#' facet_ncol = 2, |
|
108 |
#' facet = TRUE, |
|
109 |
#' facet_var = "ARM", |
|
110 |
#' reg_line = TRUE, |
|
111 |
#' hline = NULL, |
|
112 |
#' vline = .5, |
|
113 |
#' rotate_xlab = TRUE, |
|
114 |
#' font_size = 14, |
|
115 |
#' dot_size = 2, |
|
116 |
#' reg_text_size = 3 |
|
117 |
#' ) |
|
118 |
g_scatterplot <- function(label = "Scatter Plot", |
|
119 |
data, |
|
120 |
param_var = "PARAMCD", |
|
121 |
param = "CRP", |
|
122 |
xaxis_var = "BASE", |
|
123 |
yaxis_var = "AVAL", |
|
124 |
trt_group = "ARM", |
|
125 |
visit = "AVISITCD", |
|
126 |
loq_flag_var = "LOQFL", |
|
127 |
unit = "AVALU", |
|
128 |
xlim = c(NA, NA), |
|
129 |
ylim = c(NA, NA), |
|
130 |
color_manual = NULL, |
|
131 |
shape_manual = NULL, |
|
132 |
facet_ncol = 2, |
|
133 |
facet = FALSE, |
|
134 |
facet_var = "ARM", |
|
135 |
reg_line = FALSE, |
|
136 |
hline = NULL, |
|
137 |
vline = NULL, |
|
138 |
rotate_xlab = FALSE, |
|
139 |
font_size = 12, |
|
140 |
dot_size = NULL, |
|
141 |
reg_text_size = 3) { |
|
142 | ! |
lifecycle::deprecate_soft( |
143 | ! |
when = "0.1.15", |
144 | ! |
what = "g_scatterplot()", |
145 | ! |
details = "You should use goshawk::g_correlationplot instead of goshawk::g_scatterplot" |
146 |
) |
|
147 | ||
148 | ! |
checkmate::assert_numeric(xlim, len = 2) |
149 | ! |
checkmate::assert_numeric(ylim, len = 2) |
150 | ||
151 |
# create scatter plot over time pairwise per treatment arm |
|
152 | ! |
plot_data <- data %>% |
153 | ! |
filter(!!sym(param_var) == param) |
154 |
# Setup the ggtitle label. Combine the biomarker and the units (if available) |
|
155 | ! |
ggtitle_label <- ifelse(is.null(unit), paste0(plot_data$PARAM, "@ Visits"), |
156 | ! |
ifelse(plot_data[[unit]] == "", paste(plot_data$PARAM, "@ Visits"), |
157 | ! |
paste0(plot_data$PARAM, " (", plot_data[[unit]], ") @ Visits") |
158 |
) |
|
159 |
) |
|
160 |
# Setup the x-axis label. Combine the biomarker and the units (if available) |
|
161 | ! |
x_axis_label <- ifelse(is.null(unit), paste(plot_data$PARAM, xaxis_var, "Values"), |
162 | ! |
ifelse(plot_data[[unit]] == "", paste(plot_data$PARAM, xaxis_var, "Values"), |
163 | ! |
paste0(plot_data$PARAM, " (", plot_data[[unit]], ") ", xaxis_var, " Values") |
164 |
) |
|
165 |
) |
|
166 |
# Setup the y-axis label. Combine the biomarker and the units (if available) |
|
167 | ! |
y_axis_label <- ifelse(is.null(unit), paste(plot_data$PARAM, yaxis_var, "Values"), |
168 | ! |
ifelse(plot_data[[unit]] == "", paste(plot_data$PARAM, yaxis_var, "Values"), |
169 | ! |
paste0(plot_data$PARAM, " (", plot_data[[unit]], ") ", yaxis_var, " Values") |
170 |
) |
|
171 |
) |
|
172 |
# Setup legend label |
|
173 | ! |
trt_label <- `if`(is.null(attr(data[[trt_group]], "label")), "Dose", attr(data[[trt_group]], "label")) |
174 |
# create plot foundation |
|
175 | ! |
plot1 <- ggplot2::ggplot( |
176 | ! |
data = plot_data, |
177 | ! |
ggplot2::aes( |
178 | ! |
x = !!sym(xaxis_var), |
179 | ! |
y = !!sym(yaxis_var), |
180 | ! |
color = !!sym(trt_group) |
181 |
) |
|
182 |
) + |
|
183 | ! |
ggplot2::geom_point(ggplot2::aes(shape = !!sym(loq_flag_var)), size = dot_size, na.rm = TRUE) + |
184 | ! |
ggplot2::coord_cartesian(xlim = xlim, ylim = ylim) + |
185 | ! |
ggplot2::facet_wrap(stats::as.formula(paste0(" ~ ", visit)), ncol = facet_ncol) + |
186 | ! |
ggplot2::theme_bw() + |
187 | ! |
ggplot2::ggtitle(ggtitle_label) + |
188 | ! |
ggplot2::theme(plot.title = ggplot2::element_text(size = font_size, hjust = 0.5)) + |
189 | ! |
ggplot2::xlab(x_axis_label) + |
190 | ! |
ggplot2::ylab(y_axis_label) |
191 |
# add grid faceting to foundation |
|
192 | ! |
if (facet) { |
193 | ! |
plot1 <- plot1 + |
194 | ! |
ggplot2::facet_grid(stats::as.formula(paste0(facet_var, " ~ ", visit))) |
195 |
} |
|
196 |
# add regression line |
|
197 | ! |
if (reg_line) { |
198 | ! |
slope <- function(x, y) { |
199 | ! |
ratio <- stats::sd(x) / stats::sd(y) |
200 | ! |
res <- if (!is.na(ratio) && ratio > 0) { |
201 | ! |
reg <- mc.deming(y, x, ratio) |
202 |
# return the evaluation of the ratio condition as third value in numeric vector |
|
203 |
# for conttroling downstream processing |
|
204 | ! |
c( |
205 | ! |
round(reg$b0, 2), round(reg$b1, 2), |
206 | ! |
ifelse(!is.na(ratio) && ratio > 0, stats::cor(y, x, method = "spearman", use = "complete.obs"), NA_real_) |
207 |
) |
|
208 |
} else { |
|
209 |
# if ratio condition is not met then assign NA to returned vector |
|
210 |
# so that NULL condition does not throw error below |
|
211 | ! |
as.numeric(c(NA, NA, NA)) |
212 |
} |
|
213 | ! |
return(as_tibble(stats::setNames(as.list(res), c("intercept", "slope", "corr")))) |
214 |
} |
|
215 | ! |
sub_data <- plot_data %>% |
216 | ! |
select(!!sym(trt_group), !!sym(visit), !!sym(xaxis_var), !!sym(yaxis_var)) %>% |
217 | ! |
filter(!is.na(!!sym(yaxis_var)) & !is.na(!!sym(xaxis_var))) %>% |
218 | ! |
group_by(!!sym(trt_group), !!sym(visit)) %>% |
219 | ! |
do(slope(.data[[yaxis_var]], .data[[xaxis_var]])) |
220 | ||
221 | ! |
if (!(all(is.na(sub_data$intercept)) && all(is.na(sub_data$slope)))) { |
222 | ! |
plot1 <- plot1 + |
223 | ! |
ggplot2::geom_abline( |
224 | ! |
data = sub_data, |
225 |
# has to put some neutral values for missings, i.e. big & negative intercept + 0 slope |
|
226 | ! |
ggplot2::aes( |
227 | ! |
intercept = vapply(.data$intercept, function(x) if (!is.na(x)) x else numeric(1), FUN.VALUE = -9999), |
228 | ! |
slope = vapply(.data$slope, function(x) if (!is.na(x)) x else numeric(1), FUN.VALUE = 0), |
229 | ! |
color = !!sym(trt_group) |
230 |
) |
|
231 |
) |
|
232 |
} |
|
233 | ! |
plot1 <- plot1 + |
234 | ! |
ggplot2::geom_text( |
235 | ! |
data = filter(sub_data, row_number() == 1), |
236 | ! |
ggplot2::aes( |
237 | ! |
x = -Inf, |
238 | ! |
y = Inf, |
239 | ! |
hjust = 0, |
240 | ! |
vjust = 1, |
241 | ! |
label = ifelse( |
242 | ! |
!is.na(.data$intercept) & !is.na(.data$slope) & !is.na(.data$corr), |
243 | ! |
sprintf("y = %.2f+%.2fX\ncor = %.2f", .data$intercept, .data$slope, .data$corr), |
244 | ! |
paste0("Insufficient Data For Regression") |
245 |
), |
|
246 | ! |
color = !!sym(trt_group) |
247 |
), |
|
248 | ! |
size = reg_text_size |
249 |
) + |
|
250 | ! |
ggplot2::labs(caption = paste("Deming Regression Model, Spearman Correlation Method")) |
251 |
} |
|
252 |
# Add abline |
|
253 | ! |
if (yaxis_var %in% c("AVAL", "AVALL2", "BASE2", "BASE2L2", "BASE", "BASEL2")) { |
254 | ! |
plot1 <- plot1 + ggplot2::geom_abline(intercept = 0, slope = 1) |
255 |
} |
|
256 | ! |
if (yaxis_var %in% c("CHG2", "CHG")) { |
257 | ! |
plot1 <- plot1 + ggplot2::geom_abline(intercept = 0, slope = 0) |
258 |
} |
|
259 | ! |
if (yaxis_var %in% c("PCHG2", "PCHG")) { |
260 | ! |
plot1 <- plot1 + ggplot2::geom_abline(intercept = 100, slope = 0) |
261 |
} |
|
262 |
# Format font size |
|
263 | ! |
if (!is.null(font_size)) { |
264 | ! |
plot1 <- plot1 + |
265 | ! |
ggplot2::theme( |
266 | ! |
axis.title.x = ggplot2::element_text(size = font_size), |
267 | ! |
axis.text.x = ggplot2::element_text(size = font_size), |
268 | ! |
axis.title.y = ggplot2::element_text(size = font_size), |
269 | ! |
axis.text.y = ggplot2::element_text(size = font_size), |
270 | ! |
legend.title = ggplot2::element_text(size = font_size), |
271 | ! |
legend.text = ggplot2::element_text(size = font_size), |
272 | ! |
strip.text.x = ggplot2::element_text(size = font_size), |
273 | ! |
strip.text.y = ggplot2::element_text(size = font_size) |
274 |
) |
|
275 |
} |
|
276 |
# Format treatment color |
|
277 | ! |
if (!is.null(color_manual)) { |
278 | ! |
plot1 <- plot1 + |
279 | ! |
ggplot2::scale_color_manual(values = color_manual, name = trt_label, guide = ggplot2::guide_legend(order = 1)) |
280 |
} else { |
|
281 | ! |
plot1 + |
282 | ! |
ggplot2::scale_color_discrete(guide = ggplot2::guide_legend(order = 1)) |
283 |
} |
|
284 | ||
285 |
# Format LOQ flag symbol shape |
|
286 | ! |
if (!is.null(shape_manual)) { |
287 | ! |
plot1 <- plot1 + |
288 | ! |
ggplot2::scale_shape_manual(values = shape_manual, name = "LOQ") |
289 |
} |
|
290 |
# Format dot size |
|
291 | ! |
if (!is.null(dot_size)) { |
292 | ! |
plot1 <- plot1 + |
293 | ! |
ggplot2::geom_point(ggplot2::aes(shape = !!sym(loq_flag_var)), size = dot_size, na.rm = TRUE) |
294 |
} |
|
295 |
# Format x-label |
|
296 | ! |
if (rotate_xlab) { |
297 | ! |
plot1 <- plot1 + |
298 | ! |
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) |
299 |
} |
|
300 |
# Add horizontal line |
|
301 | ! |
if (!is.null(hline)) { |
302 | ! |
plot1 <- plot1 + |
303 | ! |
ggplot2::geom_hline(ggplot2::aes(yintercept = hline), color = "red", linetype = "dashed", size = 0.5) |
304 |
} |
|
305 |
# Add vertical line |
|
306 | ! |
if (!is.null(vline)) { |
307 | ! |
plot1 <- plot1 + |
308 | ! |
ggplot2::geom_vline(ggplot2::aes(xintercept = vline), color = "red", linetype = "dashed", size = 0.5) |
309 |
} |
|
310 | ! |
plot1 |
311 |
} |
1 |
#' Function to create line plot of summary statistics over time. |
|
2 |
#' |
|
3 |
#' @param label text string to be displayed as plot label. |
|
4 |
#' @param data `ADaM` structured analysis laboratory data frame e.g. `ADLB`. |
|
5 |
#' @param biomarker_var name of variable containing biomarker names. |
|
6 |
#' @param biomarker_var_label name of variable containing biomarker labels. |
|
7 |
#' @param loq_flag_var name of variable containing `LOQ` flag e.g. `LOQFL`. |
|
8 |
#' @param biomarker biomarker name to be analyzed. |
|
9 |
#' @param value_var name of variable containing biomarker results. |
|
10 |
#' @param unit_var name of variable containing biomarker result unit. |
|
11 |
#' @param trt_group name of variable representing treatment group. |
|
12 |
#' @param trt_group_level vector that can be used to define the factor level of `trt_group`. |
|
13 |
#' @param shape categorical variable whose levels are used to split the plot lines. |
|
14 |
#' @param shape_type vector of symbol types. |
|
15 |
#' @param time name of variable containing visit names. |
|
16 |
#' @param time_level vector that can be used to define the factor level of time. Only use it when |
|
17 |
#' x-axis variable is character or factor. |
|
18 |
#' @param color_manual vector of colors. |
|
19 |
#' @param line_type vector of line types. |
|
20 |
#' @param ylim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the y-axis |
|
21 |
#' if the default limits are not suitable. |
|
22 |
#' @param median boolean whether to display median results. |
|
23 |
#' @param hline_arb ('numeric vector') value identifying intercept for arbitrary horizontal lines. |
|
24 |
#' @param hline_arb_color ('character vector') optional, color for the arbitrary horizontal lines. |
|
25 |
#' @param hline_arb_label ('character vector') optional, label for the legend to the arbitrary horizontal lines. |
|
26 |
#' @param xtick a vector to define the tick values of time in x-axis. |
|
27 |
#' Default value is `ggplot2::waiver()`. |
|
28 |
#' @param xlabel vector with same length of `xtick` to define the label of x-axis tick values. |
|
29 |
#' Default value is `ggplot2::waiver()`. |
|
30 |
#' @param rotate_xlab boolean whether to rotate x-axis labels. |
|
31 |
#' @param plot_font_size control font size for title, x-axis, y-axis and legend font. |
|
32 |
#' @param dodge control position dodge. |
|
33 |
#' @param plot_height height of produced plot. 989 pixels by default. |
|
34 |
#' @param count_threshold \code{integer} minimum number observations needed to show the appropriate |
|
35 |
#' bar and point on the plot. Default: 0 |
|
36 |
#' @param table_font_size \code{float} controls the font size of the values printed in the table. |
|
37 |
#' Default: 12 |
|
38 |
#' @param display_center_tbl boolean whether to include table of means or medians |
|
39 |
#' |
|
40 |
#' |
|
41 |
#' @author Balazs Toth (toth.balazs@gene.com) |
|
42 |
#' @author Wenyi Liu (wenyi.liu@roche.com) |
|
43 |
#' |
|
44 |
#' @details Currently, the output plot can display mean and median of input value. For mean, the |
|
45 |
#' error bar denotes |
|
46 |
#' 95\% confidence interval. For median, the error bar denotes median-25% quartile to median+75% |
|
47 |
#' quartile. |
|
48 |
#' |
|
49 |
#' @return \code{ggplot} object |
|
50 |
#' |
|
51 |
#' @export |
|
52 |
#' |
|
53 |
#' @examples |
|
54 |
#' # Example using ADaM structure analysis dataset. |
|
55 |
#' |
|
56 |
#' library(stringr) |
|
57 |
#' library(dplyr) |
|
58 |
#' library(nestcolor) |
|
59 |
#' |
|
60 |
#' # original ARM value = dose value |
|
61 |
#' arm_mapping <- list( |
|
62 |
#' "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination" |
|
63 |
#' ) |
|
64 |
#' color_manual <- c("150mg QD" = "thistle", "Placebo" = "orange", "Combination" = "steelblue") |
|
65 |
#' type_manual <- c("150mg QD" = "solid", "Placebo" = "dashed", "Combination" = "dotted") |
|
66 |
#' |
|
67 |
#' ADSL <- rADSL %>% filter(!(ARM == "B: Placebo" & AGE < 40)) |
|
68 |
#' ADLB <- rADLB |
|
69 |
#' ADLB <- right_join(ADLB, ADSL[, c("STUDYID", "USUBJID")]) |
|
70 |
#' var_labels <- lapply(ADLB, function(x) attributes(x)$label) |
|
71 |
#' |
|
72 |
#' ADLB <- ADLB %>% |
|
73 |
#' mutate(AVISITCD = case_when( |
|
74 |
#' AVISIT == "SCREENING" ~ "SCR", |
|
75 |
#' AVISIT == "BASELINE" ~ "BL", |
|
76 |
#' grepl("WEEK", AVISIT) ~ |
|
77 |
#' paste( |
|
78 |
#' "W", |
|
79 |
#' trimws( |
|
80 |
#' substr( |
|
81 |
#' AVISIT, |
|
82 |
#' start = 6, |
|
83 |
#' stop = str_locate(AVISIT, "DAY") - 1 |
|
84 |
#' ) |
|
85 |
#' ) |
|
86 |
#' ), |
|
87 |
#' TRUE ~ NA_character_ |
|
88 |
#' )) %>% |
|
89 |
#' mutate(AVISITCDN = case_when( |
|
90 |
#' AVISITCD == "SCR" ~ -2, |
|
91 |
#' AVISITCD == "BL" ~ 0, |
|
92 |
#' grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)), |
|
93 |
#' TRUE ~ NA_real_ |
|
94 |
#' )) %>% |
|
95 |
#' # use ARMCD values to order treatment in visualization legend |
|
96 |
#' mutate(TRTORD = ifelse(grepl("C", ARMCD), 1, |
|
97 |
#' ifelse(grepl("B", ARMCD), 2, |
|
98 |
#' ifelse(grepl("A", ARMCD), 3, NA) |
|
99 |
#' ) |
|
100 |
#' )) %>% |
|
101 |
#' mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>% |
|
102 |
#' mutate(ARM = factor(ARM) %>% |
|
103 |
#' reorder(TRTORD)) |
|
104 |
#' attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]] |
|
105 |
#' |
|
106 |
#' g_lineplot( |
|
107 |
#' label = "Line Plot", |
|
108 |
#' data = ADLB, |
|
109 |
#' biomarker_var = "PARAMCD", |
|
110 |
#' biomarker = "CRP", |
|
111 |
#' value_var = "AVAL", |
|
112 |
#' trt_group = "ARM", |
|
113 |
#' shape = NULL, |
|
114 |
#' time = "AVISITCDN", |
|
115 |
#' color_manual = color_manual, |
|
116 |
#' line_type = type_manual, |
|
117 |
#' median = FALSE, |
|
118 |
#' hline_arb = c(.9, 1.1, 1.2, 1.5), |
|
119 |
#' hline_arb_color = c("green", "red", "blue", "pink"), |
|
120 |
#' hline_arb_label = c("A", "B", "C", "D"), |
|
121 |
#' xtick = c(0, 1, 5), |
|
122 |
#' xlabel = c("Baseline", "Week 1", "Week 5"), |
|
123 |
#' rotate_xlab = FALSE, |
|
124 |
#' plot_height = 600 |
|
125 |
#' ) |
|
126 |
#' |
|
127 |
#' g_lineplot( |
|
128 |
#' label = "Line Plot", |
|
129 |
#' data = ADLB, |
|
130 |
#' biomarker_var = "PARAMCD", |
|
131 |
#' biomarker = "CRP", |
|
132 |
#' value_var = "AVAL", |
|
133 |
#' trt_group = "ARM", |
|
134 |
#' shape = NULL, |
|
135 |
#' time = "AVISITCD", |
|
136 |
#' color_manual = NULL, |
|
137 |
#' line_type = type_manual, |
|
138 |
#' median = TRUE, |
|
139 |
#' hline_arb = c(.9, 1.1, 1.2, 1.5), |
|
140 |
#' hline_arb_color = c("green", "red", "blue", "pink"), |
|
141 |
#' hline_arb_label = c("A", "B", "C", "D"), |
|
142 |
#' xtick = c("BL", "W 1", "W 5"), |
|
143 |
#' xlabel = c("Baseline", "Week 1", "Week 5"), |
|
144 |
#' rotate_xlab = FALSE, |
|
145 |
#' plot_height = 600 |
|
146 |
#' ) |
|
147 |
#' |
|
148 |
#' g_lineplot( |
|
149 |
#' label = "Line Plot", |
|
150 |
#' data = ADLB, |
|
151 |
#' biomarker_var = "PARAMCD", |
|
152 |
#' biomarker = "CRP", |
|
153 |
#' value_var = "AVAL", |
|
154 |
#' trt_group = "ARM", |
|
155 |
#' shape = NULL, |
|
156 |
#' time = "AVISITCD", |
|
157 |
#' color_manual = color_manual, |
|
158 |
#' line_type = type_manual, |
|
159 |
#' median = FALSE, |
|
160 |
#' hline_arb = c(.9, 1.1, 1.2, 1.5), |
|
161 |
#' hline_arb_color = c("green", "red", "blue", "pink"), |
|
162 |
#' hline_arb_label = c("A", "B", "C", "D"), |
|
163 |
#' xtick = c("BL", "W 1", "W 5"), |
|
164 |
#' xlabel = c("Baseline", "Week 1", "Week 5"), |
|
165 |
#' rotate_xlab = FALSE, |
|
166 |
#' plot_height = 600, |
|
167 |
#' count_threshold = 90, |
|
168 |
#' table_font_size = 15 |
|
169 |
#' ) |
|
170 |
#' |
|
171 |
#' g_lineplot( |
|
172 |
#' label = "Line Plot", |
|
173 |
#' data = ADLB, |
|
174 |
#' biomarker_var = "PARAMCD", |
|
175 |
#' biomarker = "CRP", |
|
176 |
#' value_var = "AVAL", |
|
177 |
#' trt_group = "ARM", |
|
178 |
#' shape = NULL, |
|
179 |
#' time = "AVISITCDN", |
|
180 |
#' color_manual = color_manual, |
|
181 |
#' line_type = type_manual, |
|
182 |
#' median = TRUE, |
|
183 |
#' hline_arb = c(.9, 1.1, 1.2, 1.5), |
|
184 |
#' hline_arb_color = c("green", "red", "blue", "pink"), |
|
185 |
#' hline_arb_label = c("A", "B", "C", "D"), |
|
186 |
#' xtick = c(0, 1, 5), |
|
187 |
#' xlabel = c("Baseline", "Week 1", "Week 5"), |
|
188 |
#' rotate_xlab = FALSE, |
|
189 |
#' plot_height = 600 |
|
190 |
#' ) |
|
191 |
#' |
|
192 |
#' g_lineplot( |
|
193 |
#' label = "Line Plot", |
|
194 |
#' data = subset(ADLB, SEX %in% c("M", "F")), |
|
195 |
#' biomarker_var = "PARAMCD", |
|
196 |
#' biomarker = "CRP", |
|
197 |
#' value_var = "AVAL", |
|
198 |
#' trt_group = "ARM", |
|
199 |
#' shape = "SEX", |
|
200 |
#' time = "AVISITCDN", |
|
201 |
#' color_manual = color_manual, |
|
202 |
#' line_type = type_manual, |
|
203 |
#' median = FALSE, |
|
204 |
#' hline_arb = c(.9, 1.1, 1.2, 1.5), |
|
205 |
#' hline_arb_color = c("green", "red", "blue", "pink"), |
|
206 |
#' hline_arb_label = c("A", "B", "C", "D"), |
|
207 |
#' xtick = c(0, 1, 5), |
|
208 |
#' xlabel = c("Baseline", "Week 1", "Week 5"), |
|
209 |
#' rotate_xlab = FALSE, |
|
210 |
#' plot_height = 1500 |
|
211 |
#' ) |
|
212 |
#' |
|
213 |
#' g_lineplot( |
|
214 |
#' label = "Line Plot", |
|
215 |
#' data = subset(ADLB, SEX %in% c("M", "F")), |
|
216 |
#' biomarker_var = "PARAMCD", |
|
217 |
#' biomarker = "CRP", |
|
218 |
#' value_var = "AVAL", |
|
219 |
#' trt_group = "ARM", |
|
220 |
#' shape = "SEX", |
|
221 |
#' time = "AVISITCDN", |
|
222 |
#' color_manual = NULL, |
|
223 |
#' median = FALSE, |
|
224 |
#' hline_arb = c(.9, 1.1, 1.2, 1.5), |
|
225 |
#' hline_arb_color = c("green", "red", "blue", "pink"), |
|
226 |
#' hline_arb_label = c("A", "B", "C", "D"), |
|
227 |
#' xtick = c(0, 1, 5), |
|
228 |
#' xlabel = c("Baseline", "Week 1", "Week 5"), |
|
229 |
#' rotate_xlab = FALSE, |
|
230 |
#' plot_height = 1500 |
|
231 |
#' ) |
|
232 |
g_lineplot <- function(label = "Line Plot", |
|
233 |
data, |
|
234 |
biomarker_var = "PARAMCD", |
|
235 |
biomarker_var_label = "PARAM", |
|
236 |
biomarker, |
|
237 |
value_var = "AVAL", |
|
238 |
unit_var = "AVALU", |
|
239 |
loq_flag_var = "LOQFL", |
|
240 |
ylim = c(NA, NA), |
|
241 |
trt_group, |
|
242 |
trt_group_level = NULL, |
|
243 |
shape = NULL, |
|
244 |
shape_type = NULL, |
|
245 |
time, |
|
246 |
time_level = NULL, |
|
247 |
color_manual = NULL, |
|
248 |
line_type = NULL, |
|
249 |
median = FALSE, |
|
250 |
hline_arb = numeric(0), |
|
251 |
hline_arb_color = "red", |
|
252 |
hline_arb_label = "Horizontal line", |
|
253 |
xtick = ggplot2::waiver(), |
|
254 |
xlabel = xtick, |
|
255 |
rotate_xlab = FALSE, |
|
256 |
plot_font_size = 12, |
|
257 |
dodge = 0.4, |
|
258 |
plot_height = 989, |
|
259 |
count_threshold = 0, |
|
260 |
table_font_size = 12, |
|
261 |
display_center_tbl = TRUE) { |
|
262 | ! |
checkmate::assert_numeric(ylim, len = 2) |
263 | ||
264 |
## Pre-process data |
|
265 | ! |
table_font_size <- grid::convertX(grid::unit(table_font_size, "points"), "mm", valueOnly = TRUE) |
266 | ||
267 |
## - convert to factors |
|
268 | ! |
label_trt_group <- attr(data[[trt_group]], "label") |
269 | ! |
data[[trt_group]] <- if (is.null(trt_group_level)) { |
270 | ! |
factor(data[[trt_group]]) |
271 |
} else { |
|
272 | ! |
factor(data[[trt_group]], levels = trt_group_level) |
273 |
} |
|
274 | ! |
attr(data[[trt_group]], "label") <- label_trt_group |
275 | ||
276 | ! |
color_manual <- if (is.null(color_manual)) { |
277 | ! |
temp <- if (!is.null(getOption("ggplot2.discrete.colour"))) { |
278 | ! |
getOption("ggplot2.discrete.colour")[1:nlevels(data[[trt_group]])] |
279 |
} else { |
|
280 | ! |
gg_color_hue(nlevels(data[[trt_group]])) |
281 |
} |
|
282 | ! |
names(temp) <- levels(data[[trt_group]]) |
283 | ! |
temp |
284 |
} else { |
|
285 | ! |
stopifnot(all(levels(data[[trt_group]]) %in% names(color_manual))) |
286 | ! |
color_manual |
287 |
} |
|
288 | ||
289 | ! |
line_type <- if (is.null(line_type)) { |
290 | ! |
stats::setNames(rep("dashed", nlevels(data[[trt_group]])), levels(data[[trt_group]])) |
291 |
} else { |
|
292 | ! |
stopifnot(all(levels(data[[trt_group]]) %in% names(line_type))) |
293 | ! |
line_type |
294 |
} |
|
295 | ||
296 | ! |
shape_type <- if (is.null(shape)) { |
297 | ! |
NULL |
298 |
} else { |
|
299 | ! |
if (is.null(shape_type)) { |
300 | ! |
default_shapes <- c(15:18, 3:14, 0:2) |
301 | ! |
res <- if (nlevels(data[[shape]]) > length(default_shapes)) { |
302 | ! |
rep(default_shapes, ceiling(nlevels(data[[shape]]) / length(default_shapes))) |
303 |
} else { |
|
304 | ! |
default_shapes[seq_len(nlevels(data[[shape]]))] |
305 |
} |
|
306 | ! |
stats::setNames(res, levels(data[[shape]])) |
307 |
} else { |
|
308 | ! |
stopifnot(all(levels(data[[shape]]) %in% names(shape_type))) |
309 | ! |
shape_type |
310 |
} |
|
311 |
} |
|
312 | ||
313 | ! |
xtype <- if (is.factor(data[[time]]) || is.character(data[[time]])) { |
314 | ! |
"discrete" |
315 |
} else { |
|
316 | ! |
"continuous" |
317 |
} |
|
318 | ! |
if (xtype == "discrete") { |
319 | ! |
data[[time]] <- if (is.null(time_level)) { |
320 | ! |
factor(data[[time]]) |
321 |
} else { |
|
322 | ! |
factor(data[[time]], levels = time_level) |
323 |
} |
|
324 |
} |
|
325 | ||
326 | ! |
groupings <- c(time, trt_group, shape) |
327 |
## Summary statistics |
|
328 | ! |
sum_data <- data %>% |
329 | ! |
filter(!!sym(biomarker_var) == biomarker) %>% |
330 | ! |
group_by_at(groupings) %>% |
331 | ! |
summarise( |
332 | ! |
count = sum(!is.na(!!sym(value_var))), |
333 | ! |
mean = mean(!!sym(value_var), na.rm = TRUE), |
334 | ! |
CIup = mean(!!sym(value_var), na.rm = TRUE) + 1.96 * stats::sd(!!sym(value_var), na.rm = TRUE) / sqrt(n()), |
335 | ! |
CIdown = mean(!!sym(value_var), na.rm = TRUE) - 1.96 * stats::sd(!!sym(value_var), na.rm = TRUE) / sqrt(n()), |
336 | ! |
median = stats::median(!!sym(value_var), na.rm = TRUE), |
337 | ! |
quant25 = stats::quantile(!!sym(value_var), 0.25, na.rm = TRUE), |
338 | ! |
quant75 = stats::quantile(!!sym(value_var), 0.75, na.rm = TRUE) |
339 |
) %>% |
|
340 | ! |
arrange_at(c(trt_group, shape)) |
341 | ||
342 |
## Filter out rows with insufficient number of counts |
|
343 | ! |
listin <- list() |
344 | ! |
listin[[trt_group]] <- sum_data[[trt_group]] |
345 | ||
346 | ! |
if (!is.null(shape)) { |
347 | ! |
listin[[shape]] <- sum_data[[shape]] |
348 |
} |
|
349 | ||
350 | ! |
int <- unique_name("int", names(sum_data)) |
351 | ! |
sum_data[[int]] <- new_interaction(listin, sep = " ") |
352 | ! |
sum_data[[int]] <- stringr::str_wrap(sum_data[[int]], 12) |
353 | ! |
sum_data[[int]] <- factor(sum_data[[int]], sort(unique(sum_data[[int]]))) |
354 | ||
355 | ! |
unfiltered_data <- sum_data %>% mutate("met_threshold" = count >= count_threshold) |
356 | ! |
sum_data <- unfiltered_data %>% filter(.data[["met_threshold"]]) |
357 | ||
358 |
## Base plot |
|
359 | ! |
pd <- ggplot2::position_dodge(dodge) |
360 | ! |
if (median) { |
361 | ! |
line <- "median" |
362 | ! |
up_limit <- "quant75" |
363 | ! |
down_limit <- "quant25" |
364 |
} else { |
|
365 | ! |
line <- "mean" |
366 | ! |
up_limit <- "CIup" |
367 | ! |
down_limit <- "CIdown" |
368 |
} |
|
369 | ||
370 | ! |
filtered_data <- data %>% |
371 | ! |
filter(!!sym(biomarker_var) == biomarker) |
372 | ||
373 | ! |
unit <- filtered_data %>% |
374 | ! |
pull(unit_var) %>% |
375 | ! |
unique() |
376 | ||
377 | ! |
unit1 <- if (is.na(unit) || unit == "") { |
378 |
" " |
|
379 |
} else { |
|
380 | ! |
paste0(" (", unit, ") ") |
381 |
} |
|
382 | ||
383 | ! |
biomarker1 <- filtered_data %>% |
384 | ! |
pull(biomarker_var_label) %>% |
385 | ! |
unique() |
386 | ||
387 | ! |
gtitle <- paste0(biomarker1, unit1, stringr::str_to_title(line), " by Treatment @ Visits") |
388 | ! |
gylab <- paste0(biomarker1, " ", stringr::str_to_title(line), " of ", value_var, " Values") |
389 | ||
390 |
# Setup legend label |
|
391 | ! |
trt_label <- `if`(is.null(attr(data[[trt_group]], "label")), "Dose", attr(data[[trt_group]], "label")) |
392 | ||
393 |
# Add footnote to identify LLOQ and ULOQ values pulled from data |
|
394 | ! |
caption_loqs_label <- h_caption_loqs_label(loqs_data = filtered_data, flag_var = loq_flag_var) |
395 | ||
396 | ! |
if (is.null(shape)) { |
397 | ! |
plot1 <- ggplot2::ggplot( |
398 | ! |
data = sum_data, |
399 | ! |
ggplot2::aes( |
400 | ! |
x = !!sym(time), |
401 | ! |
y = !!sym(line), |
402 | ! |
color = !!sym(trt_group), |
403 | ! |
linetype = !!sym(trt_group), |
404 | ! |
group = !!sym(int) |
405 |
) |
|
406 |
) + |
|
407 | ! |
ggplot2::theme_bw() + |
408 | ! |
ggplot2::geom_point(position = pd) + |
409 | ! |
ggplot2::scale_color_manual( |
410 | ! |
values = color_manual, name = trt_label, guide = ggplot2::guide_legend(ncol = 3, order = 1) |
411 |
) + |
|
412 | ! |
ggplot2::scale_linetype_manual( |
413 | ! |
values = line_type, name = trt_label, guide = ggplot2::guide_legend(ncol = 3, order = 1) |
414 |
) |
|
415 |
} else { |
|
416 | ! |
mappings <- sum_data %>% |
417 | ! |
ungroup() %>% |
418 | ! |
select(!!sym(trt_group), !!sym(shape), int) %>% |
419 | ! |
distinct() %>% |
420 | ! |
mutate( |
421 | ! |
cols = color_manual[as.character(!!sym(trt_group))], |
422 | ! |
types = line_type[as.character(!!sym(trt_group))], |
423 | ! |
shps = shape_type[!!sym(shape)] |
424 |
) |
|
425 | ||
426 | ! |
col_mapping <- stats::setNames(mappings$cols, mappings$int) |
427 | ! |
shape_mapping <- stats::setNames(mappings$shps, mappings$int) |
428 | ! |
type_mapping <- stats::setNames(mappings$types, mappings$int) |
429 | ||
430 | ! |
plot1 <- ggplot2::ggplot( |
431 | ! |
data = sum_data, |
432 | ! |
ggplot2::aes( |
433 | ! |
x = !!sym(time), |
434 | ! |
y = !!sym(line), |
435 | ! |
color = !!sym(int), |
436 | ! |
linetype = !!sym(int), |
437 | ! |
group = !!sym(int), |
438 | ! |
shape = !!sym(int) |
439 |
) |
|
440 |
) + |
|
441 | ! |
ggplot2::theme_bw() + |
442 | ! |
ggplot2::scale_color_manual(" ", values = col_mapping, guide = ggplot2::guide_legend(ncol = 3, order = 1)) + |
443 | ! |
ggplot2::scale_linetype_manual(" ", values = type_mapping, guide = ggplot2::guide_legend(ncol = 3, order = 1)) + |
444 | ! |
ggplot2::scale_shape_manual(" ", values = shape_mapping, guide = ggplot2::guide_legend(ncol = 3, order = 1)) + |
445 | ! |
ggplot2::theme(legend.key.size = grid::unit(1, "cm")) + |
446 | ! |
ggplot2::geom_point(position = pd, size = 3) |
447 |
} |
|
448 | ||
449 | ! |
plot1 <- plot1 + |
450 | ! |
ggplot2::geom_line(position = pd) + |
451 | ! |
ggplot2::geom_errorbar( |
452 | ! |
ggplot2::aes(ymin = !!sym(down_limit), ymax = !!sym(up_limit)), |
453 | ! |
width = 0.45, position = pd, linetype = "solid" |
454 |
) + |
|
455 | ! |
ggplot2::ggtitle(gtitle) + |
456 | ! |
ggplot2::labs(caption = paste( |
457 | ! |
"The output plot can display mean and median of input value.", |
458 | ! |
"For mean, the error bar denotes 95% confidence interval.", |
459 | ! |
"For median, the bar denotes the first to third quartile.\n", |
460 | ! |
caption_loqs_label |
461 |
)) + |
|
462 | ! |
ggplot2::xlab(time) + |
463 | ! |
ggplot2::ylab(gylab) + |
464 | ! |
ggplot2::theme( |
465 | ! |
legend.box = "vertical", |
466 | ! |
legend.position = "bottom", |
467 | ! |
legend.direction = "horizontal", |
468 | ! |
plot.title = ggplot2::element_text(size = plot_font_size, margin = ggplot2::margin(), hjust = 0.5), |
469 | ! |
axis.title.y = ggplot2::element_text(margin = ggplot2::margin(r = 20)) |
470 |
) |
|
471 | ||
472 |
# Apply y-axis zoom range |
|
473 | ! |
plot1 <- plot1 + |
474 | ! |
ggplot2::coord_cartesian(ylim = ylim) |
475 | ||
476 |
# Format x-label |
|
477 | ! |
if (xtype == "continuous") { |
478 | ! |
plot1 <- plot1 + |
479 | ! |
ggplot2::scale_x_continuous(breaks = xtick, labels = xlabel, limits = c(NA, NA)) |
480 | ! |
} else if (xtype == "discrete") { |
481 | ! |
plot1 <- plot1 + |
482 | ! |
ggplot2::scale_x_discrete(breaks = xtick, labels = xlabel) |
483 |
} |
|
484 | ||
485 | ! |
if (rotate_xlab) { |
486 | ! |
plot1 <- plot1 + |
487 | ! |
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) |
488 |
} |
|
489 | ||
490 | ||
491 | ! |
plot1 <- plot1 + geom_axes_lines( |
492 | ! |
sum_data, |
493 | ! |
hline_arb = hline_arb, hline_arb_color = hline_arb_color, hline_arb_label = hline_arb_label |
494 |
) |
|
495 | ||
496 |
# Format font size |
|
497 | ! |
if (!is.null(plot_font_size)) { |
498 | ! |
plot1 <- plot1 + |
499 | ! |
ggplot2::theme( |
500 | ! |
axis.title.x = ggplot2::element_text(size = plot_font_size), |
501 | ! |
axis.text.x = ggplot2::element_text(size = plot_font_size), |
502 | ! |
axis.title.y = ggplot2::element_text(size = plot_font_size), |
503 | ! |
axis.text.y = ggplot2::element_text(size = plot_font_size), |
504 | ! |
legend.title = ggplot2::element_text(size = plot_font_size), |
505 | ! |
legend.text = ggplot2::element_text(size = plot_font_size) |
506 |
) |
|
507 |
} |
|
508 | ||
509 | ! |
labels <- levels(unfiltered_data[[int]]) |
510 | ! |
lines <- sum(stringr::str_count(unique(labels), "\n")) / 2 + length(unique(labels)) |
511 | ! |
minline <- 36 |
512 | ! |
tabletotal <- lines * minline * ifelse(display_center_tbl, 2, 1) |
513 | ! |
plotsize <- plot_height - tabletotal |
514 | ! |
if (plotsize <= 250) { |
515 | ! |
stop("Due to number of line splitting levels the current plot height is not sufficient to display plot. |
516 | ! |
If applicable, please try a combination of: |
517 | ! |
* increasing the plot height using the Plot Aesthetic Settings, |
518 | ! |
* increasing the relative height of plot to table(s), |
519 | ! |
* increasing the initial maximum plot_height argument during creation of this app, |
520 | ! |
* and / or consider removing the mean / median table.") |
521 |
} |
|
522 | ||
523 | ! |
if (display_center_tbl) { |
524 | ! |
unfiltered_data$center <- if (median) { |
525 | ! |
sprintf(ifelse(unfiltered_data$count > 0, "%.2f", ""), unfiltered_data$median) |
526 |
} else { |
|
527 | ! |
sprintf(ifelse(unfiltered_data$count > 0, "%.2f", ""), unfiltered_data$mean) |
528 |
} |
|
529 | ! |
tbl_central_value_title <- if (median) "Median" else "Mean" |
530 | ! |
tbl_central_value <- ggplot2::ggplot( |
531 | ! |
unfiltered_data, |
532 | ! |
ggplot2::aes(x = !!sym(time), y = !!sym(int), label = .data[["center"]]) |
533 |
) + |
|
534 | ! |
ggplot2::geom_text(ggplot2::aes(color = .data[["met_threshold"]]), size = table_font_size) + |
535 | ! |
ggplot2::ggtitle(tbl_central_value_title) + |
536 | ! |
ggplot2::theme_minimal() + |
537 | ! |
ggplot2::scale_y_discrete(labels = labels) + |
538 | ! |
ggplot2::theme( |
539 | ! |
panel.grid.major = ggplot2::element_blank(), |
540 | ! |
legend.position = "none", |
541 | ! |
panel.grid.minor = ggplot2::element_blank(), |
542 | ! |
panel.border = ggplot2::element_blank(), axis.text.x = ggplot2::element_blank(), |
543 | ! |
axis.ticks = ggplot2::element_blank(), |
544 | ! |
axis.title.x = ggplot2::element_blank(), |
545 | ! |
axis.title.y = ggplot2::element_blank(), |
546 | ! |
axis.text.y = ggplot2::element_text(size = plot_font_size), |
547 | ! |
plot.title = ggplot2::element_text(face = "bold", size = plot_font_size) |
548 |
) + |
|
549 | ! |
ggplot2::scale_color_manual(values = c("FALSE" = "red", "TRUE" = "black")) |
550 |
} |
|
551 | ||
552 | ! |
tbl <- ggplot2::ggplot( |
553 | ! |
unfiltered_data, |
554 | ! |
ggplot2::aes(x = !!sym(time), y = !!sym(int), label = .data[["count"]]) |
555 |
) + |
|
556 | ! |
ggplot2::geom_text(ggplot2::aes(color = .data[["met_threshold"]]), size = table_font_size) + |
557 | ! |
ggplot2::ggtitle("Number of observations") + |
558 | ! |
ggplot2::theme_minimal() + |
559 | ! |
ggplot2::scale_y_discrete(labels = labels) + |
560 | ! |
ggplot2::theme( |
561 | ! |
panel.grid.major = ggplot2::element_blank(), |
562 | ! |
legend.position = "none", |
563 | ! |
panel.grid.minor = ggplot2::element_blank(), |
564 | ! |
panel.border = ggplot2::element_blank(), axis.text.x = ggplot2::element_blank(), |
565 | ! |
axis.ticks = ggplot2::element_blank(), |
566 | ! |
axis.title.x = ggplot2::element_blank(), |
567 | ! |
axis.title.y = ggplot2::element_blank(), |
568 | ! |
axis.text.y = ggplot2::element_text(size = plot_font_size), |
569 | ! |
plot.title = ggplot2::element_text(face = "bold", size = plot_font_size) |
570 |
) + |
|
571 | ! |
ggplot2::scale_color_manual(values = c("FALSE" = "red", "TRUE" = "black")) |
572 | ||
573 |
# Plot the grobs using plot_grid |
|
574 | ! |
if (display_center_tbl) { |
575 | ! |
cowplot::plot_grid(plot1, tbl_central_value, tbl, |
576 | ! |
align = "v", ncol = 1, |
577 | ! |
rel_heights = c(plotsize, tabletotal / 2, tabletotal / 2) |
578 |
) |
|
579 |
} else { |
|
580 | ! |
cowplot::plot_grid(plot1, tbl, align = "v", ncol = 1, rel_heights = c(plotsize, tabletotal)) |
581 |
} |
|
582 |
} |
|
583 | ||
584 |
new_interaction <- function(args, drop = FALSE, sep = ".", lex.order = FALSE) { # nolint |
|
585 | ! |
for (i in seq_along(args)) { |
586 | ! |
if (is.null(args[[i]])) { |
587 | ! |
args[[i]] <- NULL |
588 |
} |
|
589 |
} |
|
590 | ! |
if (length(args) == 1) { |
591 | ! |
return(paste0(names(args), ":", args[[1]])) |
592 |
} |
|
593 | ! |
args <- mapply(function(n, val) paste0(n, ":", val), names(args), args, SIMPLIFY = FALSE) |
594 | ! |
interaction(args, drop = drop, sep = sep, lex.order = lex.order) |
595 |
} |
|
596 | ||
597 |
unique_name <- function(newname, old_names) { |
|
598 | ! |
if (newname %in% old_names) { |
599 | ! |
unique_name(paste0(newname, "1"), old_names) |
600 |
} |
|
601 | ! |
newname |
602 |
} |
|
603 | ||
604 |
gg_color_hue <- function(n) { |
|
605 | ! |
hues <- seq(15, 375, length = n + 1) |
606 | ! |
grDevices::hcl(h = hues, l = 65, c = 100)[1:n] |
607 |
} |
1 |
#' Function to create a spaghetti plot. |
|
2 |
#' |
|
3 |
#' This function is rendered by teal.goshawk module |
|
4 |
#' |
|
5 |
#' @param data data frame with variables to be summarized and generate statistics which will display |
|
6 |
#' in the plot. |
|
7 |
#' @param subj_id unique subject id variable name. |
|
8 |
#' @param biomarker_var name of variable containing biomarker names. |
|
9 |
#' @param biomarker_var_label name of variable containing biomarker labels. |
|
10 |
#' @param biomarker biomarker name to be analyzed. |
|
11 |
#' @param value_var name of variable containing biomarker results. |
|
12 |
#' @param unit_var name of variable containing biomarker units. |
|
13 |
#' @param trt_group name of variable representing treatment group. |
|
14 |
#' @param trt_group_level vector that can be used to define the factor level of `trt_group`. |
|
15 |
#' @param loq_flag_var name of variable containing `LOQ` flag e.g. `LOQFL`. |
|
16 |
#' @param time name of variable containing visit names. |
|
17 |
#' @param time_level vector that can be used to define the factor level of time. Only use it when |
|
18 |
#' x-axis variable is character or factor. |
|
19 |
#' @param color_manual vector of colors. |
|
20 |
#' @param color_comb name or hex value for combined treatment color. |
|
21 |
#' @param ylim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the y-axis |
|
22 |
#' if the default limits are not suitable. |
|
23 |
#' @param alpha subject line transparency (0 = transparent, 1 = opaque) |
|
24 |
#' @param facet_ncol number of facets per row. |
|
25 |
#' @param facet_scales passed to `scales` in [`ggplot2::facet_wrap`]. Should scales be fixed (`"fixed"`, |
|
26 |
#' the default), free (`"free"`), or free in one dimension (`"free_x"`, `"free_y"`)? |
|
27 |
#' @param xtick a vector to define the tick values of time in x-axis. |
|
28 |
#' Default value is `ggplot2::waiver()`. |
|
29 |
#' @param xlabel vector with same length of `xtick` to define the label of x-axis tick values. Default |
|
30 |
#' value is `ggplot2::waiver()`. |
|
31 |
#' @param rotate_xlab boolean whether to rotate x-axis labels. |
|
32 |
#' @param font_size control font size for title, x-axis, y-axis and legend font. |
|
33 |
#' @param group_stats control group mean or median overlay. |
|
34 |
#' @param hline_arb ('numeric vector') value identifying intercept for arbitrary horizontal lines. |
|
35 |
#' @param hline_arb_color ('character vector') optional, color for the arbitrary horizontal lines. |
|
36 |
#' @param hline_arb_label ('character vector') optional, label for the legend to the arbitrary horizontal lines. |
|
37 |
#' @param hline_vars ('character vector'), names of variables `(ANR*)` or values `(*LOQ)` identifying intercept values. |
|
38 |
#' The data inside of the `ggplot2` object must also contain the columns with these variable names |
|
39 |
#' @param hline_vars_colors ('character vector') colors for the horizontal lines defined by variables. |
|
40 |
#' @param hline_vars_labels ('character vector') labels for the legend to the horizontal lines defined by variables. |
|
41 |
#' |
|
42 |
#' |
|
43 |
#' @author Wenyi Liu (wenyi.liu@roche.com) |
|
44 |
#' |
|
45 |
#' @return \code{ggplot} object |
|
46 |
#' |
|
47 |
#' @export |
|
48 |
#' |
|
49 |
#' @examples |
|
50 |
#' # Example using ADaM structure analysis dataset. |
|
51 |
#' |
|
52 |
#' library(stringr) |
|
53 |
#' |
|
54 |
#' # original ARM value = dose value |
|
55 |
#' arm_mapping <- list( |
|
56 |
#' "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination" |
|
57 |
#' ) |
|
58 |
#' color_manual <- c("150mg QD" = "#000000", "Placebo" = "#3498DB", "Combination" = "#E74C3C") |
|
59 |
#' |
|
60 |
#' ADLB <- rADLB |
|
61 |
#' var_labels <- lapply(ADLB, function(x) attributes(x)$label) |
|
62 |
#' ADLB <- ADLB %>% |
|
63 |
#' mutate(AVISITCD = case_when( |
|
64 |
#' AVISIT == "SCREENING" ~ "SCR", |
|
65 |
#' AVISIT == "BASELINE" ~ "BL", |
|
66 |
#' grepl("WEEK", AVISIT) ~ |
|
67 |
#' paste( |
|
68 |
#' "W", |
|
69 |
#' trimws( |
|
70 |
#' substr( |
|
71 |
#' AVISIT, |
|
72 |
#' start = 6, |
|
73 |
#' stop = str_locate(AVISIT, "DAY") - 1 |
|
74 |
#' ) |
|
75 |
#' ) |
|
76 |
#' ), |
|
77 |
#' TRUE ~ NA_character_ |
|
78 |
#' )) %>% |
|
79 |
#' mutate(AVISITCDN = case_when( |
|
80 |
#' AVISITCD == "SCR" ~ -2, |
|
81 |
#' AVISITCD == "BL" ~ 0, |
|
82 |
#' grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)), |
|
83 |
#' TRUE ~ NA_real_ |
|
84 |
#' )) %>% |
|
85 |
#' # use ARMCD values to order treatment in visualization legend |
|
86 |
#' mutate(TRTORD = ifelse(grepl("C", ARMCD), 1, |
|
87 |
#' ifelse(grepl("B", ARMCD), 2, |
|
88 |
#' ifelse(grepl("A", ARMCD), 3, NA) |
|
89 |
#' ) |
|
90 |
#' )) %>% |
|
91 |
#' mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>% |
|
92 |
#' mutate(ARM = factor(ARM) %>% |
|
93 |
#' reorder(TRTORD)) %>% |
|
94 |
#' mutate(ANRLO = .5, ANRHI = 1) %>% |
|
95 |
#' rowwise() %>% |
|
96 |
#' group_by(PARAMCD) %>% |
|
97 |
#' mutate(LBSTRESC = ifelse(USUBJID %in% sample(USUBJID, 1, replace = TRUE), |
|
98 |
#' paste("<", round(runif(1, min = .5, max = .7))), LBSTRESC |
|
99 |
#' )) %>% |
|
100 |
#' mutate(LBSTRESC = ifelse(USUBJID %in% sample(USUBJID, 1, replace = TRUE), |
|
101 |
#' paste(">", round(runif(1, min = .9, max = 1.2))), LBSTRESC |
|
102 |
#' )) %>% |
|
103 |
#' ungroup() |
|
104 |
#' attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]] |
|
105 |
#' attr(ADLB[["ANRLO"]], "label") <- "Analysis Normal Range Lower Limit" |
|
106 |
#' attr(ADLB[["ANRHI"]], "label") <- "Analysis Normal Range Upper Limit" |
|
107 |
#' |
|
108 |
#' # add LLOQ and ULOQ variables |
|
109 |
#' ADLB_LOQS <- goshawk:::h_identify_loq_values(ADLB, "LOQFL") |
|
110 |
#' ADLB <- left_join(ADLB, ADLB_LOQS, by = "PARAM") |
|
111 |
#' |
|
112 |
#' g_spaghettiplot( |
|
113 |
#' data = ADLB, |
|
114 |
#' subj_id = "USUBJID", |
|
115 |
#' biomarker_var = "PARAMCD", |
|
116 |
#' biomarker = "CRP", |
|
117 |
#' value_var = "AVAL", |
|
118 |
#' trt_group = "ARM", |
|
119 |
#' time = "AVISITCD", |
|
120 |
#' color_manual = color_manual, |
|
121 |
#' color_comb = "#39ff14", |
|
122 |
#' alpha = .02, |
|
123 |
#' xtick = c("BL", "W 1", "W 4"), |
|
124 |
#' xlabel = c("Baseline", "Week 1", "Week 4"), |
|
125 |
#' rotate_xlab = FALSE, |
|
126 |
#' group_stats = "median", |
|
127 |
#' hline_vars = c("ANRHI", "ANRLO"), |
|
128 |
#' hline_vars_colors = c("pink", "brown") |
|
129 |
#' ) |
|
130 |
#' |
|
131 |
#' g_spaghettiplot( |
|
132 |
#' data = ADLB, |
|
133 |
#' subj_id = "USUBJID", |
|
134 |
#' biomarker_var = "PARAMCD", |
|
135 |
#' biomarker = "CRP", |
|
136 |
#' value_var = "AVAL", |
|
137 |
#' trt_group = "ARM", |
|
138 |
#' time = "AVISITCD", |
|
139 |
#' color_manual = color_manual, |
|
140 |
#' color_comb = "#39ff14", |
|
141 |
#' alpha = .02, |
|
142 |
#' xtick = c("BL", "W 1", "W 4"), |
|
143 |
#' xlabel = c("Baseline", "Week 1", "Week 4"), |
|
144 |
#' rotate_xlab = FALSE, |
|
145 |
#' group_stats = "median", |
|
146 |
#' hline_arb = 1.3, |
|
147 |
#' hline_vars = c("ANRHI", "ANRLO", "ULOQN", "LLOQN"), |
|
148 |
#' hline_vars_colors = c("pink", "brown", "purple", "gray") |
|
149 |
#' ) |
|
150 |
#' |
|
151 |
#' g_spaghettiplot( |
|
152 |
#' data = ADLB, |
|
153 |
#' subj_id = "USUBJID", |
|
154 |
#' biomarker_var = "PARAMCD", |
|
155 |
#' biomarker = "CRP", |
|
156 |
#' value_var = "AVAL", |
|
157 |
#' trt_group = "ARM", |
|
158 |
#' time = "AVISITCDN", |
|
159 |
#' color_manual = color_manual, |
|
160 |
#' color_comb = "#39ff14", |
|
161 |
#' alpha = .02, |
|
162 |
#' xtick = c(0, 1, 4), |
|
163 |
#' xlabel = c("Baseline", "Week 1", "Week 4"), |
|
164 |
#' rotate_xlab = FALSE, |
|
165 |
#' group_stats = "median", |
|
166 |
#' hline_arb = c(.5, .7, 1), |
|
167 |
#' hline_arb_color = c("blue", "red", "green"), |
|
168 |
#' hline_arb_label = c("Arb_Hori_line_A", "Arb_Hori_line_B", "Arb_Hori_line_C"), |
|
169 |
#' hline_vars = c("ANRHI", "ANRLO") |
|
170 |
#' ) |
|
171 |
#' |
|
172 |
#' # removing missing levels from the plot with facet_scales |
|
173 |
#' |
|
174 |
#' g_spaghettiplot( |
|
175 |
#' data = ADLB, |
|
176 |
#' subj_id = "USUBJID", |
|
177 |
#' biomarker_var = "PARAMCD", |
|
178 |
#' biomarker = "CRP", |
|
179 |
#' value_var = "AVAL", |
|
180 |
#' trt_group = "ARM", |
|
181 |
#' time = "RACE", |
|
182 |
#' color_manual = color_manual, |
|
183 |
#' color_comb = "#39ff14", |
|
184 |
#' alpha = .02, |
|
185 |
#' facet_scales = "fixed", |
|
186 |
#' rotate_xlab = FALSE, |
|
187 |
#' group_stats = "median", |
|
188 |
#' hline_arb = c(.5, .7, 1), |
|
189 |
#' hline_arb_color = c("blue", "red", "green"), |
|
190 |
#' hline_arb_label = c("Arb_Hori_line_A", "Arb_Hori_line_B", "Arb_Hori_line_C"), |
|
191 |
#' hline_vars = c("ANRHI", "ANRLO") |
|
192 |
#' ) |
|
193 |
#' |
|
194 |
#' g_spaghettiplot( |
|
195 |
#' data = ADLB, |
|
196 |
#' subj_id = "USUBJID", |
|
197 |
#' biomarker_var = "PARAMCD", |
|
198 |
#' biomarker = "CRP", |
|
199 |
#' value_var = "AVAL", |
|
200 |
#' trt_group = "ARM", |
|
201 |
#' time = "RACE", |
|
202 |
#' color_manual = color_manual, |
|
203 |
#' color_comb = "#39ff14", |
|
204 |
#' alpha = .02, |
|
205 |
#' facet_scales = "free_x", |
|
206 |
#' rotate_xlab = FALSE, |
|
207 |
#' group_stats = "median", |
|
208 |
#' hline_arb = c(.5, .7, 1), |
|
209 |
#' hline_arb_color = c("blue", "red", "green"), |
|
210 |
#' hline_arb_label = c("Arb_Hori_line_A", "Arb_Hori_line_B", "Arb_Hori_line_C"), |
|
211 |
#' hline_vars = c("ANRHI", "ANRLO") |
|
212 |
#' ) |
|
213 |
#' |
|
214 |
g_spaghettiplot <- function(data, |
|
215 |
subj_id = "USUBJID", |
|
216 |
biomarker_var = "PARAMCD", |
|
217 |
biomarker_var_label = "PARAM", |
|
218 |
biomarker, |
|
219 |
value_var = "AVAL", |
|
220 |
unit_var = "AVALU", |
|
221 |
trt_group, |
|
222 |
trt_group_level = NULL, |
|
223 |
loq_flag_var = "LOQFL", |
|
224 |
time, |
|
225 |
time_level = NULL, |
|
226 |
color_manual = NULL, |
|
227 |
color_comb = "#39ff14", |
|
228 |
ylim = c(NA, NA), |
|
229 |
alpha = 1.0, |
|
230 |
facet_ncol = 2, |
|
231 |
facet_scales = c("fixed", "free", "free_x", "free_y"), |
|
232 |
xtick = ggplot2::waiver(), |
|
233 |
xlabel = xtick, |
|
234 |
rotate_xlab = FALSE, |
|
235 |
font_size = 12, |
|
236 |
group_stats = "NONE", |
|
237 |
hline_arb = numeric(0), |
|
238 |
hline_arb_color = "red", |
|
239 |
hline_arb_label = "Horizontal line", |
|
240 |
hline_vars = character(0), |
|
241 |
hline_vars_colors = "green", |
|
242 |
hline_vars_labels = hline_vars) { |
|
243 | ! |
checkmate::assert_numeric(ylim, len = 2) |
244 | ! |
facet_scales <- match.arg(facet_scales) |
245 | ||
246 |
## Pre-process data |
|
247 | ! |
label_trt_group <- attr(data[[trt_group]], "label") |
248 | ! |
data[[trt_group]] <- if (!is.null(trt_group_level)) { |
249 | ! |
factor(data[[trt_group]], levels = trt_group_level) |
250 |
} else { |
|
251 | ! |
factor(data[[trt_group]]) |
252 |
} |
|
253 | ! |
attr(data[[trt_group]], "label") <- label_trt_group |
254 | ||
255 | ||
256 | ! |
xtype <- ifelse(is.factor(data[[time]]) | is.character(data[[time]]), "discrete", "continuous") |
257 | ! |
if (xtype == "discrete") { |
258 | ! |
data[[time]] <- if (!is.null(time_level)) { |
259 | ! |
factor(data[[time]], levels = time_level) |
260 |
} else { |
|
261 | ! |
factor(data[[time]]) |
262 |
} |
|
263 |
} |
|
264 | ||
265 |
# Plot |
|
266 | ! |
plot_data <- data %>% |
267 | ! |
filter(!!sym(biomarker_var) %in% biomarker) |
268 | ! |
unit <- plot_data %>% |
269 | ! |
select(!!sym(unit_var)) %>% |
270 | ! |
unique() %>% |
271 | ! |
magrittr::extract2(1) |
272 | ! |
unit1 <- ifelse(is.na(unit) | unit == "", " ", paste0(" (", unit, ") ")) |
273 | ! |
biomarker1 <- plot_data %>% |
274 | ! |
select(!!sym(biomarker_var_label)) %>% |
275 | ! |
unique() %>% |
276 | ! |
magrittr::extract2(1) |
277 | ! |
gtitle <- paste0(biomarker1, unit1, value_var, " Values by Treatment @ Visits") |
278 | ! |
gxlab <- if (is.null(attr(data[[time]], "label"))) time else attr(data[[time]], "label") |
279 | ! |
gylab <- paste0(biomarker1, " ", value_var, " Values") |
280 | ||
281 |
# Setup legend label |
|
282 | ! |
trt_label <- `if`(is.null(attr(data[[trt_group]], "label")), "Dose", attr(data[[trt_group]], "label")) |
283 | ||
284 |
# Add footnote to identify LLOQ and ULOQ values pulled from data |
|
285 | ! |
caption_loqs_label <- h_caption_loqs_label(loqs_data = plot_data, flag_var = loq_flag_var) |
286 | ||
287 | ! |
plot <- ggplot2::ggplot( |
288 | ! |
data = plot_data, |
289 | ! |
ggplot2::aes(x = !!sym(time), y = !!sym(value_var), color = !!sym(trt_group), group = !!sym(subj_id)) |
290 |
) + |
|
291 | ! |
ggplot2::geom_point(size = 0.8, na.rm = TRUE, ggplot2::aes(shape = !!sym(loq_flag_var))) + |
292 | ! |
ggplot2::geom_line(linewidth = 0.4, alpha = alpha, na.rm = TRUE) + |
293 | ! |
ggplot2::facet_wrap(trt_group, ncol = facet_ncol, scales = facet_scales) + |
294 | ! |
ggplot2::labs(caption = caption_loqs_label) + |
295 | ! |
ggplot2::theme_bw() + |
296 | ! |
ggplot2::ggtitle(gtitle) + |
297 | ! |
ggplot2::xlab(gxlab) + |
298 | ! |
ggplot2::ylab(gylab) + |
299 | ! |
ggplot2::theme(plot.title = ggplot2::element_text(size = font_size, margin = ggplot2::margin(), hjust = 0.5)) |
300 | ||
301 |
# Apply y-axis zoom range |
|
302 | ! |
plot <- plot + ggplot2::coord_cartesian(ylim = ylim) |
303 | ||
304 |
# add group statistics |
|
305 |
# can't use stat_summary() because of presenting values for groups with all missings |
|
306 | ! |
if (group_stats != "NONE") { |
307 | ! |
if (group_stats == "MEAN") { |
308 | ! |
plot_data_groupped <- plot_data %>% |
309 | ! |
group_by(!!sym(trt_group), !!sym(time)) %>% |
310 | ! |
transmute(AGG_VAL = mean(!!sym(value_var), na.rm = TRUE)) |
311 | ||
312 | ! |
plot_data_groupped$metric <- "Mean" |
313 |
} else { |
|
314 | ! |
plot_data_groupped <- plot_data %>% |
315 | ! |
group_by(!!sym(trt_group), !!sym(time)) %>% |
316 | ! |
transmute(AGG_VAL = stats::median(!!sym(value_var), na.rm = TRUE)) |
317 | ||
318 | ! |
plot_data_groupped$metric <- "Median" |
319 |
} |
|
320 | ! |
plot <- plot + |
321 | ! |
ggplot2::geom_line( |
322 | ! |
ggplot2::aes(x = !!sym(time), y = .data$AGG_VAL, group = 1, linetype = "metric"), |
323 | ! |
data = plot_data_groupped, |
324 | ! |
lwd = 1, |
325 | ! |
color = color_comb, |
326 | ! |
na.rm = TRUE |
327 |
) + |
|
328 | ! |
ggplot2::guides(linetype = ggplot2::guide_legend("Group statistic", order = 2)) |
329 |
} |
|
330 |
# Format x-label |
|
331 | ! |
if (xtype == "continuous") { |
332 | ! |
plot <- plot + |
333 | ! |
ggplot2::scale_x_continuous(breaks = xtick, labels = xlabel, limits = c(NA, NA)) |
334 | ! |
} else if (xtype == "discrete") { |
335 | ! |
plot <- plot + |
336 | ! |
ggplot2::scale_x_discrete(breaks = xtick, labels = xlabel) |
337 |
} |
|
338 | ! |
if (rotate_xlab) { |
339 | ! |
plot <- plot + |
340 | ! |
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) |
341 |
} |
|
342 |
# Add manual color |
|
343 | ! |
if (!is.null(color_manual)) { |
344 | ! |
plot <- plot + |
345 | ! |
ggplot2::scale_color_manual(values = color_manual, name = trt_label, guide = ggplot2::guide_legend(order = 1)) |
346 |
} else { |
|
347 | ! |
plot + |
348 | ! |
ggplot2::scale_color_discrete(guide = ggplot2::guide_legend(order = 1)) |
349 |
} |
|
350 | ||
351 | ||
352 |
# Format font size |
|
353 | ! |
if (!is.null(font_size)) { |
354 | ! |
plot <- plot + |
355 | ! |
ggplot2::theme( |
356 | ! |
plot.title = ggplot2::element_text(size = font_size, margin = ggplot2::margin()), |
357 | ! |
axis.title.x = ggplot2::element_text(size = font_size), |
358 | ! |
axis.text.x = ggplot2::element_text(size = font_size), |
359 | ! |
axis.title.y = ggplot2::element_text(size = font_size), |
360 | ! |
axis.text.y = ggplot2::element_text(size = font_size), |
361 | ! |
legend.title = ggplot2::element_text(size = font_size), |
362 | ! |
legend.text = ggplot2::element_text(size = font_size), |
363 | ! |
strip.text.x = ggplot2::element_text(size = font_size), |
364 | ! |
strip.text.y = ggplot2::element_text(size = font_size) |
365 |
) |
|
366 |
} |
|
367 |
# Add horizontal line for range based on option |
|
368 | ! |
plot + geom_axes_lines( |
369 | ! |
plot_data, |
370 | ! |
hline_arb = hline_arb, |
371 | ! |
hline_arb_color = hline_arb_color, |
372 | ! |
hline_arb_label = hline_arb_label, |
373 | ! |
hline_vars = hline_vars, |
374 | ! |
hline_vars_colors = hline_vars_colors, |
375 | ! |
hline_vars_labels = hline_vars_labels |
376 |
) |
|
377 |
} |
1 |
#' Function to create a boxplot. |
|
2 |
#' |
|
3 |
#' A box plot is a method for graphically depicting groups of numerical data |
|
4 |
#' through their quartiles. Box plots may also have lines extending vertically |
|
5 |
#' from the boxes (whiskers) indicating variability outside the upper and lower |
|
6 |
#' quartiles, hence the term box-and-whisker. Outliers may be plotted as |
|
7 |
#' individual points. Box plots are non-parametric: they display variation in |
|
8 |
#' samples of a statistical population without making any assumptions of the |
|
9 |
#' underlying statistical distribution. The spacings between the different parts |
|
10 |
#' of the box indicate the degree of dispersion (spread) and skewness in the |
|
11 |
#' data, and show outliers. In addition to the points themselves, they allow one |
|
12 |
#' to visually estimate various L-estimators, notably the interquartile range, |
|
13 |
#' midhinge, range, mid-range, and trimean. |
|
14 |
#' |
|
15 |
#' @param data `ADaM` structured analysis laboratory data frame e.g. `ADLB`. |
|
16 |
#' @param biomarker biomarker to visualize e.g. `IGG`. |
|
17 |
#' @param param_var name of variable containing biomarker codes e.g. `PARAMCD`. |
|
18 |
#' @param yaxis_var name of variable containing biomarker results displayed on |
|
19 |
#' Y-axis e.g. `AVAL`. |
|
20 |
#' @param trt_group name of variable representing treatment `trt_group` e.g. `ARM`. |
|
21 |
#' @param loq_flag_var name of variable containing `LOQ` flag e.g. `LOQFL`. |
|
22 |
#' @param loq_legend `logical` whether to include `LoQ` legend. |
|
23 |
#' @param unit biomarker unit label e.g. (U/L) |
|
24 |
#' @param color_manual vector of color for `trt_group` |
|
25 |
#' @param shape_manual vector of shapes (used with `loq_flag_var`) |
|
26 |
#' @param box add boxes to the plot (boolean) |
|
27 |
#' @param ylim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the y-axis |
|
28 |
#' if the default limits are not suitable. |
|
29 |
#' @param facet_var variable to facet the plot by, or `"None"` if no faceting |
|
30 |
#' required. |
|
31 |
#' @param xaxis_var variable used to group the data on the x-axis. |
|
32 |
#' @param facet_ncol number of facets per row. NULL = Use the default for `ggplot2::facet_wrap` |
|
33 |
#' @param rotate_xlab 45 degree rotation of x-axis label values. |
|
34 |
#' @param font_size point size of text to use. NULL is use default size |
|
35 |
#' @param dot_size plot dot size. |
|
36 |
#' @param alpha dot transparency (0 = transparent, 1 = opaque) |
|
37 |
#' @param hline_arb ('numeric vector') value identifying intercept for arbitrary horizontal lines. |
|
38 |
#' @param hline_arb_color ('character vector') optional, color for the arbitrary horizontal lines. |
|
39 |
#' @param hline_arb_label ('character vector') optional, label for the legend to the arbitrary horizontal lines. |
|
40 |
#' @param hline_vars ('character vector'), names of variables `(ANR*)` or values `(*LOQ)` identifying intercept values. |
|
41 |
#' The data inside of the `ggplot2` object must also contain the columns with these variable names |
|
42 |
#' @param hline_vars_colors ('character vector') colors for the horizontal lines defined by variables. |
|
43 |
#' @param hline_vars_labels ('character vector') labels for the legend to the horizontal lines defined by variables. |
|
44 |
#' |
|
45 |
#' @author Balazs Toth |
|
46 |
#' @author Jeff Tomlinson (tomlinsj) jeffrey.tomlinson@roche.com |
|
47 |
#' |
|
48 |
#' @return \code{ggplot} object |
|
49 |
#' |
|
50 |
#' @export |
|
51 |
#' |
|
52 |
#' @examples |
|
53 |
#' # Example using ADaM structure analysis dataset. |
|
54 |
#' |
|
55 |
#' library(nestcolor) |
|
56 |
#' |
|
57 |
#' ADLB <- rADLB |
|
58 |
#' var_labels <- lapply(ADLB, function(x) attributes(x)$label) |
|
59 |
#' ADLB <- ADLB %>% |
|
60 |
#' mutate(AVISITCD = case_when( |
|
61 |
#' AVISIT == "SCREENING" ~ "SCR", |
|
62 |
#' AVISIT == "BASELINE" ~ "BL", |
|
63 |
#' grepl("WEEK", AVISIT) ~ |
|
64 |
#' paste( |
|
65 |
#' "W", |
|
66 |
#' trimws( |
|
67 |
#' substr( |
|
68 |
#' AVISIT, |
|
69 |
#' start = 6, |
|
70 |
#' stop = stringr::str_locate(AVISIT, "DAY") - 1 |
|
71 |
#' ) |
|
72 |
#' ) |
|
73 |
#' ), |
|
74 |
#' TRUE ~ NA_character_ |
|
75 |
#' )) %>% |
|
76 |
#' mutate(AVISITCDN = case_when( |
|
77 |
#' AVISITCD == "SCR" ~ -2, |
|
78 |
#' AVISITCD == "BL" ~ 0, |
|
79 |
#' grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)), |
|
80 |
#' TRUE ~ NA_real_ |
|
81 |
#' )) %>% |
|
82 |
#' mutate(ANRLO = .5, ANRHI = 1) %>% |
|
83 |
#' rowwise() %>% |
|
84 |
#' group_by(PARAMCD) %>% |
|
85 |
#' mutate(LBSTRESC = ifelse( |
|
86 |
#' USUBJID %in% sample(USUBJID, 1, replace = TRUE), |
|
87 |
#' paste("<", round(runif(1, min = .5, max = 1))), LBSTRESC |
|
88 |
#' )) %>% |
|
89 |
#' mutate(LBSTRESC = ifelse( |
|
90 |
#' USUBJID %in% sample(USUBJID, 1, replace = TRUE), |
|
91 |
#' paste(">", round(runif(1, min = 1, max = 1.5))), LBSTRESC |
|
92 |
#' )) %>% |
|
93 |
#' ungroup() |
|
94 |
#' attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]] |
|
95 |
#' attr(ADLB[["ANRLO"]], "label") <- "Analysis Normal Range Lower Limit" |
|
96 |
#' attr(ADLB[["ANRHI"]], "label") <- "Analysis Normal Range Upper Limit" |
|
97 |
#' |
|
98 |
#' # add LLOQ and ULOQ variables |
|
99 |
#' ADLB_LOQS <- goshawk:::h_identify_loq_values(ADLB, "LOQFL") |
|
100 |
#' ADLB <- left_join(ADLB, ADLB_LOQS, by = "PARAM") |
|
101 |
#' |
|
102 |
#' g_boxplot(ADLB, |
|
103 |
#' biomarker = "CRP", |
|
104 |
#' param_var = "PARAMCD", |
|
105 |
#' yaxis_var = "AVAL", |
|
106 |
#' trt_group = "ARM", |
|
107 |
#' loq_flag_var = "LOQFL", |
|
108 |
#' loq_legend = FALSE, |
|
109 |
#' unit = "AVALU", |
|
110 |
#' shape_manual = c("N" = 1, "Y" = 2, "NA" = NULL), |
|
111 |
#' facet_var = "AVISIT", |
|
112 |
#' xaxis_var = "STUDYID", |
|
113 |
#' alpha = 0.5, |
|
114 |
#' rotate_xlab = TRUE, |
|
115 |
#' hline_arb = c(.9, 1.2), |
|
116 |
#' hline_arb_color = "blue", |
|
117 |
#' hline_arb_label = "Hori_line_label", |
|
118 |
#' hline_vars = c("ANRHI", "ANRLO", "ULOQN", "LLOQN"), |
|
119 |
#' hline_vars_colors = c("pink", "brown", "purple", "gray"), |
|
120 |
#' hline_vars_labels = c("A", "B", "C", "D") |
|
121 |
#' ) |
|
122 |
g_boxplot <- function(data, |
|
123 |
biomarker, |
|
124 |
param_var = "PARAMCD", |
|
125 |
yaxis_var, |
|
126 |
trt_group, |
|
127 |
xaxis_var = NULL, |
|
128 |
loq_flag_var = "LOQFL", |
|
129 |
loq_legend = TRUE, |
|
130 |
unit = NULL, |
|
131 |
color_manual = NULL, |
|
132 |
shape_manual = NULL, |
|
133 |
box = TRUE, |
|
134 |
ylim = c(NA, NA), |
|
135 |
dot_size = 2, |
|
136 |
alpha = 1.0, |
|
137 |
facet_ncol = NULL, |
|
138 |
rotate_xlab = FALSE, |
|
139 |
font_size = NULL, |
|
140 |
facet_var = NULL, |
|
141 |
hline_arb = numeric(0), |
|
142 |
hline_arb_color = "red", |
|
143 |
hline_arb_label = "Horizontal line", |
|
144 |
hline_vars = character(0), |
|
145 |
hline_vars_colors = "green", |
|
146 |
hline_vars_labels = hline_vars) { |
|
147 | ! |
if (is.null(data[[param_var]])) { |
148 | ! |
stop(paste("param_var", param_var, "is not in data.")) |
149 |
} |
|
150 | ||
151 | ! |
if (!any(data[[param_var]] == biomarker)) { |
152 | ! |
stop(paste("biomarker", biomarker, "is not found in param_var", param_var, ".")) |
153 |
} |
|
154 | ! |
checkmate::assert_flag(loq_legend) |
155 | ! |
checkmate::assert_number(dot_size) |
156 | ! |
checkmate::assert_numeric(ylim, len = 2) |
157 | ||
158 |
# filter input data |
|
159 | ! |
data <- data %>% |
160 | ! |
filter(!!sym(param_var) == biomarker) |
161 | ||
162 | ! |
if (!is.null(unit)) { |
163 |
# check unit is in the dataset |
|
164 | ! |
if (is.null(data[[unit]])) { |
165 | ! |
stop(paste("unit variable", unit, "is not in data.")) |
166 |
} |
|
167 |
# extract the most common unit |
|
168 |
# if there are ties, take the use alphabetic order |
|
169 | ! |
tmp_unit <- data %>% |
170 | ! |
count(!!sym(unit)) %>% |
171 | ! |
top_n(1, n) %>% |
172 | ! |
arrange(!!sym(unit)) %>% |
173 | ! |
slice(1) %>% |
174 | ! |
select(!!sym(unit)) %>% |
175 | ! |
as.character() |
176 | ! |
if (is.factor(data[[unit]])) { |
177 | ! |
unit <- levels(data[[unit]])[as.numeric(tmp_unit)] |
178 |
} else { |
|
179 | ! |
unit <- tmp_unit |
180 |
} |
|
181 |
} |
|
182 |
# Setup the Y axis label. Combine the biomarker and the units (if available) |
|
183 | ! |
y_axis_label <- ifelse(is.null(unit), paste(data$PARAM[1], yaxis_var, "Values"), |
184 | ! |
ifelse(unit == "", paste(data$PARAM[1], yaxis_var, "Values"), |
185 | ! |
paste0(data$PARAM[1], " (", unit, ") ", yaxis_var, " Values") |
186 |
) |
|
187 |
) |
|
188 |
# Setup the ggtitle label. Combine the biomarker and the units (if available) |
|
189 | ! |
ggtitle_label <- ifelse(is.null(unit), paste(data$PARAM[1], "Distribution by Treatment @ Visits"), |
190 | ! |
ifelse(unit == "", paste(data$PARAM[1], "Distribution by Treatment @ Visits"), |
191 | ! |
paste0(data$PARAM[1], " (", unit, ") Distribution by Treatment @ Visits") |
192 |
) |
|
193 |
) |
|
194 |
# Setup legend label |
|
195 | ! |
trt_label <- `if`(is.null(attr(data[[trt_group]], "label")), "Dose", attr(data[[trt_group]], "label")) |
196 | ||
197 |
# add footnote to identify LLOQ and ULOQ values pulled from data |
|
198 | ! |
caption_loqs_label <- h_caption_loqs_label(loqs_data = data, flag_var = loq_flag_var) |
199 |
# Base plot |
|
200 | ! |
plot1 <- ggplot2::ggplot(data) |
201 |
# Add boxes if required |
|
202 | ! |
if (box) { |
203 | ! |
plot1 <- plot1 + |
204 | ! |
ggplot2::geom_boxplot( |
205 | ! |
data = data, |
206 | ! |
ggplot2::aes( |
207 | ! |
x = !!sym(xaxis_var), |
208 | ! |
y = !!sym(yaxis_var), |
209 | ! |
fill = NULL |
210 |
), |
|
211 | ! |
outlier.shape = NA, |
212 | ! |
na.rm = TRUE |
213 |
) |
|
214 |
} |
|
215 |
# Extend is.infinite to include zero length objects. |
|
216 | ! |
is_finite <- function(x) { |
217 | ! |
if (length(x) == 0) { |
218 | ! |
return(FALSE) |
219 |
} |
|
220 | ! |
return(is.finite(x)) |
221 |
} |
|
222 | ||
223 | ! |
plot1 <- plot1 + |
224 | ! |
ggplot2::labs(color = trt_label, x = NULL, y = y_axis_label, caption = caption_loqs_label) + |
225 | ! |
ggplot2::theme_bw() + |
226 | ! |
ggplot2::ggtitle(ggtitle_label) + |
227 | ! |
ggplot2::theme(plot.title = ggplot2::element_text(size = font_size, hjust = 0.5)) |
228 |
# Colors supplied? Use color_manual, otherwise default ggplot coloring. |
|
229 | ! |
plot1 <- if (!is.null(color_manual)) { |
230 | ! |
plot1 + |
231 | ! |
ggplot2::scale_color_manual(values = color_manual, guide = ggplot2::guide_legend(order = 1)) |
232 |
} else { |
|
233 | ! |
plot1 + |
234 | ! |
ggplot2::scale_color_discrete(guide = ggplot2::guide_legend(order = 1)) |
235 |
} |
|
236 | ||
237 |
# Format LOQ flag symbol shape |
|
238 | ! |
if (is.null(shape_manual)) { |
239 | ! |
shape_names <- unique(data[!is.na(data[[loq_flag_var]]), ][[loq_flag_var]]) |
240 | ! |
shape_manual <- seq_along(shape_names) |
241 | ! |
names(shape_manual) <- shape_names |
242 |
} |
|
243 |
# add LOQ legend conditionally |
|
244 | ! |
plot1 <- if (!loq_legend) { |
245 | ! |
plot1 + ggplot2::scale_shape_manual(values = shape_manual, name = "LoQ", guide = "none") |
246 |
} else { |
|
247 | ! |
plot1 + ggplot2::scale_shape_manual(values = shape_manual, name = "LoQ", guide = ggplot2::guide_legend(order = 2)) |
248 |
} |
|
249 | ||
250 | ! |
plot1 <- plot1 + |
251 | ! |
ggplot2::geom_jitter( |
252 | ! |
data = data, |
253 | ! |
ggplot2::aes( |
254 | ! |
x = !!sym(xaxis_var), |
255 | ! |
y = !!sym(yaxis_var), |
256 | ! |
shape = !!sym(loq_flag_var), |
257 | ! |
color = !!sym(trt_group) |
258 |
), |
|
259 | ! |
alpha = alpha, |
260 | ! |
position = ggplot2::position_jitter(width = 0.1, height = 0), |
261 | ! |
size = dot_size, |
262 | ! |
na.rm = TRUE |
263 |
) |
|
264 | ||
265 |
# Any limits for the Y axis? |
|
266 | ! |
plot1 <- plot1 + ggplot2::coord_cartesian(ylim = ylim) |
267 | ||
268 | ||
269 |
# Add facetting. |
|
270 | ! |
if (!is.null(facet_var)) { |
271 | ! |
if (facet_var != "None" & facet_var %in% names(data)) { # nolint |
272 | ! |
if (!is_finite(facet_ncol)) facet_ncol <- 0 |
273 | ! |
if (facet_ncol >= 1) { |
274 | ! |
plot1 <- plot1 + |
275 | ! |
ggplot2::facet_wrap(stats::as.formula(paste0(" ~ ", facet_var)), ncol = round(facet_ncol)) |
276 |
} else { |
|
277 | ! |
plot1 <- plot1 + |
278 | ! |
ggplot2::facet_wrap(stats::as.formula(paste0(" ~ ", facet_var))) |
279 |
} |
|
280 |
} |
|
281 |
} |
|
282 | ||
283 | ||
284 | ||
285 | ||
286 |
# Format font size |
|
287 | ! |
if (is_finite(font_size)) { |
288 | ! |
plot1 <- plot1 + |
289 | ! |
ggplot2::theme( |
290 | ! |
axis.title.x = ggplot2::element_text(size = font_size), |
291 | ! |
axis.text.x = ggplot2::element_text(size = font_size), |
292 | ! |
axis.title.y = ggplot2::element_text(size = font_size), |
293 | ! |
axis.text.y = ggplot2::element_text(size = font_size), |
294 | ! |
legend.title = ggplot2::element_text(size = font_size), |
295 | ! |
legend.text = ggplot2::element_text(size = font_size), |
296 | ! |
strip.text.x = ggplot2::element_text(size = font_size), |
297 | ! |
strip.text.y = ggplot2::element_text(size = font_size) |
298 |
) |
|
299 |
} |
|
300 |
# Format x-label |
|
301 | ! |
if (rotate_xlab) { |
302 | ! |
plot1 <- plot1 + |
303 | ! |
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) |
304 |
} |
|
305 | ||
306 |
# Add horizontal line for range based on option |
|
307 | ! |
plot1 + geom_axes_lines(data, |
308 | ! |
hline_arb = hline_arb, hline_arb_color = hline_arb_color, hline_arb_label = hline_arb_label, |
309 | ! |
hline_vars = hline_vars, hline_vars_colors = hline_vars_colors, hline_vars_labels = hline_vars_labels |
310 |
) |
|
311 |
} |
1 |
#' Function to create a correlation plot. |
|
2 |
#' |
|
3 |
#' Default plot displays correlation facetted by visit with color attributed treatment arms and |
|
4 |
#' symbol attributed `LOQ` values. |
|
5 |
#' |
|
6 |
#' @param label text string to used to identify plot. |
|
7 |
#' @param data `ADaM` structured analysis laboratory data frame e.g. `ADLB`. |
|
8 |
#' @param param_var name of variable containing biomarker codes e.g. `PARAMCD`. |
|
9 |
#' @param xaxis_param x-axis biomarker to visualize e.g. `IGG`. |
|
10 |
#' @param xaxis_var name of variable containing biomarker results displayed on X-axis e.g. `BASE`. |
|
11 |
#' @param xvar x-axis analysis variable from transposed data set. |
|
12 |
#' @param yaxis_param y-axis biomarker to visualize e.g. `IGG`. |
|
13 |
#' @param yaxis_var name of variable containing biomarker results displayed on Y-axis.g. `AVAL`. |
|
14 |
#' @param yvar y-axis analysis variable from transposed data set. |
|
15 |
#' @param trt_group name of variable representing treatment group e.g. `ARM`. |
|
16 |
#' @param visit name of variable containing nominal visits e.g. `AVISITCD`. |
|
17 |
#' @param loq_flag_var name of variable containing `LOQ` flag e.g. `LOQFL_COMB`. |
|
18 |
#' @param visit_facet visit facet toggle. |
|
19 |
#' @param loq_legend `logical` whether to include `LoQ` legend. |
|
20 |
#' @param unit name of variable containing biomarker unit e.g. `AVALU`. |
|
21 |
#' @param xlim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the x-axis |
|
22 |
#' if the default limits are not suitable. |
|
23 |
#' @param ylim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the y-axis |
|
24 |
#' if the default limits are not suitable. |
|
25 |
#' @param title_text plot title. |
|
26 |
#' @param xaxis_lab x-axis label. |
|
27 |
#' @param yaxis_lab y-axis label. |
|
28 |
#' @param color_manual vector of colors applied to treatment values. |
|
29 |
#' @param shape_manual vector of symbols applied to `LOQ` values. (used with `loq_flag_var`). |
|
30 |
#' @param facet_ncol number of facets per row. |
|
31 |
#' @param facet set layout to use treatment facetting. |
|
32 |
#' @param facet_var variable to use for treatment facetting. |
|
33 |
#' @param reg_line include regression line and annotations for slope and coefficient. |
|
34 |
#' Use with facet = TRUE. |
|
35 |
#' @param hline_arb ('numeric vector') value identifying intercept for arbitrary horizontal lines. |
|
36 |
#' @param hline_arb_color ('character vector') optional, color for the arbitrary horizontal lines. |
|
37 |
#' @param hline_arb_label ('character vector') optional, label for the legend to the arbitrary horizontal lines. |
|
38 |
#' @param hline_vars ('character vector'), names of variables `(ANR*)` or values `(*LOQ)` identifying intercept values. |
|
39 |
#' The data inside of the `ggplot2` object must also contain the columns with these variable names |
|
40 |
#' @param hline_vars_colors ('character vector') colors for the horizontal lines defined by variables. |
|
41 |
#' @param hline_vars_labels ('character vector') labels for the legend to the horizontal lines defined by variables. |
|
42 |
#' @param vline_arb ('numeric vector') value identifying intercept for arbitrary vertical lines. |
|
43 |
#' @param vline_arb_color ('character vector') optional, color for the arbitrary vertical lines. |
|
44 |
#' @param vline_arb_label ('character vector') optional, label for the legend to the arbitrary vertical lines. |
|
45 |
#' @param vline_vars ('character vector'), names of variables `(ANR*)` or values `(*LOQ)` identifying intercept values. |
|
46 |
#' The data inside of the `ggplot2` object must also contain the columns with these variable names |
|
47 |
#' @param vline_vars_colors ('character vector') colors for the vertical lines defined by variables. |
|
48 |
#' @param vline_vars_labels ('character vector') labels for the legend to the vertical lines defined by variables. |
|
49 |
#' @param rotate_xlab 45 degree rotation of x-axis label values. |
|
50 |
#' @param font_size font size control for title, x-axis label, y-axis label and legend. |
|
51 |
#' @param dot_size plot dot size. |
|
52 |
#' @param reg_text_size font size control for regression line annotations. |
|
53 |
#' |
|
54 |
#' @author Nick Paszty (npaszty) paszty.nicholas@gene.com |
|
55 |
#' @author Balazs Toth (tothb2) toth.balazs@gene.com |
|
56 |
#' |
|
57 |
#' @details Regression uses `deming` model. |
|
58 |
#' |
|
59 |
#' @export |
|
60 |
#' |
|
61 |
#' @examples |
|
62 |
#' # Example using ADaM structure analysis dataset. |
|
63 |
#' |
|
64 |
#' library(stringr) |
|
65 |
#' library(tidyr) |
|
66 |
#' |
|
67 |
#' # original ARM value = dose value |
|
68 |
#' arm_mapping <- list( |
|
69 |
#' "A: Drug X" = "150mg QD", |
|
70 |
#' "B: Placebo" = "Placebo", |
|
71 |
#' "C: Combination" = "Combination" |
|
72 |
#' ) |
|
73 |
#' color_manual <- c("150mg QD" = "#000000", "Placebo" = "#3498DB", "Combination" = "#E74C3C") |
|
74 |
#' # assign LOQ flag symbols: circles for "N" and triangles for "Y", squares for "NA" |
|
75 |
#' shape_manual <- c("N" = 1, "Y" = 2, "NA" = 0) |
|
76 |
#' |
|
77 |
#' ADLB <- rADLB |
|
78 |
#' var_labels <- lapply(ADLB, function(x) attributes(x)$label) |
|
79 |
#' ADLB <- ADLB %>% |
|
80 |
#' mutate(AVISITCD = case_when( |
|
81 |
#' AVISIT == "SCREENING" ~ "SCR", |
|
82 |
#' AVISIT == "BASELINE" ~ "BL", |
|
83 |
#' grepl("WEEK", AVISIT) ~ |
|
84 |
#' paste( |
|
85 |
#' "W", |
|
86 |
#' trimws( |
|
87 |
#' substr( |
|
88 |
#' AVISIT, |
|
89 |
#' start = 6, |
|
90 |
#' stop = str_locate(AVISIT, "DAY") - 1 |
|
91 |
#' ) |
|
92 |
#' ) |
|
93 |
#' ), |
|
94 |
#' TRUE ~ NA_character_ |
|
95 |
#' )) %>% |
|
96 |
#' mutate(AVISITCDN = case_when( |
|
97 |
#' AVISITCD == "SCR" ~ -2, |
|
98 |
#' AVISITCD == "BL" ~ 0, |
|
99 |
#' grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)), |
|
100 |
#' TRUE ~ NA_real_ |
|
101 |
#' )) %>% |
|
102 |
#' # use ARMCD values to order treatment in visualization legend |
|
103 |
#' mutate(TRTORD = ifelse(grepl("C", ARMCD), 1, |
|
104 |
#' ifelse(grepl("B", ARMCD), 2, |
|
105 |
#' ifelse(grepl("A", ARMCD), 3, NA) |
|
106 |
#' ) |
|
107 |
#' )) %>% |
|
108 |
#' mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>% |
|
109 |
#' mutate(ARM = factor(ARM) %>% |
|
110 |
#' reorder(TRTORD)) %>% |
|
111 |
#' mutate( |
|
112 |
#' ANRHI = case_when( |
|
113 |
#' PARAMCD == "ALT" ~ 60, |
|
114 |
#' PARAMCD == "CRP" ~ 70, |
|
115 |
#' PARAMCD == "IGA" ~ 80, |
|
116 |
#' TRUE ~ NA_real_ |
|
117 |
#' ), |
|
118 |
#' ANRLO = case_when( |
|
119 |
#' PARAMCD == "ALT" ~ 20, |
|
120 |
#' PARAMCD == "CRP" ~ 30, |
|
121 |
#' PARAMCD == "IGA" ~ 40, |
|
122 |
#' TRUE ~ NA_real_ |
|
123 |
#' ) |
|
124 |
#' ) %>% |
|
125 |
#' rowwise() %>% |
|
126 |
#' group_by(PARAMCD) %>% |
|
127 |
#' mutate(LBSTRESC = ifelse( |
|
128 |
#' USUBJID %in% sample(USUBJID, 1, replace = TRUE), |
|
129 |
#' paste("<", round(runif(1, min = 25, max = 30))), LBSTRESC |
|
130 |
#' )) %>% |
|
131 |
#' mutate(LBSTRESC = ifelse( |
|
132 |
#' USUBJID %in% sample(USUBJID, 1, replace = TRUE), |
|
133 |
#' paste(">", round(runif(1, min = 70, max = 75))), LBSTRESC |
|
134 |
#' )) %>% |
|
135 |
#' ungroup() |
|
136 |
#' attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]] |
|
137 |
#' attr(ADLB[["ANRHI"]], "label") <- "Analysis Normal Range Upper Limit" |
|
138 |
#' attr(ADLB[["ANRLO"]], "label") <- "Analysis Normal Range Lower Limit" |
|
139 |
#' |
|
140 |
#' # add LLOQ and ULOQ variables |
|
141 |
#' ADLB_LOQS <- goshawk:::h_identify_loq_values(ADLB, flag_var = "LOQFL") |
|
142 |
#' ADLB <- left_join(ADLB, ADLB_LOQS, by = "PARAM") |
|
143 |
#' |
|
144 |
#' # given the 2 param and 2 analysis vars we need to transform the data |
|
145 |
#' plot_data_t1 <- ADLB %>% |
|
146 |
#' gather( |
|
147 |
#' ANLVARS, ANLVALS, PARAM, LBSTRESC, BASE2, BASE, AVAL, BASE, LOQFL, |
|
148 |
#' ANRHI, ANRLO, ULOQN, LLOQN |
|
149 |
#' ) %>% |
|
150 |
#' mutate(ANL.PARAM = ifelse(ANLVARS %in% c("PARAM", "LBSTRESC", "LOQFL"), |
|
151 |
#' paste0(ANLVARS, "_", PARAMCD), |
|
152 |
#' paste0(ANLVARS, ".", PARAMCD) |
|
153 |
#' )) %>% |
|
154 |
#' select(USUBJID, ARM, ARMCD, AVISITN, AVISITCD, ANL.PARAM, ANLVALS) %>% |
|
155 |
#' spread(ANL.PARAM, ANLVALS) |
|
156 |
#' |
|
157 |
#' # the transformed analysis value variables are character and need to be converted to numeric for |
|
158 |
#' # ggplot |
|
159 |
#' # remove records where either of the analysis variables are NA since they will not appear on the |
|
160 |
#' # plot and will ensure that LOQFL = NA level is removed |
|
161 |
#' plot_data_t2 <- plot_data_t1 %>% |
|
162 |
#' filter(!is.na(BASE.CRP) & !is.na(AVAL.ALT)) %>% |
|
163 |
#' mutate_at(vars(contains(".")), as.numeric) %>% |
|
164 |
#' mutate( |
|
165 |
#' LOQFL_COMB = ifelse(LOQFL_CRP == "Y" | LOQFL_ALT == "Y", "Y", "N") |
|
166 |
#' ) |
|
167 |
#' |
|
168 |
#' g_correlationplot( |
|
169 |
#' label = "Correlation Plot", |
|
170 |
#' data = plot_data_t2, |
|
171 |
#' param_var = "PARAMCD", |
|
172 |
#' xaxis_param = c("CRP"), |
|
173 |
#' xaxis_var = "AVAL", |
|
174 |
#' xvar = "AVAL.CRP", |
|
175 |
#' yaxis_param = c("ALT"), |
|
176 |
#' yaxis_var = "BASE", |
|
177 |
#' yvar = "BASE.ALT", |
|
178 |
#' trt_group = "ARM", |
|
179 |
#' visit = "AVISITCD", |
|
180 |
#' visit_facet = TRUE, |
|
181 |
#' loq_legend = TRUE, |
|
182 |
#' unit = "AVALU", |
|
183 |
#' title_text = "Correlation of ALT to CRP", |
|
184 |
#' xaxis_lab = "CRP", |
|
185 |
#' yaxis_lab = "ALT", |
|
186 |
#' color_manual = color_manual, |
|
187 |
#' shape_manual = shape_manual, |
|
188 |
#' facet_ncol = 4, |
|
189 |
#' facet = FALSE, |
|
190 |
#' facet_var = "ARM", |
|
191 |
#' reg_line = FALSE, |
|
192 |
#' hline_arb = c(15, 25), |
|
193 |
#' hline_arb_color = c("gray", "green"), |
|
194 |
#' hline_arb_label = "Hori_line_label", |
|
195 |
#' vline_arb = c(.5, 1), |
|
196 |
#' vline_arb_color = c("red", "black"), |
|
197 |
#' vline_arb_label = c("Vertical Line A", "Vertical Line B"), |
|
198 |
#' hline_vars = c("ANRHI.ALT", "ANRLO.ALT", "ULOQN.ALT", "LLOQN.ALT"), |
|
199 |
#' hline_vars_colors = c("green", "blue", "purple", "cyan"), |
|
200 |
#' hline_vars_labels = c("ANRHI ALT Label", "ANRLO ALT Label", "ULOQN ALT Label", "LLOQN ALT Label"), |
|
201 |
#' vline_vars = c("ANRHI.CRP", "ANRLO.CRP", "ULOQN.CRP", "LLOQN.CRP"), |
|
202 |
#' vline_vars_colors = c("yellow", "orange", "brown", "gold"), |
|
203 |
#' vline_vars_labels = c("ANRHI CRP Label", "ANRLO CRP Label", "ULOQN CRP Label", "LLOQN CRP Label"), |
|
204 |
#' rotate_xlab = FALSE, |
|
205 |
#' font_size = 14, |
|
206 |
#' dot_size = 2, |
|
207 |
#' reg_text_size = 3 |
|
208 |
#' ) |
|
209 |
g_correlationplot <- function(label = "Correlation Plot", |
|
210 |
data, |
|
211 |
param_var = "PARAMCD", |
|
212 |
xaxis_param = "CRP", |
|
213 |
xaxis_var = "BASE", |
|
214 |
xvar, |
|
215 |
yaxis_param = "IGG", |
|
216 |
yaxis_var = "AVAL", |
|
217 |
yvar, |
|
218 |
trt_group = "ARM", |
|
219 |
visit = "AVISITCD", |
|
220 |
loq_flag_var = "LOQFL_COMB", |
|
221 |
visit_facet = TRUE, |
|
222 |
loq_legend = TRUE, |
|
223 |
unit = "AVALU", |
|
224 |
xlim = c(NA, NA), |
|
225 |
ylim = c(NA, NA), |
|
226 |
title_text = title_text, |
|
227 |
xaxis_lab = xaxis_lab, |
|
228 |
yaxis_lab = yaxis_lab, |
|
229 |
color_manual = NULL, |
|
230 |
shape_manual = NULL, |
|
231 |
facet_ncol = 2, |
|
232 |
facet = FALSE, |
|
233 |
facet_var = "ARM", |
|
234 |
reg_line = FALSE, |
|
235 |
hline_arb = numeric(0), |
|
236 |
hline_arb_color = "red", |
|
237 |
hline_arb_label = "Horizontal line", |
|
238 |
hline_vars = character(0), |
|
239 |
hline_vars_colors = "green", |
|
240 |
hline_vars_labels = hline_vars, |
|
241 |
vline_arb = numeric(0), |
|
242 |
vline_arb_color = "red", |
|
243 |
vline_arb_label = "Vertical line", |
|
244 |
vline_vars = character(0), |
|
245 |
vline_vars_colors = "green", |
|
246 |
vline_vars_labels = vline_vars, |
|
247 |
rotate_xlab = FALSE, |
|
248 |
font_size = 12, |
|
249 |
dot_size = 2, |
|
250 |
reg_text_size = 3) { |
|
251 | ! |
checkmate::assert_flag(loq_legend) |
252 | ! |
checkmate::assert_number(dot_size, lower = 1) |
253 | ! |
checkmate::assert_numeric(xlim, len = 2) |
254 | ! |
checkmate::assert_numeric(ylim, len = 2) |
255 | ||
256 |
# create correlation plot over time pairwise per treatment arm |
|
257 | ! |
plot_data <- data |
258 | ||
259 |
# identify param and lbstresc combinations in transposed data variable name |
|
260 | ! |
t_param_var_x <- paste("PARAM", xaxis_param, sep = "_") |
261 | ! |
t_lbstresc_var_x <- paste("LBSTRESC", xaxis_param, sep = "_") |
262 | ! |
t_param_var_y <- paste("PARAM", yaxis_param, sep = "_") |
263 | ! |
t_lbstresc_var_y <- paste("LBSTRESC", yaxis_param, sep = "_") |
264 | ||
265 | ! |
xaxis_param_loqs_data <- data %>% |
266 | ! |
mutate( |
267 | ! |
PARAM = !!sym(t_param_var_x), LBSTRESC = !!sym(t_lbstresc_var_x), |
268 | ! |
sym(loq_flag_var) == !!sym(loq_flag_var) |
269 |
) %>% |
|
270 | ! |
select("PARAM", "LBSTRESC", loq_flag_var) |
271 | ||
272 | ! |
yaxis_param_loqs_data <- data %>% |
273 | ! |
mutate( |
274 | ! |
PARAM = !!sym(t_param_var_y), LBSTRESC = !!sym(t_lbstresc_var_y), |
275 | ! |
sym(loq_flag_var) == !!sym(loq_flag_var) |
276 |
) %>% |
|
277 | ! |
select("PARAM", "LBSTRESC", loq_flag_var) |
278 | ||
279 |
# add footnote to identify xaxis assay LLOQ and ULOQ values pulled from data |
|
280 | ! |
caption_loqs_label_x <- h_caption_loqs_label(loqs_data = xaxis_param_loqs_data, flag_var = loq_flag_var) |
281 | ! |
caption_loqs_label_y <- h_caption_loqs_label(loqs_data = yaxis_param_loqs_data, flag_var = loq_flag_var) |
282 | ! |
caption_loqs_label_x_y <- paste0(union(caption_loqs_label_x, caption_loqs_label_y), collapse = "\n") |
283 | ||
284 |
# Setup legend label |
|
285 | ! |
trt_label <- `if`(is.null(attr(data[[trt_group]], "label")), "Dose", attr(data[[trt_group]], "label")) |
286 | ||
287 |
# create plot foundation - titles and axes labels are defined in |
|
288 |
# teal.goshawk.tm_g_correlationplot.R |
|
289 | ! |
plot1 <- ggplot2::ggplot( |
290 | ! |
data = plot_data, |
291 | ! |
ggplot2::aes( |
292 | ! |
x = !!sym(xvar), |
293 | ! |
y = !!sym(yvar), |
294 | ! |
color = !!sym(trt_group) |
295 |
) |
|
296 |
) + |
|
297 | ! |
ggplot2::coord_cartesian(xlim = xlim, ylim = ylim) + |
298 | ! |
ggplot2::theme_bw() + |
299 | ! |
ggplot2::labs(caption = caption_loqs_label_x_y) + |
300 | ! |
ggplot2::ggtitle(title_text) + |
301 | ! |
ggplot2::theme(plot.title = ggplot2::element_text(size = font_size, hjust = 0.5)) + |
302 | ! |
ggplot2::xlab(xaxis_lab) + |
303 | ! |
ggplot2::ylab(yaxis_lab) |
304 | ||
305 |
# conditionally facet |
|
306 | ! |
plot1 <- if (visit_facet && facet) { |
307 | ! |
plot1 + |
308 | ! |
ggplot2::facet_grid(stats::as.formula(paste0(facet_var, " ~ ", visit))) |
309 | ! |
} else if (visit_facet) { |
310 | ! |
plot1 + |
311 | ! |
ggplot2::facet_wrap(stats::as.formula(paste0(" ~ ", visit)), ncol = facet_ncol) |
312 | ! |
} else if (facet) { |
313 | ! |
plot1 + |
314 | ! |
ggplot2::facet_wrap(stats::as.formula(paste0(" ~ ", facet_var)), ncol = facet_ncol) |
315 |
} else { |
|
316 | ! |
plot1 |
317 |
} |
|
318 | ||
319 | ! |
plot1 <- plot1 + |
320 | ! |
ggplot2::geom_point(ggplot2::aes(shape = !!sym(loq_flag_var)), size = dot_size, na.rm = TRUE) |
321 | ||
322 |
# add regression line |
|
323 | ! |
if (reg_line) { |
324 | ! |
slope <- function(x, y) { |
325 | ! |
ratio <- stats::sd(x) / stats::sd(y) |
326 | ! |
if (!is.na(ratio) && ratio > 0) { |
327 | ! |
reg <- mc.deming(y, x, ratio) |
328 |
# return the evaluation of the ratio condition as third value in numeric vector to control |
|
329 |
# downstream processing |
|
330 | ! |
return(c(round(reg$b0, 2), round(reg$b1, 2), !is.na(ratio) & ratio > 0)) |
331 |
} |
|
332 |
# if ratio condition is not met then assign NA to vector so that NULL condition does not throw |
|
333 |
# the error below |
|
334 | ! |
return(as.numeric(c(NA, NA, NA))) |
335 |
} |
|
336 | ||
337 | ! |
sub_data <- filter(plot_data, !is.na(!!sym(yvar)) & !is.na(!!sym(xvar))) %>% |
338 | ! |
group_by(!!sym(trt_group), !!sym(visit)) %>% |
339 | ! |
mutate(intercept = slope(!!sym(yvar), !!sym(xvar))[1]) %>% |
340 | ! |
mutate(slope = slope(!!sym(yvar), !!sym(xvar))[2]) %>% |
341 | ! |
mutate(corr = ifelse( |
342 | ! |
slope(!!sym(yvar), !!sym(xvar))[3], |
343 | ! |
stats::cor(!!sym(yvar), !!sym(xvar), method = "spearman", use = "complete.obs"), |
344 | ! |
NA |
345 |
)) |
|
346 | ! |
plot1 <- plot1 + |
347 | ! |
ggplot2::geom_abline( |
348 | ! |
data = filter(sub_data, row_number() == 1), # only need to return 1 row within group_by |
349 | ! |
ggplot2::aes(intercept = .data$intercept, slope = .data$slope, color = !!sym(trt_group)) |
350 |
) + |
|
351 | ! |
ggplot2::geom_text( |
352 | ! |
data = filter(sub_data, row_number() == 1), |
353 | ! |
ggplot2::aes_( |
354 | ! |
x = -Inf, |
355 | ! |
y = Inf, |
356 | ! |
hjust = 0, |
357 | ! |
vjust = 1, |
358 | ! |
label = ~ ifelse( |
359 | ! |
!is.na(intercept) & !is.na(slope) & !is.na(corr), |
360 | ! |
sprintf("y = %.2f+%.2fX\ncor = %.2f", intercept, slope, corr), |
361 | ! |
paste0("Insufficient Data For Regression") |
362 |
), |
|
363 | ! |
color = sym(trt_group) |
364 |
), |
|
365 | ! |
size = reg_text_size, |
366 | ! |
show.legend = FALSE |
367 |
) + |
|
368 | ! |
ggplot2::labs(caption = paste0( |
369 | ! |
"Deming Regression Model, Spearman Correlation Method.\n", |
370 | ! |
caption_loqs_label_x_y |
371 |
)) |
|
372 |
} |
|
373 |
# Format font size |
|
374 | ! |
if (!is.null(font_size)) { |
375 | ! |
plot1 <- plot1 + ggplot2::theme( |
376 | ! |
axis.title.x = ggplot2::element_text(size = font_size), |
377 | ! |
axis.text.x = ggplot2::element_text(size = font_size), |
378 | ! |
axis.title.y = ggplot2::element_text(size = font_size), |
379 | ! |
axis.text.y = ggplot2::element_text(size = font_size), |
380 | ! |
legend.title = ggplot2::element_text(size = font_size), |
381 | ! |
legend.text = ggplot2::element_text(size = font_size), |
382 | ! |
strip.text.x = ggplot2::element_text(size = font_size), |
383 | ! |
strip.text.y = ggplot2::element_text(size = font_size) |
384 |
) |
|
385 |
} |
|
386 |
# Format treatment color |
|
387 | ! |
plot1 <- if (!is.null(color_manual)) { |
388 | ! |
plot1 + |
389 | ! |
ggplot2::scale_color_manual(values = color_manual, name = trt_label, guide = ggplot2::guide_legend(order = 1)) |
390 |
} else { |
|
391 | ! |
plot1 + |
392 | ! |
ggplot2::scale_color_discrete(guide = ggplot2::guide_legend(order = 1)) |
393 |
} |
|
394 | ||
395 |
# Format LOQ flag symbol shape |
|
396 | ! |
if (is.null(shape_manual)) { |
397 | ! |
shape_names <- unique(data[!is.na(data[[loq_flag_var]]), ][[loq_flag_var]]) |
398 | ! |
shape_manual <- seq_along(shape_names) |
399 | ! |
names(shape_manual) <- shape_names |
400 |
} |
|
401 |
# add LOQ legend conditionally |
|
402 | ! |
plot1 <- if (!loq_legend) { |
403 | ! |
plot1 + ggplot2::scale_shape_manual(values = shape_manual, name = "LoQ", guide = "none") |
404 |
} else { |
|
405 | ! |
plot1 + ggplot2::scale_shape_manual(values = shape_manual, name = "LoQ", guide = ggplot2::guide_legend(order = 2)) |
406 |
} |
|
407 |
# Format x-label |
|
408 | ! |
if (rotate_xlab) { |
409 | ! |
plot1 <- plot1 + |
410 | ! |
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) |
411 |
} |
|
412 | ||
413 | ! |
plot1 + geom_axes_lines( |
414 | ! |
plot_data, |
415 | ! |
hline_arb = hline_arb, hline_arb_color = hline_arb_color, hline_arb_label = hline_arb_label, |
416 | ! |
hline_vars = hline_vars, hline_vars_colors = hline_vars_colors, hline_vars_labels = hline_vars_labels, |
417 | ! |
vline_arb = vline_arb, vline_arb_color = vline_arb_color, vline_arb_label = vline_arb_label, |
418 | ! |
vline_vars = vline_vars, vline_vars_colors = vline_vars_colors, vline_vars_labels = vline_vars_labels |
419 |
) |
|
420 |
} |
1 |
#' Add horizontal and/or vertical lines and their legend labels to a plot |
|
2 |
#' |
|
3 |
#' This function is currently designed to be used with \code{\link{g_boxplot}}, \code{\link{g_correlationplot}}, |
|
4 |
#' \code{\link{g_spaghettiplot}}, and ('g_density_distribution_plot'), but may also work in general. |
|
5 |
#' |
|
6 |
#' @param data ('data.frame') data where `hline_vars` and `vline_var` columns are taken from. |
|
7 |
#' @param hline_arb ('numeric vector') value identifying intercept for arbitrary horizontal lines. |
|
8 |
#' @param hline_arb_color ('character vector') optional, color for the arbitrary horizontal lines. |
|
9 |
#' @param hline_arb_label ('character vector') optional, label for the legend to the arbitrary horizontal lines. |
|
10 |
#' @param hline_vars ('character vector'), names of variables `(ANR*)` or values `(*LOQ)` identifying intercept values. |
|
11 |
#' Needs `data` to be specified. |
|
12 |
#' @param hline_vars_colors ('character vector') colors for the horizontal lines defined by variables. |
|
13 |
#' @param hline_vars_labels ('character vector') labels for the legend to the horizontal lines defined by variables. |
|
14 |
#' @param vline_arb ('numeric vector') value identifying intercept for arbitrary vertical lines. |
|
15 |
#' @param vline_arb_color ('character vector') optional, color for the arbitrary vertical lines. |
|
16 |
#' @param vline_arb_label ('character vector') optional, label for the legend to the arbitrary vertical lines. |
|
17 |
#' @param vline_vars ('character vector'), names of variables `(ANR*)` or values `(*LOQ)` identifying intercept values. |
|
18 |
#' Needs `data` to be specified. |
|
19 |
#' @param vline_vars_colors ('character vector') colors for the vertical lines defined by variables. |
|
20 |
#' @param vline_vars_labels ('character vector') labels for the legend to the vertical lines defined by variables. |
|
21 |
#' |
|
22 |
#' @examples |
|
23 |
#' p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + |
|
24 |
#' ggplot2::geom_point() + |
|
25 |
#' goshawk:::geom_axes_lines( |
|
26 |
#' hline_arb = c(20, 25, 30), |
|
27 |
#' hline_arb_color = "red", |
|
28 |
#' hline_arb_label = "Hori Line" |
|
29 |
#' ) |
|
30 |
#' @return \code{ggplot} object |
|
31 |
#' @keywords internal |
|
32 |
#' |
|
33 |
geom_axes_lines <- function(data, |
|
34 |
hline_arb = numeric(0), |
|
35 |
hline_arb_color = "red", |
|
36 |
hline_arb_label = "Horizontal line", |
|
37 |
hline_vars = character(0), |
|
38 |
hline_vars_colors = "green", |
|
39 |
hline_vars_labels = hline_vars, |
|
40 |
vline_arb = numeric(0), |
|
41 |
vline_arb_color = "red", |
|
42 |
vline_arb_label = "Vertical line", |
|
43 |
vline_vars = character(0), |
|
44 |
vline_vars_colors = "green", |
|
45 |
vline_vars_labels = vline_vars) { |
|
46 | ! |
arb_hlines <- if (length(hline_arb) > 0) { |
47 | ! |
geom_arb_hline( |
48 | ! |
yintercept = hline_arb, |
49 | ! |
color = hline_arb_color, |
50 | ! |
label = hline_arb_label, |
51 | ! |
legend_title = "Horizontal arbitrary line(s)", |
52 | ! |
linetype = 2 |
53 |
) |
|
54 |
} |
|
55 | ||
56 | ! |
range_hlines <- if (length(hline_vars > 0)) { |
57 | ! |
geom_range_hline( |
58 | ! |
data = data, |
59 | ! |
vars = hline_vars, |
60 | ! |
color = hline_vars_colors, |
61 | ! |
label = hline_vars_labels, |
62 | ! |
linetype = 2 |
63 |
) |
|
64 |
} |
|
65 | ||
66 | ! |
arb_vlines <- if (length(vline_arb) > 0) { |
67 | ! |
geom_arb_vline( |
68 | ! |
xintercept = vline_arb, |
69 | ! |
color = vline_arb_color, |
70 | ! |
label = vline_arb_label, |
71 | ! |
legend_title = "Vertical arbitrary line(s)", |
72 | ! |
linetype = 2 |
73 |
) |
|
74 |
} |
|
75 | ||
76 | ! |
range_vlines <- if (length(vline_vars) > 0) { |
77 | ! |
geom_range_vline( |
78 | ! |
data = data, |
79 | ! |
vars = vline_vars, |
80 | ! |
color = vline_vars_colors, |
81 | ! |
label = vline_vars_labels, |
82 | ! |
linetype = 2 |
83 |
) |
|
84 |
} |
|
85 | ||
86 | ! |
Filter( |
87 | ! |
function(x) !is.null(x) || !is.na(x), |
88 | ! |
list( |
89 | ! |
arb_hlines, |
90 | ! |
range_hlines, |
91 | ! |
arb_vlines, |
92 | ! |
range_vlines |
93 |
) |
|
94 |
) |
|
95 |
} |
|
96 | ||
97 |
#' Validate line arguments given in the parameters against the data. |
|
98 |
#' |
|
99 |
#' helper function to be called by [geom_axes_lines()] |
|
100 |
#' |
|
101 |
#' @param data (`data.frame`)\cr |
|
102 |
#' should contain `vars` which will be used to create the plot. |
|
103 |
#' @param vars (`character`)\cr |
|
104 |
#' names of variables to take the values from. Only first value from the variable will be |
|
105 |
#' applied. |
|
106 |
#' @param color (`character`)\cr |
|
107 |
#' colors for the lines defined by variables. |
|
108 |
#' @param label (`character`)\cr |
|
109 |
#' labels for the legend to the lines defined by variables. |
|
110 |
#' |
|
111 |
#' @return (`data.frame`) containing the `values`,`colors` and `labels` fields defining attributes |
|
112 |
#' for horizontal or vertical lines. |
|
113 |
#' @keywords internal |
|
114 |
#' |
|
115 |
validate_line_args <- function(data, |
|
116 |
vars = character(0), |
|
117 |
color = "green", |
|
118 |
label = vars) { |
|
119 | ! |
if (length(vars) > 0) { |
120 | ! |
stopifnot(all(vars %in% names(data))) |
121 | ! |
checkmate::assert_data_frame(data[vars], types = "numeric") |
122 | ! |
checkmate::assert( |
123 | ! |
check_color(color, len = 1), |
124 | ! |
check_color(color, len = length(vars)) |
125 |
) |
|
126 | ! |
checkmate::assert_character(label, len = length(vars)) |
127 | ||
128 | ||
129 | ! |
label <- vapply( |
130 | ! |
vars, |
131 | ! |
FUN.VALUE = character(1), |
132 | ! |
USE.NAMES = FALSE, |
133 | ! |
FUN = function(x) { |
134 | ! |
if (is.null(attributes(data[[x]])$label)) { |
135 | ! |
x |
136 |
} else { |
|
137 | ! |
attributes(data[[x]])$label |
138 |
} |
|
139 |
} |
|
140 |
) |
|
141 | ||
142 | ! |
vars <- sapply( |
143 | ! |
vars, |
144 | ! |
USE.NAMES = FALSE, |
145 | ! |
function(name) { |
146 | ! |
x <- data[[name]] |
147 | ! |
vals <- unique(x) |
148 | ! |
if (!checkmate::test_number(vals)) { |
149 | ! |
warning(sprintf("First value is taken from variable '%s' to draw the straight line", name)) |
150 |
} |
|
151 | ! |
vals[1] |
152 |
} |
|
153 |
) |
|
154 |
} else { |
|
155 | ! |
vars <- numeric(0) |
156 | ! |
color <- character(0) |
157 | ! |
label <- character(0) |
158 |
} |
|
159 | ||
160 | ! |
data.frame( |
161 | ! |
values = vars, |
162 | ! |
colors = color, |
163 | ! |
labels = label |
164 |
) |
|
165 |
} |
|
166 | ||
167 |
#' Straight lines for `ggplot2` |
|
168 |
#' |
|
169 |
#' Arbitrary lines for `ggplot2` |
|
170 |
#' @name geom_straight_lines |
|
171 |
#' @param vars (`character`)\cr |
|
172 |
#' names of variables `(ANR*)` or values `(*LOQ)` identifying intercept values. |
|
173 |
#' @param xintercept (`numeric`)\cr |
|
174 |
#' Position of the vertical line(s) on the x-axis |
|
175 |
#' @param yintercept (`numeric`)\cr |
|
176 |
#' Position of the horizontal line(s) on the y-axis |
|
177 |
#' @param label (`character`)\cr |
|
178 |
#' Label to be rendered in the legend. |
|
179 |
#' Should be a single string or vector of length equal to length of `xintercept`. |
|
180 |
#' @param color (`character`)\cr |
|
181 |
#' Valid color convertible to RGB scale by [grDevices::col2rgb()]. |
|
182 |
#' Should be a single string or vector of length equal to length of `xintercept`. |
|
183 |
#' |
|
184 |
#' @inherit ggplot2::geom_hline return |
|
185 |
#' |
|
186 |
#' @keywords internal |
|
187 |
#' |
|
188 |
NULL |
|
189 | ||
190 |
#' @rdname geom_straight_lines |
|
191 |
#' @examples |
|
192 |
#' # horizontal arbitrary lines |
|
193 |
#' data <- data.frame(x = seq_len(10), y = seq_len(10), color = rep(c("a", "b"), each = 5)) |
|
194 |
#' ggplot2::ggplot(data, ggplot2::aes(x = x, y = y, color = color)) + |
|
195 |
#' ggplot2::geom_point() + |
|
196 |
#' goshawk:::geom_arb_hline( |
|
197 |
#' yintercept = c(2, 5), color = "blue", label = c("h1", "h2"), linetype = 2 |
|
198 |
#' ) |
|
199 |
geom_arb_hline <- function(yintercept, |
|
200 |
label = "Horizontal line", |
|
201 |
color = "red", |
|
202 |
legend_title = "Horizontal line(s)", |
|
203 |
...) { |
|
204 | ! |
checkmate::assert_numeric(yintercept, min.len = 1) |
205 | ! |
checkmate::assert( |
206 | ! |
check_color(color, len = 1), |
207 | ! |
check_color(color, len = length(yintercept)) |
208 |
) |
|
209 | ! |
checkmate::assert( |
210 | ! |
checkmate::check_string(label), |
211 | ! |
checkmate::check_character(label, len = length(yintercept)) |
212 |
) |
|
213 | ! |
data <- data.frame(yintercept, color, label, color_var = paste(color, label)) |
214 | ||
215 | ! |
list( |
216 | ! |
ggnewscale::new_scale_color(), |
217 | ! |
ggplot2::geom_hline( |
218 | ! |
data = data, |
219 | ! |
mapping = ggplot2::aes( |
220 | ! |
yintercept = yintercept, |
221 | ! |
color = .data[["color_var"]], # need legend entry for each color-label combination |
222 |
), |
|
223 |
... |
|
224 |
), |
|
225 | ! |
ggplot2::scale_color_manual( |
226 | ! |
name = legend_title, |
227 | ! |
values = stats::setNames(data$color, data$color_var), |
228 | ! |
labels = data$label, |
229 | ! |
limits = data$color_var, |
230 | ! |
guide = ggplot2::guide_legend(order = 11) # high order to be put after main plot items |
231 |
) |
|
232 |
) |
|
233 |
} |
|
234 | ||
235 |
#' @rdname geom_straight_lines |
|
236 |
#' @examples |
|
237 |
#' # vertical arbitrary lines |
|
238 |
#' data <- data.frame(x = seq_len(10), y = seq_len(10), color = rep(c("a", "b"), each = 5)) |
|
239 |
#' ggplot2::ggplot(data, ggplot2::aes(x = x, y = y, color = color)) + |
|
240 |
#' ggplot2::geom_point() + |
|
241 |
#' goshawk:::geom_arb_vline( |
|
242 |
#' xintercept = c(2, 5), color = "blue", label = c("h1", "h2"), linetype = 2 |
|
243 |
#' ) |
|
244 |
geom_arb_vline <- function(xintercept, |
|
245 |
label = "Vertical line", |
|
246 |
color = "red", |
|
247 |
legend_title = "Vertical line(s)", |
|
248 |
...) { |
|
249 | ! |
checkmate::assert_numeric(xintercept, min.len = 1) |
250 | ! |
checkmate::assert( |
251 | ! |
check_color(color, len = 1), |
252 | ! |
check_color(color, len = length(xintercept)) |
253 |
) |
|
254 | ! |
checkmate::assert( |
255 | ! |
checkmate::check_character(label, len = 1), |
256 | ! |
checkmate::check_character(label, len = length(xintercept)) |
257 |
) |
|
258 | ||
259 | ! |
data <- data.frame(xintercept, color, label, color_var = paste(color, label)) |
260 | ||
261 | ! |
list( |
262 | ! |
ggnewscale::new_scale_color(), |
263 | ! |
ggplot2::geom_vline( |
264 | ! |
data = data, |
265 | ! |
mapping = ggplot2::aes( |
266 | ! |
xintercept = xintercept, |
267 | ! |
color = .data[["color_var"]], # need legend entry for each color-label combination |
268 |
), |
|
269 |
... |
|
270 |
), |
|
271 | ! |
ggplot2::scale_color_manual( |
272 | ! |
name = legend_title, |
273 | ! |
values = stats::setNames(data$color, data$color_var), |
274 | ! |
labels = data$label, |
275 | ! |
limits = data$color_var, |
276 | ! |
guide = ggplot2::guide_legend(order = 12) # high order to be put after main plot items |
277 |
) |
|
278 |
) |
|
279 |
} |
|
280 | ||
281 | ||
282 |
#' @rdname geom_straight_lines |
|
283 |
#' @examples |
|
284 |
#' # horizontal range |
|
285 |
#' |
|
286 |
#' data <- data.frame( |
|
287 |
#' x = seq_len(10), |
|
288 |
#' y = seq_len(10), |
|
289 |
#' color = rep(c("a", "b"), each = 5), |
|
290 |
#' lower = rep(c(2, 3), each = 5), |
|
291 |
#' upper = rep(c(7, 8), each = 5) |
|
292 |
#' ) |
|
293 |
#' ggplot2::ggplot(data, ggplot2::aes(x = x, y = y, color = color)) + |
|
294 |
#' ggplot2::geom_point() + |
|
295 |
#' goshawk:::geom_range_hline( |
|
296 |
#' vars = c("lower", "upper"), |
|
297 |
#' data = data.frame(lower = 2, upper = 7), |
|
298 |
#' color = "blue", |
|
299 |
#' linetype = 2 |
|
300 |
#' ) |
|
301 |
geom_range_hline <- function(vars, |
|
302 |
data, |
|
303 |
color = "green", |
|
304 |
label = vars, |
|
305 |
legend_title = "Horizontal range line(s)", |
|
306 |
...) { |
|
307 | ! |
line_data <- validate_line_args( |
308 | ! |
data = data, |
309 | ! |
vars = vars, |
310 | ! |
label = label, |
311 | ! |
color = color |
312 |
) |
|
313 | ! |
geom_arb_hline( |
314 | ! |
yintercept = line_data$values, |
315 | ! |
label = line_data$labels, |
316 | ! |
color = line_data$colors, |
317 | ! |
legend_title = legend_title, |
318 |
... |
|
319 |
) |
|
320 |
} |
|
321 | ||
322 |
#' @rdname geom_straight_lines |
|
323 |
#' @examples |
|
324 |
#' # vertical range |
|
325 |
#' data <- data.frame( |
|
326 |
#' x = seq_len(10), |
|
327 |
#' y = seq_len(10), |
|
328 |
#' color = rep(c("a", "b"), each = 5), |
|
329 |
#' lower = rep(c(2, 3), each = 5), |
|
330 |
#' upper = rep(c(7, 8), each = 5) |
|
331 |
#' ) |
|
332 |
#' ggplot2::ggplot(data, ggplot2::aes(x = x, y = y, color = color)) + |
|
333 |
#' ggplot2::geom_point() + |
|
334 |
#' goshawk:::geom_range_vline( |
|
335 |
#' vars = c("lower", "upper"), |
|
336 |
#' data = data.frame(lower = 2, upper = 7), |
|
337 |
#' color = "blue", |
|
338 |
#' linetype = 2 |
|
339 |
#' ) |
|
340 |
geom_range_vline <- function(vars, |
|
341 |
data, |
|
342 |
color = "green", |
|
343 |
label = vars, |
|
344 |
legend_title = "Vertical range line(s)", |
|
345 |
...) { |
|
346 | ! |
line_data <- validate_line_args( |
347 | ! |
data = data, |
348 | ! |
vars = vars, |
349 | ! |
label = label, |
350 | ! |
color = color |
351 |
) |
|
352 | ! |
geom_arb_vline( |
353 | ! |
xintercept = line_data$values, |
354 | ! |
label = line_data$labels, |
355 | ! |
color = line_data$colors, |
356 | ! |
legend_title = legend_title, |
357 |
... |
|
358 |
) |
|
359 |
} |
1 |
#' Function to create a table of descriptive summary statistics to accompany plots. |
|
2 |
#' |
|
3 |
#' Output descriptive summary statistics table as a data frame. Includes biomarker, treatment, |
|
4 |
#' visit, |
|
5 |
#' n, mean, median, SD, min, max, %missing values, % `LOQ` values. |
|
6 |
#' |
|
7 |
#' @param data name of data frame to summarize. |
|
8 |
#' @param trt_group treatment group variable name e.g. `ARM`. |
|
9 |
#' @param param_var name of variable containing biomarker codes e.g. `PARAMCD`. |
|
10 |
#' @param param biomarker to visualize e.g. `IGG`. |
|
11 |
#' @param xaxis_var name of variable containing biomarker results displayed on X-axis e.g. `AVAL`. |
|
12 |
#' @param facet_var name of variable facetted on typically containing visit values e.g. `AVISITCD`. |
|
13 |
#' If NULL then ignored. It defaults to `"AVISITCD"` when not provided. |
|
14 |
#' @param loq_flag_var name of variable containing `LOQ` flag e.g. `LOQFL`. Defaults to `"LOQFL"`. |
|
15 |
#' @param ... additional options |
|
16 |
#' |
|
17 |
#' @author Nick Paszty (npaszty) paszty.nicholas@gene.com |
|
18 |
#' @author Balazs Toth (tothb2) toth.balazs@gene.com |
|
19 |
#' |
|
20 |
#' @details provide additional information as needed. link to specification file |
|
21 |
#' \url{https://posit.co/} |
|
22 |
#' |
|
23 |
#' @export |
|
24 |
#' |
|
25 |
#' @examples |
|
26 |
#' # Example using ADaM structure analysis dataset. |
|
27 |
#' |
|
28 |
#' library(stringr) |
|
29 |
#' |
|
30 |
#' # original ARM value = dose value |
|
31 |
#' arm_mapping <- list( |
|
32 |
#' "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination" |
|
33 |
#' ) |
|
34 |
#' |
|
35 |
#' ADLB <- rADLB |
|
36 |
#' ADLB <- ADLB %>% |
|
37 |
#' mutate(AVISITCD = case_when( |
|
38 |
#' AVISIT == "SCREENING" ~ "SCR", |
|
39 |
#' AVISIT == "BASELINE" ~ "BL", |
|
40 |
#' grepl("WEEK", AVISIT) ~ |
|
41 |
#' paste( |
|
42 |
#' "W", |
|
43 |
#' trimws( |
|
44 |
#' substr( |
|
45 |
#' AVISIT, |
|
46 |
#' start = 6, |
|
47 |
#' stop = str_locate(AVISIT, "DAY") - 1 |
|
48 |
#' ) |
|
49 |
#' ) |
|
50 |
#' ), |
|
51 |
#' TRUE ~ NA_character_ |
|
52 |
#' )) %>% |
|
53 |
#' mutate(AVISITCDN = case_when( |
|
54 |
#' AVISITCD == "SCR" ~ -2, |
|
55 |
#' AVISITCD == "BL" ~ 0, |
|
56 |
#' grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)), |
|
57 |
#' TRUE ~ NA_real_ |
|
58 |
#' )) %>% |
|
59 |
#' # use ARMCD values to order treatment in visualization legend |
|
60 |
#' mutate(TRTORD = ifelse(grepl("C", ARMCD), 1, |
|
61 |
#' ifelse(grepl("B", ARMCD), 2, |
|
62 |
#' ifelse(grepl("A", ARMCD), 3, NA) |
|
63 |
#' ) |
|
64 |
#' )) %>% |
|
65 |
#' mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>% |
|
66 |
#' mutate(ARM = factor(ARM) %>% |
|
67 |
#' reorder(TRTORD)) |
|
68 |
#' |
|
69 |
#' tbl <- t_summarytable( |
|
70 |
#' data = ADLB, |
|
71 |
#' trt_group = "ARM", |
|
72 |
#' param_var = "PARAMCD", |
|
73 |
#' param = c("CRP"), |
|
74 |
#' xaxis_var = "AVAL", |
|
75 |
#' facet_var = "AVISITCD", |
|
76 |
#' loq_flag_var = "LOQFL" |
|
77 |
#' ) |
|
78 |
#' tbl |
|
79 |
t_summarytable <- function(data, |
|
80 |
trt_group, |
|
81 |
param_var, |
|
82 |
param, |
|
83 |
xaxis_var, |
|
84 |
facet_var = "AVISITCD", |
|
85 |
loq_flag_var = "LOQFL", |
|
86 |
...) { |
|
87 | ! |
if (!is.null(facet_var) && trt_group == facet_var) { |
88 | ! |
data[paste0(facet_var, "_")] <- data[facet_var] |
89 | ! |
facet_var <- paste0(facet_var, "_") |
90 |
} |
|
91 | ||
92 | ! |
table_data <- data %>% |
93 | ! |
filter(!!sym(param_var) == param) |
94 | ||
95 |
# get unique study id or unique study ids if multiple study data |
|
96 | ! |
study_id <- as.data.frame(table(table_data$STUDYID)) %>% |
97 | ! |
mutate(STUDYID = paste(.data$Var1, collapse = "/")) %>% |
98 | ! |
rename(StudyID = "STUDYID") %>% |
99 | ! |
select("StudyID") %>% |
100 | ! |
slice(1) |
101 | ||
102 |
# get analysis variable name |
|
103 | ! |
anl_var <- as.data.frame(xaxis_var) %>% |
104 | ! |
rename("AnlVar" = xaxis_var) |
105 | ||
106 | ! |
min_max_ignore_na <- function(x, type = c("min", "max")) { |
107 | ! |
type <- match.arg(type) |
108 | ! |
if (all(is.na(x))) { |
109 | ! |
return(NA) |
110 |
} |
|
111 | ! |
return( |
112 | ! |
switch(type, |
113 | ! |
min = min, |
114 | ! |
max = max |
115 | ! |
)(x, na.rm = TRUE) |
116 |
) |
|
117 |
} |
|
118 | ||
119 |
# by treatment group table |
|
120 | ||
121 | ! |
sum_data_by_arm <- table_data %>% filter(!!sym(param_var) == param) |
122 | ! |
if (!is.null(facet_var)) { |
123 | ! |
sum_data_by_arm <- sum_data_by_arm %>% |
124 | ! |
group_by(!!sym(param_var), !!sym(trt_group), .data$TRTORD, !!sym(facet_var)) |
125 |
} else { |
|
126 | ! |
sum_data_by_arm <- sum_data_by_arm %>% |
127 | ! |
group_by(!!sym(param_var), !!sym(trt_group), .data$TRTORD) |
128 |
} |
|
129 | ||
130 | ! |
sum_data_by_arm <- sum_data_by_arm %>% |
131 | ! |
summarise( |
132 | ! |
n = sum(!is.na(!!sym(xaxis_var))), |
133 | ! |
Mean = round(mean(!!sym(xaxis_var), na.rm = TRUE), digits = 2), |
134 | ! |
Median = round(stats::median(!!sym(xaxis_var), na.rm = TRUE), digits = 2), |
135 | ! |
StdDev = round(stats::sd(!!sym(xaxis_var), na.rm = TRUE), digits = 2), |
136 | ! |
Min = round(min_max_ignore_na(!!sym(xaxis_var), type = "min"), digits = 2), |
137 | ! |
Max = round(min_max_ignore_na(!!sym(xaxis_var), type = "max"), digits = 2), |
138 | ! |
PctMiss = round(100 * sum(is.na(!!sym(xaxis_var))) / length(!!sym(xaxis_var)), digits = 2), |
139 | ! |
PctLOQ = round(100 * sum(!!sym(loq_flag_var) == "Y", na.rm = TRUE) / length(!!sym(loq_flag_var)), digits = 2) |
140 |
) |
|
141 | ||
142 | ! |
if (!is.null(facet_var)) { |
143 | ! |
sum_data_by_arm <- sum_data_by_arm %>% |
144 | ! |
select( |
145 | ! |
all_of(c(param_var, trt_group, facet_var)), "n", "Mean", "Median", "StdDev", |
146 | ! |
"Min", "Max", "PctMiss", "PctLOQ", "TRTORD" |
147 |
) %>% |
|
148 | ! |
ungroup() |
149 |
} else { |
|
150 | ! |
sum_data_by_arm <- sum_data_by_arm %>% |
151 | ! |
select( |
152 | ! |
all_of(c(param_var, trt_group)), "n", "Mean", "Median", "StdDev", |
153 | ! |
"Min", "Max", "PctMiss", "PctLOQ", "TRTORD" |
154 |
) %>% |
|
155 | ! |
ungroup() |
156 |
} |
|
157 | ||
158 |
# by combined treatment group table |
|
159 | ! |
sum_data_combined_arm <- table_data %>% |
160 | ! |
filter(!!sym(param_var) == param) |
161 | ||
162 | ! |
if (!is.null(facet_var)) { |
163 | ! |
sum_data_combined_arm <- sum_data_combined_arm %>% |
164 | ! |
group_by(!!sym(param_var), !!sym(facet_var)) |
165 |
} else { |
|
166 | ! |
sum_data_combined_arm <- sum_data_combined_arm %>% |
167 | ! |
group_by(!!sym(param_var)) |
168 |
} |
|
169 | ||
170 | ! |
sum_data_combined_arm <- sum_data_combined_arm %>% |
171 | ! |
summarise( |
172 | ! |
n = sum(!is.na(!!sym(xaxis_var))), |
173 | ! |
Mean = round(mean(!!sym(xaxis_var), na.rm = TRUE), digits = 2), |
174 | ! |
Median = round(stats::median(!!sym(xaxis_var), na.rm = TRUE), digits = 2), |
175 | ! |
StdDev = round(stats::sd(!!sym(xaxis_var), na.rm = TRUE), digits = 2), |
176 | ! |
Min = round(min_max_ignore_na(!!sym(xaxis_var), type = "min"), digits = 2), |
177 | ! |
Max = round(min_max_ignore_na(!!sym(xaxis_var), type = "max"), digits = 2), |
178 | ! |
PctMiss = round(100 * sum(is.na(!!sym(xaxis_var))) / length(!!sym(xaxis_var)), digits = 2), |
179 | ! |
PctLOQ = round(100 * sum(!!sym(loq_flag_var) == "Y", na.rm = TRUE) / length(!!sym(loq_flag_var)), digits = 2), |
180 | ! |
MAXTRTORDVIS = max(.data$TRTORD) # identifies the maximum treatment order within visits |
181 | ! |
) %>% # additional use of max function identifies maximum treatment order across all visits. |
182 | ! |
mutate(!!trt_group := "Comb.", TRTORD = max(.data$MAXTRTORDVIS) + 1) |
183 | ||
184 |
# select only those columns needed to prop |
|
185 | ! |
if (!is.null(facet_var)) { |
186 | ! |
sum_data_combined_arm <- sum_data_combined_arm %>% |
187 | ! |
select( |
188 | ! |
all_of(c(param_var, trt_group, facet_var)), "n", "Mean", "Median", "StdDev", |
189 | ! |
"Min", "Max", "PctMiss", "PctLOQ", "TRTORD" |
190 |
) %>% |
|
191 | ! |
ungroup() |
192 | ||
193 |
# combine the two data sets and apply some formatting. Note that R coerces treatment group into |
|
194 |
# character since it is a factor and character |
|
195 | ! |
sum_data <- rbind(sum_data_by_arm, sum_data_combined_arm) %>% # concatenate |
196 |
# reorder variables |
|
197 | ! |
select( |
198 | ! |
all_of(c(Biomarker = param_var, Treatment = trt_group, Facet = facet_var)), |
199 | ! |
"n", "Mean", "Median", "StdDev", "Min", "Max", "PctMiss", "PctLOQ", "TRTORD" |
200 |
) %>% |
|
201 | ! |
arrange(.data$Biomarker, .data$Facet, .data$TRTORD) %>% # drop variable |
202 | ! |
select(-"TRTORD") |
203 |
} else { |
|
204 | ! |
sum_data_combined_arm <- sum_data_combined_arm %>% |
205 | ! |
select( |
206 | ! |
all_of(c(param_var, trt_group)), "n", "Mean", "Median", "StdDev", |
207 | ! |
"Min", "Max", "PctMiss", "PctLOQ", "TRTORD" |
208 |
) %>% |
|
209 | ! |
ungroup() |
210 | ||
211 |
# combine the two data sets and apply some formatting. Note that R coerces treatment group into |
|
212 |
# character since it is a factor and character |
|
213 | ! |
sum_data <- rbind(sum_data_by_arm, sum_data_combined_arm) %>% # concatenate |
214 |
# reorder variables |
|
215 | ! |
select( |
216 | ! |
all_of(c(Biomarker = param_var, Treatment = trt_group)), |
217 | ! |
"n", "Mean", "Median", "StdDev", "Min", "Max", "PctMiss", "PctLOQ", "TRTORD" |
218 |
) %>% |
|
219 | ! |
arrange(.data$Biomarker, .data$TRTORD) %>% # drop variable |
220 | ! |
select(-"TRTORD") |
221 |
} |
|
222 | ||
223 |
# add analysis variable as first column |
|
224 | ! |
sum_data <- cbind(study_id, anl_var, sum_data) |
225 |
} |
1 |
#' Function to create a density distribution plot. |
|
2 |
#' |
|
3 |
#' Default plot displays overall density facetted by visit with treatment arms and combined |
|
4 |
#' treatment overlaid. |
|
5 |
#' |
|
6 |
#' @param label text string used to identify plot. |
|
7 |
#' @param data `ADaM` structured analysis laboratory data frame e.g. `ADLB`. |
|
8 |
#' @param param_var name of variable containing biomarker codes e.g. `PARAMCD`. |
|
9 |
#' @param param biomarker to visualize e.g. `IGG`. |
|
10 |
#' @param xaxis_var name of variable containing biomarker results displayed on X-axis e.g. `AVAL`. |
|
11 |
#' @param trt_group name of variable representing treatment group e.g. `ARM`. |
|
12 |
#' @param loq_flag_var name of variable containing `LOQ` flag e.g. `LOQFL`. |
|
13 |
#' @param unit name of variable containing biomarker unit e.g. `AVALU`. |
|
14 |
#' @param xlim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the x-axis |
|
15 |
#' if the default limits are not suitable. |
|
16 |
#' @param ylim ('numeric vector') optional, a vector of length 2 to specify the minimum and maximum of the y-axis |
|
17 |
#' if the default limits are not suitable. |
|
18 |
#' @param color_manual vector of colors applied to treatment values. |
|
19 |
#' @param color_comb name or hex value for combined treatment color. |
|
20 |
#' @param comb_line display combined treatment line toggle. |
|
21 |
#' @param facet_var variable to use for facetting. |
|
22 |
#' @param hline_arb ('numeric vector') value identifying intercept for arbitrary horizontal lines. |
|
23 |
#' @param hline_arb_color ('character vector') optional, color for the arbitrary horizontal lines. |
|
24 |
#' @param hline_arb_label ('character vector') optional, label for the legend to the arbitrary horizontal lines. |
|
25 |
#' @param facet_ncol number of facets per row. |
|
26 |
#' @param rotate_xlab 45 degree rotation of x-axis label values. |
|
27 |
#' @param font_size font size control for title, x-axis label, y-axis label and legend. |
|
28 |
#' @param line_size plot line thickness. |
|
29 |
#' @param rug_plot should a rug plot be displayed under the density plot. Note this |
|
30 |
#' option is most useful if the data only contains a single treatment group. |
|
31 |
#' |
|
32 |
#' @author Nick Paszty (npaszty) paszty.nicholas@gene.com |
|
33 |
#' @author Balazs Toth (tothb2) toth.balazs@gene.com |
|
34 |
#' |
|
35 |
#' @export |
|
36 |
#' |
|
37 |
#' @examples |
|
38 |
#' # Example using ADaM structure analysis dataset. |
|
39 |
#' |
|
40 |
#' library(stringr) |
|
41 |
#' |
|
42 |
#' # original ARM value = dose value |
|
43 |
#' arm_mapping <- list( |
|
44 |
#' "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination" |
|
45 |
#' ) |
|
46 |
#' color_manual <- c("150mg QD" = "#000000", "Placebo" = "#3498DB", "Combination" = "#E74C3C") |
|
47 |
#' |
|
48 |
#' ADLB <- rADLB |
|
49 |
#' var_labels <- lapply(ADLB, function(x) attributes(x)$label) |
|
50 |
#' ADLB <- ADLB %>% |
|
51 |
#' mutate(AVISITCD = case_when( |
|
52 |
#' AVISIT == "SCREENING" ~ "SCR", |
|
53 |
#' AVISIT == "BASELINE" ~ "BL", |
|
54 |
#' grepl("WEEK", AVISIT) ~ |
|
55 |
#' paste( |
|
56 |
#' "W", |
|
57 |
#' trimws( |
|
58 |
#' substr( |
|
59 |
#' AVISIT, |
|
60 |
#' start = 6, |
|
61 |
#' stop = str_locate(AVISIT, "DAY") - 1 |
|
62 |
#' ) |
|
63 |
#' ) |
|
64 |
#' ), |
|
65 |
#' TRUE ~ NA_character_ |
|
66 |
#' )) %>% |
|
67 |
#' mutate(AVISITCDN = case_when( |
|
68 |
#' AVISITCD == "SCR" ~ -2, |
|
69 |
#' AVISITCD == "BL" ~ 0, |
|
70 |
#' grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)), |
|
71 |
#' TRUE ~ NA_real_ |
|
72 |
#' )) %>% |
|
73 |
#' # use ARMCD values to order treatment in visualization legend |
|
74 |
#' mutate(TRTORD = ifelse(grepl("C", ARMCD), 1, |
|
75 |
#' ifelse(grepl("B", ARMCD), 2, |
|
76 |
#' ifelse(grepl("A", ARMCD), 3, NA) |
|
77 |
#' ) |
|
78 |
#' )) %>% |
|
79 |
#' mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>% |
|
80 |
#' mutate(ARM = factor(ARM) %>% |
|
81 |
#' reorder(TRTORD)) |
|
82 |
#' attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]] |
|
83 |
#' |
|
84 |
#' g_density_distribution_plot( |
|
85 |
#' label = "Density Distribution Plot", |
|
86 |
#' data = ADLB, |
|
87 |
#' param_var = "PARAMCD", |
|
88 |
#' param = c("CRP"), |
|
89 |
#' xaxis_var = "AVAL", |
|
90 |
#' unit = "AVALU", |
|
91 |
#' color_manual = color_manual, |
|
92 |
#' color_comb = "#39ff14", |
|
93 |
#' comb_line = FALSE, |
|
94 |
#' facet_var = "AVISITCD", |
|
95 |
#' hline_arb = 1.75, |
|
96 |
#' hline_arb_color = "black", |
|
97 |
#' hline_arb_label = "Horizontal Line A", |
|
98 |
#' facet_ncol = 2, |
|
99 |
#' rotate_xlab = FALSE, |
|
100 |
#' font_size = 10, |
|
101 |
#' line_size = .5 |
|
102 |
#' ) |
|
103 |
g_density_distribution_plot <- function(label = "Density Distribution Plot", |
|
104 |
data, |
|
105 |
param_var = "PARAMCD", |
|
106 |
param = "CRP", |
|
107 |
xaxis_var = "AVAL", |
|
108 |
trt_group = "ARM", |
|
109 |
unit = "AVALU", |
|
110 |
loq_flag_var = "LOQFL", |
|
111 |
xlim = c(NA, NA), |
|
112 |
ylim = c(NA, NA), |
|
113 |
color_manual = NULL, |
|
114 |
color_comb = "#39ff14", |
|
115 |
comb_line = TRUE, |
|
116 |
facet_var = "AVISITCD", |
|
117 |
hline_arb = character(0), |
|
118 |
hline_arb_color = "red", |
|
119 |
hline_arb_label = "Horizontal line", |
|
120 |
facet_ncol = 2, |
|
121 |
rotate_xlab = FALSE, |
|
122 |
font_size = 12, |
|
123 |
line_size = 2, |
|
124 |
rug_plot = FALSE) { |
|
125 | ! |
checkmate::assert_numeric(xlim, len = 2) |
126 | ! |
checkmate::assert_numeric(ylim, len = 2) |
127 | ||
128 | ! |
plot_data <- data %>% |
129 | ! |
filter(!!sym(param_var) == param) |
130 | ||
131 |
# Setup the ggtitle label. Combine the biomarker and the units (if available) |
|
132 | ! |
ggtitle_label <- ifelse( |
133 | ! |
is.null(unit), |
134 | ! |
paste(plot_data$PARAM, "Density: Combined Treatment (Comb.) & by Treatment @ Visits"), |
135 | ! |
ifelse( |
136 | ! |
plot_data[[unit]] == "", |
137 | ! |
paste(plot_data$PARAM, "Density: Combined Treatment (Comb.) & by Treatment @ Visits"), |
138 | ! |
paste0(plot_data$PARAM, " (", plot_data[[unit]], ") Density: Combined Treatment (Comb.) & by Treatment @ Visits") |
139 |
) |
|
140 |
) |
|
141 | ||
142 |
# Setup the x-axis label. Combine the biomarker and the units (if available) |
|
143 | ! |
x_axis_label <- ifelse( |
144 | ! |
is.null(unit), |
145 | ! |
paste(plot_data$PARAM, xaxis_var, "Values"), |
146 | ! |
ifelse( |
147 | ! |
plot_data[[unit]] == "", |
148 | ! |
paste(plot_data$PARAM, xaxis_var, "Values"), |
149 | ! |
paste0(plot_data$PARAM, " (", plot_data[[unit]], ") ", xaxis_var, " Values") |
150 |
) |
|
151 |
) |
|
152 | ||
153 |
# Setup legend label |
|
154 | ! |
trt_label <- `if`(is.null(attr(data[[trt_group]], "label")), "Dose", attr(data[[trt_group]], "label")) |
155 | ||
156 | ! |
if (comb_line) { |
157 | ! |
plot_data <- dplyr::bind_rows( |
158 | ! |
plot_data, |
159 | ! |
plot_data %>% |
160 | ! |
dplyr::mutate(!!sym(trt_group) := "Combined Dose") |
161 |
) |
|
162 |
} |
|
163 | ||
164 | ! |
color_manual <- if (is.null(color_manual)) { |
165 | ! |
group_names <- unique(plot_data[[trt_group]]) |
166 | ! |
color_values <- seq_along(group_names) |
167 | ! |
if (!is.null(getOption("ggplot2.discrete.colour"))) { |
168 | ! |
color_values <- getOption("ggplot2.discrete.colour")[color_values] |
169 |
} |
|
170 | ! |
names(color_values) <- group_names |
171 | ! |
color_values |
172 |
} else { |
|
173 | ! |
color_manual |
174 |
} |
|
175 | ||
176 | ! |
if (comb_line) { |
177 | ! |
if (!is.null(color_comb)) { |
178 | ! |
color_manual["Combined Dose"] <- color_comb |
179 | ! |
} else if (!"Combined Dose" %in% names(color_manual)) { |
180 | ! |
color_manual["Combined Dose"] <- length(color_manual) + 1 |
181 |
} |
|
182 |
} |
|
183 | ||
184 |
# Add footnote to identify LLOQ and ULOQ values pulled from data |
|
185 | ! |
caption_loqs_label <- h_caption_loqs_label(loqs_data = plot_data, flag_var = loq_flag_var) |
186 | ||
187 | ! |
plot1 <- ggplot2::ggplot(plot_data) + |
188 | ! |
ggplot2::stat_density( |
189 | ! |
ggplot2::aes(x = !!sym(xaxis_var), colour = !!sym(trt_group)), |
190 | ! |
linewidth = line_size, |
191 | ! |
geom = "line", |
192 | ! |
position = "identity" |
193 |
) + |
|
194 | ! |
ggplot2::coord_cartesian(xlim = xlim, ylim = ylim) + |
195 | ! |
ggplot2::facet_wrap(stats::as.formula(paste0(" ~ ", facet_var)), ncol = facet_ncol) + |
196 | ! |
ggplot2::labs(caption = caption_loqs_label) + |
197 | ! |
ggplot2::theme_bw() + |
198 | ! |
ggplot2::ggtitle(ggtitle_label) + |
199 | ! |
ggplot2::theme(plot.title = ggplot2::element_text(size = font_size, hjust = 0.5)) + |
200 | ! |
ggplot2::xlab(paste(x_axis_label)) + |
201 | ! |
ggplot2::ylab(paste("Density")) + |
202 | ! |
ggplot2::scale_color_manual(values = color_manual, name = trt_label, guide = ggplot2::guide_legend(order = 1)) |
203 | ||
204 | ! |
if (rug_plot) { |
205 | ! |
plot1 <- plot1 + |
206 | ! |
ggplot2::geom_rug(ggplot2::aes(x = !!sym(xaxis_var), colour = !!sym(trt_group))) |
207 |
} |
|
208 | ||
209 |
# Format font size |
|
210 | ! |
if (!is.null(font_size)) { |
211 | ! |
plot1 <- plot1 + |
212 | ! |
ggplot2::theme( |
213 | ! |
axis.title.x = ggplot2::element_text(size = font_size), |
214 | ! |
axis.text.x = ggplot2::element_text(size = font_size), |
215 | ! |
axis.title.y = ggplot2::element_text(size = font_size), |
216 | ! |
axis.text.y = ggplot2::element_text(size = font_size), |
217 | ! |
legend.title = ggplot2::element_text(size = font_size), |
218 | ! |
legend.text = ggplot2::element_text(size = font_size), |
219 | ! |
strip.text.x = ggplot2::element_text(size = font_size), |
220 | ! |
strip.text.y = ggplot2::element_text(size = font_size) |
221 |
) |
|
222 |
} |
|
223 | ||
224 |
# Format x-label |
|
225 | ! |
if (rotate_xlab) { |
226 | ! |
plot1 <- plot1 + |
227 | ! |
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) |
228 |
} |
|
229 | ||
230 |
# Add horizontal line |
|
231 | ! |
plot1 + geom_axes_lines( |
232 | ! |
plot_data, |
233 | ! |
hline_arb = hline_arb, |
234 | ! |
hline_arb_color = hline_arb_color, |
235 | ! |
hline_arb_label = hline_arb_label |
236 |
) |
|
237 |
} |