Home > Blockchain >  Stop clipping ggsurvplot risk table text but keep hard xlim clipping for graph
Stop clipping ggsurvplot risk table text but keep hard xlim clipping for graph

Time:12-14

I want to specify the end time in my ggsurvplot, annotating the ends of the survival curves as shown below. But this leads to a truncation of the rightmost text in number-at-risk table. See code and output graphic below. Note that in the graphic, the rightmost values are 20 and 21, but all you see is 2 and 2.

If I increase the right-side x-axis limit (replace xlim=c(-50, time_cutoff) with xlim=c(-50, time_cutoff 50)), the risk table text is not clipped, but the survival curves are also plotted out to time_cutoff 50 and the labels I show to the right of the survival curve do not align with the ends of the survival curves.

library(survival)
library(survminer)
library(patchwork)

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 = 500
xticks = c(0, 250, 500)
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)  
                #scale_x_continuous(name=NULL, breaks = xticks)  
                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)#  
                #scale_x_continuous(name=NULL, breaks = xticks)


(my_plot$plot / my_plot$table)   plot_layout(heights = c(3,1))

enter image description here

CodePudding user response:

As I mentioned in the comments in my answer to the previous question, you can turn clipping off and remove the panel border from the table plot:

library(survival)
library(survminer)
library(patchwork)

fit <- survfit(Surv(time, status) ~ sex, data = lung)

my_plot <- ggsurvplot(fit, data = lung,
                     risk.table = TRUE,
                     tables.height = 0.2,
                     ggtheme = theme_bw())

time_cutoff = 500

survs  <- summary(fit, times=time_cutoff)$surv
labels <- paste0(round(survs * 100), '%')

my_plot$plot <- my_plot$plot   
  coord_cartesian(ylim = 0:1, xlim = c(-50, time_cutoff), expand = FALSE)  
  scale_y_continuous(name = NULL, 
                     sec.axis = sec_axis(~., name = NULL, breaks = survs, 
                                         labels = labels)) 

my_plot$table <- my_plot$table   
  coord_cartesian(ylim = table_ylim, xlim = c(-50, time_cutoff), 
                  clip = 'off', expand = FALSE)  
  theme(panel.border = element_blank())

(my_plot$plot / my_plot$table)   plot_layout(heights = c(3, 1))

enter image description here

  • Related