kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
509 stars 164 forks source link

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

Open nolankm opened 8 months ago

nolankm commented 8 months ago

Improvements to alignment of risk table for various combinations of risk.table.pos, axes.offset, and whether or not xlim is specified.

Summary of changes:

  1. ggsurvplot_core()

    • Added x-axis limits to the attr of the ggsurv object, which will be used by the .put_risktable_in_survplot function
    • Added risk.table.pos to the pms object, which will be used by the .plot_survtable function
  2. .put_risktable_in_survplot()

    • This function now has access to the x axis limits via the attr added in (1) above
    • Where axes.offset=TRUE, xmin and xmax now allow for the default 5% axis expansion used in scale_x_continuous.
    • Where axes.offset=FALSE, xmin and xmax are set without this 5% expansion
    • Additionally where axes.offset=FALSE, minor improvement applied to the vertical position of the risk table values via change to ymin and ymax
  3. .plot_survtable()

    • This function now has access to the risk.table.pos value
    • Where axes.offset=FALSE, the "offset" from the origin is set to zero to align values with the y-axis.
    • clip='off' prevents the first value on the risk table being cropped
    • Where risk.table.pos="out" and axes.offset=FALSE, theme elements are updated to (i) make a margin between the strata labels and the first risk table value, and (ii) to remove elements that would obscure the risk table value such as the y-axis, y ticks, and border.
# Compare before and after
fit <- survfit(Surv(time, status) ~ sex, data = lung)

# Example 1 
ggsurvplot(fit,
           data = lung,
           break.time.by = 100,
           risk.table = TRUE,
           risk.table.pos = "out",
           axes.offset=F,
           xlim=c(0,1020),
           ggtheme = theme_classic())

# Example 2
ggsurvplot(fit,
           data = lung,
           break.time.by = 100,
           risk.table = TRUE,
           risk.table.pos = "in",
           xlim=c(0,520),
           ggtheme = theme_classic())

# Example 3
ggsurvplot(fit,
           data = lung,
           break.time.by = 100,
           risk.table = TRUE,
           risk.table.pos = "in",
           axes.offset=F,
           xlim=c(0,1020),
           ggtheme = theme_classic())

Results example 1: image

Results example 2: image

Results example 3: image