From c3fecc1bf02818b037fca14dcc8cc34752358ced Mon Sep 17 00:00:00 2001 From: Jens Sauer Date: Mon, 16 Nov 2020 22:38:07 +0100 Subject: [PATCH] survival: Create functions for plots Each plot has now its own function. This makes it easier to adjust the creation and tweaking of a single plot. sma_plot_file() will now call the individual plot function before saveing the plot. --- survival.R | 84 ++++++++++++++++++++++++++---------------------------- utils.R | 11 ++----- 2 files changed, 42 insertions(+), 53 deletions(-) diff --git a/survival.R b/survival.R index 5bc657f..3b5b0eb 100644 --- a/survival.R +++ b/survival.R @@ -13,53 +13,49 @@ surv_dx <- Surv(time = secmal$event_time_dx, event = secmal$event_status) surv_asct <- Surv(time = secmal$event_time_asct, event = secmal$event_status) # plot survival after diagnosis -# scaled to years +sma_plot_surv_dx <- function() { + plot(survfit(surv_dx ~ 1), mark.time = TRUE, xscale = 365.25, + xlab = "Years", ylab = "Survival") + title("Kaplan-Meier estimate for\nsecMalASCT study", + "Survival after diagnosis") +} -sma_plot_file("survival_dx.png", png, - list(main = "Kaplan-Meier estimate for\nsecMalASCT study", - sub = "Survival after diagnosis"), - NULL, - plot, survfit(surv_dx ~ 1), - mark.time = TRUE, - xscale = 365.25, - xlab = "Years", - ylab = "Survival") +# plot survival after diagnosis per sex +sma_plot_surv_dx_sex <- function() { + plot(survfit(surv_dx ~ sex, data = secmal), mark.time = TRUE, + xscale = 365.25, xlab = "Years", ylab = "Survival", + lty = 2:3) + title("Kaplan-Meier estimate for\nsecMalASCT study", + "Survival after diagnosis") + legend(100, .9, c("Female", "Male"), lty = 2:3) +} -# One graph per sex -sma_plot_file("survival_dx_sex.png", png, - list(main = "Kaplan-Meier estimate for\nsecMalASCT study", - sub = "Survival after diagnosis"), - list(100, .9, c("Female", "Male"), lty = 2:3), - plot, survfit(surv_dx ~ sex, data = secmal), - mark.time = TRUE, - xscale = 365.25, - xlab = "Years", - ylab = "Survival", - lty = 2:3) +# plot survival after transplantation +sma_plot_surv_asct <- function() { + plot(survfit(surv_asct ~ 1), mark.time = TRUE, xscale = 365.25, + xlab = "Years", ylab = "Survival") + title("Kaplan-Meier estimate for\nsecMalASCT study", + "Survival after transplantation") +} -# plot survival after diagnosis -# scaled to years -sma_plot_file("survival_asct.png", png, - list(main = "Kaplan-Meier estimate for\nsecMalASCT study", - sub = "Survival after transplantation"), - NULL, - plot, survfit(surv_asct ~ 1), - mark.time = TRUE, - xscale = 365.25, - xlab = "Years", - ylab = "Survival") +# plot survival after transplantation per sex +sma_plot_surv_asct_sex <- function() { + plot(survfit(surv_asct ~ sex, data = secmal), mark.time = TRUE, + xscale = 365.25, xlab = "Years", ylab = "Survival", + lty = 2:3) + title("Kaplan-Meier estimate for\nsecMalASCT study", + "Survival after transplantation") + legend(100, .9, c("Female", "Male"), lty = 2:3) +} - -# One graph per sex -sma_plot_file("survival_asct_sex.png", png, - list(main = "Kaplan-Meier estimate for\nsecMalASCT study", - sub = "Survival after transplantation"), - list(100, .9, c("Female", "Male"), lty = 2:3), - plot, survfit(surv_asct ~ sex, data = secmal), - mark.time = TRUE, - xscale = 365.25, - xlab = "Years", - ylab = "Survival", - lty = 2:3) +# +# Write survival plots to files +# +sma_plot_file_surv <- function() { + sma_plot_file("survival_dx.png", png, sma_plot_surv_dx) + sma_plot_file("survival_dx_sex.png", png, sma_plot_surv_dx_sex) + sma_plot_file("survival_asct.png", png, sma_plot_surv_asct) + sma_plot_file("survival_asct_sex.png", png, sma_plot_surv_asct_sex) +} diff --git a/utils.R b/utils.R index c8c6105..2972992 100644 --- a/utils.R +++ b/utils.R @@ -13,19 +13,12 @@ library(tidyverse) # or NULL if not needed. # Default filesize is 3000x3000 px and 300 dpi resolution. # -sma_plot_file <- function(fname, ftype, title_list, legend_list, fun, ...) { +sma_plot_file <- function(fname, ftype, fun) { # Open file for writing ftype(filename = fname, width = 3000, height = 3000, res = 300) - # run "fun" with passed arguments - do.call(fun, list(...)) + fun() - if (!is.null(title_list)) { - do.call(title, title_list) - } - if(!is.null(legend_list)) { - do.call(legend, legend_list) - } # hide "null device 1" in garbage garbage <- dev.off() }