rggplot2ggsurvfit

Change the x-axis breaks on the plot, without affecting the risktable?


I am using the ggsurvfit package to produce a cumulative risk plot with an adjacent risk table.

However, I am struggling to get the plot to show x-axis breaks with labels that don't need to be shown in the risk table part of the plot.

In the following reprex example, I have defined that I am interested in the following x-axis breaks / time points breaks = c(1, 3, 5):

library(ggplot2)
library(ggsurvfit)
library(gtsummary)

plot <-
  survfit2(Surv(time, status) ~ surg, data = df_colon) %>%
  ggsurvfit(type = "risk", linewidth = 0.8) +
  add_confidence_interval() +
  scale_ggsurvfit(
    x_scales = list(
      breaks = c(1, 3, 5)
    )
  ) +
  add_risktable(
    risktable_stats = "{style_number(cum.event, big.mark = ',')} ({sprintf('%.1f', estimate*100)}%; {sprintf('%.1f', conf.low*100)}-{sprintf('%.1f', conf.high*100)}%)",
    stats_label = c("Time [n] (%; 95% Confidence Interval)"),
    )

plot

enter image description here

However, I would like the x-axis of the actual cumulative risk plot to keep the x-axis tick labels for all numbers between 1:10, which I have tried by adding the scale_x_continuous(breaks = seq(1, 10, 1))

plot2 <-
  plot +
  scale_x_continuous(breaks = seq(1, 10, 1))

plot2

enter image description here

The above adds all the specified breaks. However, it also adds these to the risktable. As obvious from both plots, and particular the second one. There is not enough space for including these data for all x-axis ticks.

How can I change the x-axis breaks on the plot, without affecting the risktable?


Solution

  • You can set the breaks= for the risk table individually using the times= argument:

    library(ggplot2)
    library(ggsurvfit)
    library(gtsummary)
    
    survfit2(Surv(time, status) ~ surg, data = df_colon) %>%
      ggsurvfit(type = "risk", linewidth = 0.8) +
      add_confidence_interval() +
      scale_x_continuous(breaks = seq(1, 10, 1)) +
      add_risktable(
        times = c(1, 3, 5),
        risktable_stats = "{style_number(cum.event, big.mark = ',')} ({sprintf('%.1f', estimate*100)}%; {sprintf('%.1f', conf.low*100)}-{sprintf('%.1f', conf.high*100)}%)",
        stats_label = c("Time [n] (%; 95% Confidence Interval)"),
      )