From 12ffcd4e3843a14aa09c236a979b2b7433f35281 Mon Sep 17 00:00:00 2001 From: Jens Sauer Date: Mon, 16 Nov 2020 23:22:45 +0100 Subject: [PATCH] survival: Change plot function to ggsurvplot This will create the plot using the ggsurvplot from "survminer" package. survminer user ggplot for plotting and comes with some powerful and nice looking features. --- README.md | 1 + survival.R | 37 +++++++++++++++++-------------------- utils.R | 9 ++++++++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 4e984b1..ceeb332 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,4 @@ secMalASCT statistical analysis R packages used --------------- * tidyverse +* survminer diff --git a/survival.R b/survival.R index 3d275c4..9b165a8 100644 --- a/survival.R +++ b/survival.R @@ -4,6 +4,7 @@ # Jens Mathis Sauer (c) 2020 library(survival) +library(survminer) source("utils.R") secmal <- sma_load_data("current.csv") @@ -14,38 +15,34 @@ surv_asct <- Surv(time = secmal$event_time_asct, event = secmal$event_status) # plot survival after diagnosis 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") + ggsurvplot(survfit(surv_dx ~ 1), data = secmal, xscale = "d_y", + title = "Survival after diagnosis", + break.time.by = 5 * 365.25) } # 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) + ggsurvplot(survfit(surv_dx ~ sex, data = secmal), data = secmal, + xscale = "d_y", title = "Survival after diagnosis", + legend.labs = c("Female", "Male"), + break.time.by = 5 * 365.25, + pval = TRUE) } # 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") + ggsurvplot(survfit(surv_asct ~ 1), data = secmal, xscale = "d_y", + title = "Survival after transplantation", + break.time.by = 5 * 365.25) } # 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) + ggsurvplot(survfit(surv_asct ~ sex, data = secmal), data = secmal, + xscale = "d_y", title = "Survival after transplantation", + legend.labs = c("Female", "Male"), + break.time.by = 5 * 365.25, + pval = TRUE) } # diff --git a/utils.R b/utils.R index 2972992..9ca0719 100644 --- a/utils.R +++ b/utils.R @@ -17,7 +17,14 @@ sma_plot_file <- function(fname, ftype, fun) { # Open file for writing ftype(filename = fname, width = 3000, height = 3000, res = 300) - fun() + # + # Write plot into variable, then printing it manually. This is + # needed to save plots from package "survminer" at the moment. + # + # See: https://github.com/kassambara/survminer/issues/152 + # + p <- fun() + print(p) # hide "null device 1" in garbage garbage <- dev.off()