2

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.

Tdebeus
  • 1,519
  • 5
  • 21
  • 43
  • Does the following help: remove the `xlim ` argument and add `+ coord_cartesian(xlim=c(0, 60))`? – KoenV Oct 26 '21 at 14:05
  • Unfortunately, that gives this `error: Error in .apply_surv_func(df, fun = fun) : Invalid 'fun' argument` – Tdebeus Oct 26 '21 at 14:08
  • You don't provide reproducible example, which complicates things, but we try anyway. If you save the ggsurv plot in e.g. `p1`, then you can access/manipulate the plot via `p1$plot`. Maybe the following works: `p2 <- p1$plot`, and then `p2 + coord_cartesian(xlim=c(0,60))` – KoenV Oct 26 '21 at 14:16
  • Alas, the `p2 + coord_cartesian(xlim=c(0,60))` also cuts off the higher x-axis values and removes the 95%CI. With the following comment in the console: `Coordinate system already present. Adding new coordinate system, which will replace the existing one.` – Tdebeus Oct 26 '21 at 14:28
  • That is only a warning. and it seems that this time the plot "accepts" the coord_cartesion() change. I will try to work out something with "known" data sets. That may take some time.... – KoenV Oct 26 '21 at 14:31
  • I will try to add the data today, thanks Koen – Tdebeus Oct 28 '21 at 09:35
  • Hi @KoenV I've an example dataset (added now in the question) in a Google sheet hope this helps you helping me Thanks a lot. https://docs.google.com/spreadsheets/d/1B11gN49W5l8CAoAmomAIH-lgJLDomsCVOL6GZcet1gM/edit?usp=sharing – Tdebeus Oct 28 '21 at 12:00
  • Hi @tdebeus, I was able to reproduce your graph, and finally I found a work around by saving pieces of the original graph, changing one part of it and putting it back together with `ggarrange`. Please have a look. – KoenV Oct 29 '21 at 14:33

1 Answers1

2

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.

KoenV
  • 4,113
  • 2
  • 23
  • 38