Tdebeus
Tdebeus

Reputation: 1599

Change xlim to cartesion coordinates in ggsurvplot

I want to create a KM curve with 95%-CI with the x-axis zoomed in to show values between 0-60 months. This all works fine with ggsurvplot untill I use xlim.

ggsurvplot(fitLC, data = KMSCC,
       risk.table = TRUE,
       conf.int=TRUE, 
       pval = TRUE, 
       break.x.by = 12, 
       xlab ="Time in Months",
       ylab="Relative Survival",
       ggtheme = theme_minimal(),
       risk.table.y.text.col = T,
       risk.table.y.text = FALSE)

withoutlimits

ggsurvplot(fitLC, data = KMSCC,
           risk.table = TRUE,
           conf.int=TRUE, 
           pval = TRUE, 
           break.x.by = 12, 
           xlab ="Time in Months", 
           xlim = c(0, 60),
           ylab="Relative Survival",
           ggtheme = theme_minimal(),
           risk.table.y.text.col = T,
           risk.table.y.text = FALSE)

withlimits

Concluding, is there a way to zoom in to the preferred x-axis values without changing the higher x-axis values to NA? See also: https://github.com/kassambara/survminer/issues/4 How can I change xlim mode to Cartesian coordinates?

I can't give the data seen in the plot, but for reproducibility sake here's an example dataset in a Google sheet.

Upvotes: 2

Views: 1637

Answers (1)

KoenV
KoenV

Reputation: 4283

When you zoom in in the plot of the surv_graph, with the ggsurvplot-argument xlim, or with + coord_cartesian(...) afterwards, then the table is automatically adjusted to show only the data in the plot. This may be worthy of a change request for the package. In the meanwhile, the code below may be a workaround.

ggsurvplot() creates an object of 4 lists: one of them contains the graph, another one contains the table. Extracting those 2 and "arranging" them ggarrange() may create a suitable graph. Prior to that ggarrange-operation we "zoom in" on the surv-plot with coord_cartestion(xlim= ...):

### download file from link provided by OP 
### and save it in sink with the code below
lung <- read.csv(file = "../Tdebeus_001.csv", header = TRUE)

library("survival")
library("survminer")
library("ggpubr")       # for ggarrange
    
fitLC  <- survfit(Surv(Time_months, Event) ~ Cohort, data = lung)
    
p1 <- ggsurvplot(fitLC
                 , data = lung
                 , risk.table = TRUE
                 , conf.int=TRUE
                 , pval = TRUE
                 , break.x.by = 12
                 , xlab ="Time in Months"
                 # , xlim = c(0, 60)        ## commented out !
                 , ylab="Relative Survival"
                 , ggtheme = theme_minimal()
                 , risk.table.y.text.col = T
                 , risk.table.y.text = FALSE
                 )

### save parts of the original graph    
surv_plot <- p1$plot
surv_table <- p1$table

### zoom in on the surv_plot    
surv_plot2 <- surv_plot + coord_cartesian(xlim = c(0,60))

### put it back together
ggarrange(surv_plot2, surv_table, ncol = 1, heights = c(3, 1))

This results in the following graph, which may be finetuned with other arguments to ggarrange(): (in the code above, heights gives 3/4 of the graph to the surv_plot).

enter image description here

Please, let me know whether this is what you had in mind.

Upvotes: 2

Related Questions