cool_survival <- function(data = data_frame, time = time, censor = censor, group = group){ require(survival) require(ggfortify) fit <- survfit(Surv(time, censor) ~ group, data = data) plot_data <- fortify(fit, surv.connect = TRUE) plot <- ggplot(plot_data, aes(x = time, y = surv * 100)) + # plot the survival curve(s) geom_step(aes(colour = strata), size = 2) + # add the censor indicators geom_point(data = subset(plot_data, n.censor > 0), shape = 3, size = 3, stroke = 2, aes(colour = strata)) + # add confidence intervals using the stepribbon geom in the RcmdrPlugin.KMggplot2 package RcmdrPlugin.KMggplot2::geom_stepribbon(data = plot_data, aes(x = time, ymin = lower * 100, ymax = upper * 100, fill = strata), alpha = 0.15, colour = "transparent", show.legend = FALSE, kmplot = TRUE) + # set the colours of the confidence intervals scale_fill_manual(values = c("steelblue", "darkorange")) + # set the colours of the survival curves scale_colour_manual(values = c("steelblue", "darkorange"), name = "Hip") + # apply a theme based on the Roboto font size 16 theme_bw(base_size = 16, base_family = "Roboto") + # add a title, x label, y label and footnote labs(title = "Custom Made Survival Curve", subtitle = "with ggfortify and ggplot2", x = "Time [years]", y = "Survival Probability [%]", caption = "countcool.com") + # set the caption to a smaller italic font in darkolivergreen colour theme(plot.caption = element_text(colour = "darkolivegreen", face = "italic", size = 10)) + # set the y axis limit in steps of 20 % scale_y_continuous(breaks = seq(0, 100, 20)) + # set the x axis in steps of 2 years scale_x_continuous(breaks = seq(0, max(plot_data$time) + 2, 2)) + # add numbers at risk when failed (label creates a border) annotate(geom = "label", x = subset(plot_data$time, plot_data$n.censor == 0), y = -15, label = subset(plot_data$n.risk, plot_data$n.censor == 0)) + annotate(geom = "text", x = 0.2, y = -8, label = "At Risk", fontface = "italic") return(plot) }