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.
Asked
Active
Viewed 1,040 times
0

Mark Danese
- 2,331
- 1
- 20
- 25
-
I believe the output of ggsurvplot is a ggplot object. If so, you can add to it a second axis as described here https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales – LulY Jan 26 '22 at 22:00
1 Answers
2
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)

Allan Cameron
- 147,086
- 7
- 49
- 87