diff --git a/R/ggsurvplot_core.R b/R/ggsurvplot_core.R index 102dcf7..27ecab0 100644 --- a/R/ggsurvplot_core.R +++ b/R/ggsurvplot_core.R @@ -170,6 +170,7 @@ ggsurvplot_core <- function(fit, data = NULL, fun = NULL, pms$y.text.col <- risk.table.y.text.col pms$fontsize <- risk.table.fontsize pms$survtable <- "risk.table" + pms$risk.table.pos <- risk.table.pos # color risk.table ticks by strata if(risk.table.y.text.col) pms$y.text.col <- scurve_cols res$table <- risktable <- do.call(ggsurvtable, pms) @@ -241,6 +242,10 @@ ggsurvplot_core <- function(fit, data = NULL, fun = NULL, cumevents = cumevents.y.text.col, cumcensor = cumcensor.y.text.col ) + x.axis.limits <- list( + x.axis.min = xlim[1], + x.axis.max = xlim[2] + ) # Returning the data used to generate the survival plots res$data.survplot <- d @@ -255,6 +260,7 @@ ggsurvplot_core <- function(fit, data = NULL, fun = NULL, attr(res, "cumcensor") <- cumcensor attr(res, "risk.table.pos") <- risk.table.pos attr(res, "axes.offset") <- axes.offset + attr(res, "x.axis.limits") <- x.axis.limits res } @@ -462,14 +468,24 @@ ggsurvplot_core <- function(fit, data = NULL, fun = NULL, .time <- survplot$data$time ymax <- nstrata*0.05 ymin <- -0.05 - xmin <- -max(.time)/20 - - if(!axes.offset){ - ymin <- -0.02 - xmin <- -max(.time)/50 + x.axis.limits <- attr(ggsurv, "x.axis.limits") + xlim.lower <- x.axis.limits$x.axis.min + xlim.upper <- x.axis.limits$x.axis.max + expansion.lower <- (xlim.upper-xlim.lower)*0.05 + expansion.upper <- (xlim.upper-xlim.lower)*0.05 + + if (axes.offset) { + xmin <- xlim.lower-expansion.lower + xmax <- xlim.upper+expansion.upper + } + else if(!axes.offset){ + ymin <- 0 + ymax <- nstrata*0.06 + xmin <- xlim.lower + xmax <- xlim.upper } risktable_grob = ggplotGrob(risktable) - survplot <- survplot + annotation_custom(grob = risktable_grob, xmin = xmin, + survplot <- survplot + annotation_custom(grob = risktable_grob, xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax) ggsurv$plot <- survplot ggsurv$table <- NULL diff --git a/R/ggsurvtable.R b/R/ggsurvtable.R index d5a8908..6a3617d 100644 --- a/R/ggsurvtable.R +++ b/R/ggsurvtable.R @@ -179,6 +179,7 @@ ggsurvtable <- function (fit, data = NULL, survtable = c("cumevents", "cumcenso # Helper function to plot a specific survival table .plot_survtable <- function (survsummary, times, survtable = c("cumevents", "risk.table", "cumcensor"), risk.table.type = c("absolute", "percentage", "abs_pct", "nrisk_cumcensor", "nrisk_cumevents"), + risk.table.pos = risk.table.pos, color = "black", palette = NULL, xlim = NULL, xscale = 1, title = NULL, xlab = "Time", ylab = "Strata", @@ -193,6 +194,7 @@ ggsurvtable <- function (fit, data = NULL, survtable = c("cumevents", "cumcenso survtable <- match.arg(survtable) risk.table.type <- match.arg(risk.table.type) + risk.table.pos <- match.arg(risk.table.pos) # Defining plot title #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: @@ -280,12 +282,16 @@ ggsurvtable <- function (fit, data = NULL, survtable = c("cumevents", "cumcenso # Plotting survival table #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: .expand <- ggplot2::waiver() - # Tables labels Offset from origing + # Set first risk table value offset from origin and set y-axis margin size + risktable.ytick.rmargin <- 0 if(!axes.offset){ .expand <- c(0,0) - offset <- max(xlim)/30 + offset <- 0 survsummary <- survsummary %>% dplyr::mutate(time = ifelse(time == 0, offset, time)) + + if (risk.table.pos=="out") + risktable.ytick.rmargin <- 15 } p <- ggplot(survsummary, mapping) + @@ -293,7 +299,7 @@ ggsurvtable <- function (fit, data = NULL, survtable = c("cumevents", "cumcenso ggpubr::geom_exec(geom_text, data = survsummary, size = fontsize, color = color, family = font.family) + ggtheme + scale_y_discrete(breaks = as.character(levels(survsummary$strata)),labels = yticklabs ) + - coord_cartesian(xlim = xlim) + + coord_cartesian(xlim = xlim, clip='off') + labs(title = title, x = xlab, y = ylab, color = legend.title, shape = legend.title) if (survtable == "risk.table") @@ -309,6 +315,17 @@ ggsurvtable <- function (fit, data = NULL, survtable = c("cumevents", "cumcenso trans = "log10", labels = xticklabels) p <- p + tables.theme + + # Apply y-axis margin and prevent lines being drawn on first risk table value + if (risk.table.pos=="out" & !axes.offset) { + p <- p + + theme( + axis.text.y = element_text(hjust = 0, margin = margin(r=risktable.ytick.rmargin)), + axis.ticks.y = element_blank(), + axis.line.y = element_blank(), + panel.border = element_blank() + ) + } if(!y.text) { p <- .set_large_dash_as_ytext(p)