0

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

See also this question with long labels

1 Answers1

0

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

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • There is a graphical issue that wasn't apparent until I made the labels longer. See revised question. – Gordon Lemmon Dec 16 '22 at 00:18
  • @GordonLemmon that's twice you have accepted an answer then un-accepted it because you came across a new issue that was different from the question you asked. I can see a way to solve this new issue, but I'm a bit reluctant to amend my answer to this question; if you have a new issue, you should raise a new question. – Allan Cameron Dec 16 '22 at 09:21
  • @AllenCameron - I didn't think it made sense to have two questions that solve the same problem, i.e. adding text to the left of the graph while not clipping the numbers at the right of the risk table. If someone reads the answer to the 'short label' question, they would definitely want to see the answer to the long label question. – Gordon Lemmon Dec 16 '22 at 16:09
  • @GordonLemmon the point is that there is always a difference between the answer to the question-as-it-is-asked and the question-with-new-conditions. The particular layout you are looking for doesn't exist in survminer, and any answer will require an ad-hoc change to the survminer plot that is strictly dependent on the data and the labels. Changing these after asking a question and accepting the answer means your question has changed. I hope you can understand why this is a bit frustrating, and makes me wonder if you will move the goalposts again if I invest my free time changing the answer. – Allan Cameron Dec 16 '22 at 16:26
  • I can revert my changes and make a new question – Gordon Lemmon Dec 16 '22 at 16:53