Mark Danese
Mark Danese

Reputation: 2481

Set different x-axis breaks for plot and table in ggsurvplot (survminer package in R)

I can set different x-axis breaks in ggplot and survminer::ggsurvplot. I am currently setting this by using break.time.by = 365.25 / 12 (monthly). However, I need to set axis labels for the table every 12 months (to meet formatting requirements that I can't change). I tried formatting just the table component of the overall plot and using scale_x_continuous(breaks = c(values here)) but that didn't work. I get a message saying that Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

Upvotes: 0

Views: 1980

Answers (2)

boram
boram

Reputation: 1

There is an argument called "break.time.by".

Upvotes: 0

Allan Cameron
Allan Cameron

Reputation: 173793

You can substitute a scale in the object created by ggsurvplot, which is a list-like object containing two ggplot objects (among other things). One of these ggplot objects is called table. You can either add a second scale_x_continuous to that (and get the warning about adding a second scale), or just over-write the original.

As an example, we'll use one of the built in data sets from the survival package:

library(survival)
library(survminer)

fit3 <- survfit( Surv(time, status) ~ sex, data = colon)

ggsurv <- ggsurvplot(fit3, data = colon,
  fun = "cumhaz", conf.int = TRUE,
  risk.table = TRUE, risk.table.col="strata",
  ggtheme = theme_bw())

ggsurv

We can find out which scale object is the x-scale by doing:

x <- which(ggsurv$table$scales$find("x"))

And over-write it with our own scale. For example, we can make breaks every 200 rather than every 1000 units:

ggsurv$table$scales$scales[[x]] <- scale_x_continuous(breaks = 0:15 * 200)

And our plot is now as we want it, with the new scale in the table's x axis:

ggsurv

Created on 2022-01-26 by the reprex package (v2.0.1)

Upvotes: 2

Related Questions