|
| 1 | +#' FDA Figure 14: Mean and 95% Confidence Interval of Systolic Blood Pressure Over Time |
| 2 | +#' by Treatment Arm, Safety Population, Trial X |
| 3 | +#' |
| 4 | +#' @details |
| 5 | +#' * `df` must contain the variables `AVAL` and `PARAMCD`, and the variables specified by `arm_var`, |
| 6 | +#' `saffl_var`, `visit_var`, and `add_cond`. |
| 7 | +#' * Flag variables (i.e. `XXXFL`) are expected to have two levels: `"Y"` (true) and `"N"` (false). Missing values in |
| 8 | +#' flag variables are treated as `"N"`. |
| 9 | +#' * It is assumed that `df` contains one unique record per patient. |
| 10 | +#' |
| 11 | +#' @inheritParams argument_convention |
| 12 | +#' @param add_table (`flag`)\cr whether "Mean Value" and "Number of Patients" tables should be printed under the plot |
| 13 | +#' @param visit_var (`character`)\cr visit variable to put on the x-axis |
| 14 | +#' @param paramcd_val (`character`)\cr value of `PARAMCD` to plot |
| 15 | +#' @param add_cond (`expr`)\cr expression that provides additional filters for the analysis |
| 16 | +#' (for instance on `ATPT` or `VSPOS`) |
| 17 | +#' @param annotations (named `list` of `character`)\cr list of annotations to add to the figure. Valid annotation types |
| 18 | +#' are `title`, `subtitles`, and `caption`. Each name-value pair should use the annotation type as name and the |
| 19 | +#' desired string as value. |
| 20 | +#' |
| 21 | +#' @return A `ggplot2` object. |
| 22 | +#' |
| 23 | +#' @examples |
| 24 | +#' advs <- random.cdisc.data::cadvs |
| 25 | +#' |
| 26 | +#' fig <- make_fig_14( |
| 27 | +#' df = advs, |
| 28 | +#' add_cond = bquote("ONTRTFL == 'Y' | ABLFL == 'Y'"), |
| 29 | +#' add_table = TRUE, |
| 30 | +#' yticks = c(135, 140, 145, 150, 155, 160) |
| 31 | +#' ) |
| 32 | +#' fig |
| 33 | +#' |
| 34 | +#' @export |
| 35 | +make_fig_14 <- function(df, |
| 36 | + arm_var = "ARM", |
| 37 | + saffl_var = "SAFFL", |
| 38 | + visit_var = "AVISIT", |
| 39 | + paramcd_val = "SYSBP", |
| 40 | + add_cond = NULL, |
| 41 | + x_lab = "", |
| 42 | + y_lab = NULL, |
| 43 | + yticks = NA, |
| 44 | + ggtheme = NULL, |
| 45 | + add_table = TRUE, |
| 46 | + annotations = NULL) { |
| 47 | + checkmate::assert_subset(c(arm_var, saffl_var, visit_var), names(df)) |
| 48 | + assert_flag_variables(df, saffl_var) |
| 49 | + |
| 50 | + df <- df %>% |
| 51 | + as_tibble() %>% |
| 52 | + filter( |
| 53 | + .data[[saffl_var]] == "Y", |
| 54 | + PARAMCD == {{ paramcd_val }}, |
| 55 | + !is.na(AVAL) |
| 56 | + ) %>% |
| 57 | + df_explicit_na() |
| 58 | + |
| 59 | + if (!(is.null({{ add_cond }}))) { |
| 60 | + df <- df %>% |
| 61 | + filter(!!rlang::parse_expr(add_cond)) |
| 62 | + } |
| 63 | + |
| 64 | + if (is.null({{ y_lab }})) { |
| 65 | + y_param <- unique(df$PARAM) |
| 66 | + y_avalu <- unique(df$AVALU) |
| 67 | + |
| 68 | + y_lab <- paste0("Mean Value (95% CI)", "\n", y_param, " (", y_avalu, ")") |
| 69 | + } |
| 70 | + |
| 71 | + df <- df %>% |
| 72 | + group_by(!!sym(arm_var), !!sym(visit_var), .drop = TRUE) %>% |
| 73 | + summarise( |
| 74 | + mean = mean(AVAL, na.rm = TRUE), |
| 75 | + sd = sd(AVAL, na.rm = TRUE), |
| 76 | + n = n() |
| 77 | + ) %>% |
| 78 | + mutate( |
| 79 | + se = sd / sqrt(n), |
| 80 | + lower_ci = mean - qt(1 - (0.05 / 2), n - 1) * se, |
| 81 | + upper_ci = mean + qt(1 - (0.05 / 2), n - 1) * se |
| 82 | + ) %>% |
| 83 | + ungroup() |
| 84 | + |
| 85 | + g <- |
| 86 | + ggplot( |
| 87 | + data = df, |
| 88 | + aes( |
| 89 | + x = !!sym(visit_var), |
| 90 | + y = mean, |
| 91 | + group = .data[[arm_var]], |
| 92 | + color = .data[[arm_var]] |
| 93 | + ) |
| 94 | + ) + |
| 95 | + geom_point(position = position_dodge(width = 0.5)) + |
| 96 | + geom_line(position = position_dodge(width = 0.5)) + |
| 97 | + geom_errorbar( |
| 98 | + aes( |
| 99 | + ymin = lower_ci, |
| 100 | + ymax = upper_ci |
| 101 | + ), |
| 102 | + position = position_dodge(width = 0.5) |
| 103 | + ) + |
| 104 | + labs( |
| 105 | + title = annotations[["title"]], |
| 106 | + subtitle = annotations[["subtitle"]], |
| 107 | + caption = annotations[["caption"]], |
| 108 | + x = x_lab, |
| 109 | + y = y_lab |
| 110 | + ) + |
| 111 | + theme( |
| 112 | + legend.position = "bottom", |
| 113 | + legend.title = element_blank(), |
| 114 | + plot.margin = unit(c(0.05, 0.05, 0, 0.025), "npc") |
| 115 | + ) |
| 116 | + |
| 117 | + if (any(!is.na(yticks))) { |
| 118 | + g <- g + |
| 119 | + scale_y_continuous(breaks = yticks, limits = c(min(yticks), max(yticks))) |
| 120 | + } |
| 121 | + |
| 122 | + if (!is.null(ggtheme)) g <- g + ggtheme |
| 123 | + |
| 124 | + if (add_table) { |
| 125 | + g_legend <- cowplot::get_legend(g) |
| 126 | + g <- g + theme(legend.position = "none") |
| 127 | + |
| 128 | + tbl_n <- df %>% |
| 129 | + mutate(meanr = sprintf("%.1f", mean)) %>% |
| 130 | + arrange(desc(!!sym(arm_var))) |
| 131 | + |
| 132 | + g_tbl1 <- ggplot(tbl_n, aes(x = !!sym(visit_var), y = !!sym(arm_var))) + |
| 133 | + theme( |
| 134 | + axis.title.x = element_blank(), |
| 135 | + axis.title.y = element_blank(), |
| 136 | + axis.ticks.x = element_blank(), |
| 137 | + axis.ticks.y = element_blank(), |
| 138 | + panel.background = element_blank(), |
| 139 | + axis.text.x = element_blank(), |
| 140 | + panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5), |
| 141 | + plot.margin = unit(c(0.1, 0.05, 0, 0.025), "npc"), |
| 142 | + plot.title = element_text(size = 10) |
| 143 | + ) + |
| 144 | + labs(title = "Mean Value") |
| 145 | + |
| 146 | + for (i in seq_len(nrow(tbl_n))) { |
| 147 | + g_tbl1 <- g_tbl1 + |
| 148 | + annotate("text", label = as.character(tbl_n$meanr[i]), x = tbl_n[[visit_var]][i], y = tbl_n[[arm_var]][i]) |
| 149 | + } |
| 150 | + |
| 151 | + g_tbl2 <- ggplot(tbl_n, aes(x = !!sym(visit_var), y = !!sym(arm_var))) + |
| 152 | + theme( |
| 153 | + axis.title.x = element_blank(), |
| 154 | + axis.title.y = element_blank(), |
| 155 | + axis.ticks.x = element_blank(), |
| 156 | + axis.ticks.y = element_blank(), |
| 157 | + panel.background = element_blank(), |
| 158 | + axis.text.x = element_blank(), |
| 159 | + panel.border = element_rect(color = "black", fill = NA, linewidth = 0.2), |
| 160 | + plot.margin = unit(c(0.1, 0.05, 0, 0.025), "npc"), |
| 161 | + plot.title = element_text(size = 10) |
| 162 | + ) + |
| 163 | + labs(title = "Number of Patients with Data") |
| 164 | + |
| 165 | + for (i in seq_len(nrow(tbl_n))) { |
| 166 | + g_tbl2 <- g_tbl2 + |
| 167 | + annotate("text", label = as.character(tbl_n$n[i]), x = tbl_n[[visit_var]][i], y = tbl_n[[arm_var]][i]) |
| 168 | + } |
| 169 | + |
| 170 | + cowplot::plot_grid( |
| 171 | + g, |
| 172 | + g_tbl1, |
| 173 | + g_tbl2, |
| 174 | + g_legend, |
| 175 | + align = "v", |
| 176 | + axis = "l", |
| 177 | + ncol = 1, |
| 178 | + rel_heights = c(0.60, 0.15, 0.15, 0.1) |
| 179 | + ) |
| 180 | + } else { |
| 181 | + g |
| 182 | + } |
| 183 | +} |
0 commit comments