From bfb08845ebc8ef0398054c1bac850cc081e661ec Mon Sep 17 00:00:00 2001 From: Christian Covington Date: Mon, 5 Mar 2018 09:13:23 -0500 Subject: [PATCH 1/4] added two ways to generate calibration plot without saving --- R/calibration_by_factor.R | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/R/calibration_by_factor.R b/R/calibration_by_factor.R index 34e481b..ec6f812 100644 --- a/R/calibration_by_factor.R +++ b/R/calibration_by_factor.R @@ -13,7 +13,7 @@ #' @import data.table #' #' @param data data.table object containing data to be plotted. (data table) -#' @param output_file filepath to which we write plot. (character) +#' @param output_file filepath to which we write plot (if NA or '', plot will not save). (character) #' @param outcome_col_name column name of outcome for which we want the mean (by factor and risk quantile) plotted on y-axis. (character) #' @param quantile_col_name column name of risk quantile to go on x-axis (could be something like y_hat_percentile). (character) #' @param cluster_by_col_name column name of grouping for which we want to cluster standard errors (typically something like empi). (character) @@ -123,7 +123,7 @@ plot_calibration_by_risk_quantile_by_factor <- function( } # create calibration plot - with RIBBON for SE - if(SE_line == TRUE & SE_style == 'ribbon'){ + if (SE_line == TRUE & SE_style == 'ribbon') { calibration_plot <- ggplot(data = plot_dt, aes( y = mean_obs_outcome, x = get(quantile_col_name), @@ -149,8 +149,10 @@ plot_calibration_by_risk_quantile_by_factor <- function( g <- calibration_plot } - # save - ggsave(output_file, g) + # save output if path is not NA + if (!is.na(output_file) | output_file == '') { + ggsave(output_file, g) + } return(g) } From c57fd003f844a7c5f729c1039388ccedd9ede7b2 Mon Sep 17 00:00:00 2001 From: Christian Covington Date: Mon, 5 Mar 2018 10:53:33 -0500 Subject: [PATCH 2/4] fixed unintended behavior during save --- R/calibration_by_factor.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calibration_by_factor.R b/R/calibration_by_factor.R index 8d01604..f0a1237 100644 --- a/R/calibration_by_factor.R +++ b/R/calibration_by_factor.R @@ -158,7 +158,7 @@ plot_calibration_by_risk_quantile_by_factor <- function( } # save output if path is not NA - if (!is.na(output_file) | output_file == '') { + if (!is.na(output_file) & output_file != '') { ggsave(output_file, g) } if (return_plot) { From 88061b25fa181799a9f59d4171a2af2cbe7e1a0f Mon Sep 17 00:00:00 2001 From: Christian Covington Date: Mon, 5 Mar 2018 12:55:41 -0500 Subject: [PATCH 3/4] allow for quantile_col_name to be factor without throwing error (code now converts to numeric when necessary) --- R/calibration_by_factor.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/calibration_by_factor.R b/R/calibration_by_factor.R index f0a1237..3d4c2b6 100644 --- a/R/calibration_by_factor.R +++ b/R/calibration_by_factor.R @@ -131,6 +131,7 @@ plot_calibration_by_risk_quantile_by_factor <- function( } # create calibration plot - with RIBBON for SE + plot_dt[, get(quantile_col_name) := as.numeric(get(quantile_col_name))] # convert quantile column to numeric (if not already) for better plotting if (SE_line == TRUE & SE_style == 'ribbon') { calibration_plot <- ggplot(data = plot_dt, aes( y = mean_obs_outcome, From 2a6e1cad116d71be33b4344afb1b6166d9051369 Mon Sep 17 00:00:00 2001 From: Christian Covington Date: Mon, 5 Mar 2018 15:17:49 -0500 Subject: [PATCH 4/4] added argument to allow for no legend --- R/calibration_by_factor.R | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/R/calibration_by_factor.R b/R/calibration_by_factor.R index 3d4c2b6..7dc3282 100644 --- a/R/calibration_by_factor.R +++ b/R/calibration_by_factor.R @@ -27,7 +27,8 @@ #' vector of hex color codes matching the number of levels in plot_by_col_name. (character) #' @param ymin minimum of y-axis. (numeric) #' @param make_footnote whether or not to include a footnote on the plot (boolean) -#' @param return_plot whether or not to return created plot (boolean)' +#' @param include_legend whether or not to include a legend on the plot (boolean) +#' @param return_plot whether or not to return created plot (boolean) #' #' @return g optionally returns created plot #' @@ -55,8 +56,12 @@ plot_calibration_by_risk_quantile_by_factor <- function( SE_style = 'ribbon', ymin = 0, make_footnote = TRUE, + include_legend = TRUE, return_plot = FALSE) { + # set legend to appear at bottom of plot, or not appear at all + legend_position <- ifelse(include_legend == TRUE, 'bottom', 'none') + # compute means data[, mean_obs_outcome := mean(get(outcome_col_name)), by = c(quantile_col_name, plot_by_col_name)] @@ -102,6 +107,8 @@ plot_calibration_by_risk_quantile_by_factor <- function( n_breaks <- ifelse(uniqueN(data[, get(quantile_col_name)]) < 5, uniqueN(data[, get(quantile_col_name)]), 5) + plot_dt[, (quantile_col_name) := as.numeric(get(quantile_col_name))] # convert quantile column to numeric (if not already) for better plotting + # create calibration plot - with LINES for SE if(!SE_line | (SE_line & SE_style == 'line')) { calibration_plot <- ggplot(data = plot_dt, aes(y = mean_obs_outcome, x = get(quantile_col_name), color = factor(get(plot_by_col_name)))) + @@ -109,7 +116,7 @@ plot_calibration_by_risk_quantile_by_factor <- function( geom_line(aes(group = factor(get(plot_by_col_name)))) + color_scale + theme_bw() + - theme(legend.position = 'bottom') + + theme(legend.position = legend_position) + xlab(xlabel) + ylab(ylabel) + # determine where the axes begin, make sure y axis is % @@ -131,7 +138,6 @@ plot_calibration_by_risk_quantile_by_factor <- function( } # create calibration plot - with RIBBON for SE - plot_dt[, get(quantile_col_name) := as.numeric(get(quantile_col_name))] # convert quantile column to numeric (if not already) for better plotting if (SE_line == TRUE & SE_style == 'ribbon') { calibration_plot <- ggplot(data = plot_dt, aes( y = mean_obs_outcome, @@ -146,7 +152,7 @@ plot_calibration_by_risk_quantile_by_factor <- function( xlab(xlabel) + ylab(ylabel) + theme_bw() + - theme(legend.position = 'bottom') + + theme(legend.position = legend_position) + scale_y_continuous(labels = scales::percent, limits = c(ymin, NA))+ scale_x_continuous(breaks = pretty_breaks(n = n_breaks)) } @@ -158,7 +164,7 @@ plot_calibration_by_risk_quantile_by_factor <- function( g <- calibration_plot } - # save output if path is not NA + # save output if path is not NA or '' if (!is.na(output_file) & output_file != '') { ggsave(output_file, g) }