I want to add survival percentages at the end of the curves. Doing so leads to the risk table not lining up with the graph (the graph width is reduced relative to the risk table width).
Code to reproduce the graph:
library(survival)
library(survminer)
fit<- survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit, data = lung,
risk.table = TRUE,
tables.height = 0.2,
ggtheme = theme_bw() # Change ggplot2 theme
)
time_cutoff = 600
survs = summary(fit, times=time_cutoff)$surv
labels = paste(round(survs*100), '%', sep='')
my_plot$plot <- my_plot$plot + coord_cartesian(ylim=c(0,1), xlim = c(0,time_cutoff), clip = 'on', expand=FALSE)
my_plot$plot <- my_plot$plot + scale_y_continuous(name=NULL, sec.axis=sec_axis(~., name=NULL, breaks = survs, labels= labels))
my_plot
Thanks to Allen Cameron's answer for getting me 95% to the answer. Adding to his response, I have to modify the table the same way we modified the survival graph:
library(survival)
library(survminer)
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# Customized survival curves
my_plot = ggsurvplot(fit, data = lung,
# Add p-value and tervals
risk.table = TRUE,
tables.height = 0.2,
ggtheme = theme_bw() # Change ggplot2 theme
)
time_cutoff = 600
survs = summary(fit, times=time_cutoff)$surv
labels = paste(round(survs*100), '%', sep='')
my_plot$plot <- my_plot$plot + coord_cartesian(ylim=c(0,1), xlim = c(-50,time_cutoff), clip = 'on', expand=FALSE)
my_plot$plot <- my_plot$plot + scale_y_continuous(name=NULL, sec.axis=sec_axis(~., name=NULL, breaks = survs, labels= labels))
table_ylim = ggplot_build(my_plot$table)$layout$panel_params[[1]]$y.range
my_plot$table <- my_plot$table + coord_cartesian(ylim=table_ylim, xlim = c(-50,time_cutoff), clip = 'on', expand=FALSE)
library(patchwork)
(my_plot$plot / my_plot$table) + plot_layout(heights = c(3,1))