Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Risk table alignment modifications - possible fix for #302 and #448 #645

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions R/ggsurvplot_core.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions R/ggsurvtable.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Expand Down Expand Up @@ -280,20 +282,24 @@ 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) +
scale_shape_manual(values = 1:length(levels(survsummary$strata)))+
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")
Expand All @@ -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)
Expand Down