Merge branch 'plot-function'

* plot-function:
  utils: Hide garbage output from dev.off()
  survival: Replace manual plotting with sma_plot_file
  Introduce plot to file function
This commit is contained in:
Jens Sauer 2020-11-03 16:57:42 +01:00
commit fdd17c0c71
2 changed files with 73 additions and 18 deletions

View File

@ -4,6 +4,8 @@
# Jens Mathis Sauer (c) 2020
library(survival)
source("utils.R")
secmal <- read.csv2("current.csv", header=TRUE)
# Setup survival object
@ -12,28 +14,52 @@ surv_asct <- Surv(time = secmal$event_time_asct, event = secmal$event_status)
# plot survival after diagnosis
# scaled to years
png(filename = "survival_dx.png", width = 3000, height = 3000, res = 300)
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")
dev.off()
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")
# One graph per sex
png(filename = "survival_dx_sex.png", width = 3000, height = 3000, res = 300)
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)
dev.off()
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 diagnosis
# scaled to years
png(file = "survival_asct.png", width = 3000, height = 3000, res = 300)
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")
dev.off()
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")
# One graph per sex
png(filename = "survival_asct_sex.png", width = 3000, height = 3000, res = 300)
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)
dev.off()
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)

29
utils.R Normal file
View File

@ -0,0 +1,29 @@
# secMalASCT utilities
#
# License: GPL version 3
# Jens Mathis Sauer (c) 2020
#
# Write plot to filename
#
# This will plot "fun" with arguments "..." as filetype "ftype" and
# the output to "filename". Title and legends can be passed as list,
# 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, ...) {
# Open file for writing
ftype(filename = fname, width = 3000, height = 3000, res = 300)
# run "fun" with passed arguments
do.call(fun, list(...))
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()
}