rggsurvfit

Using ggcuminc, how can I format my risk table with commas?


Reproducible example below. I cannot figure out out to format the numbers in my risktable with commas, even using scales::comma.

library(ggplot2)
library(survival)
library(tidycmprsk)
library(ggsurvfit)
library(scales)

time<-rep(1:10, times=300)
status<-rep(0:2, times=1000)
status <- factor(status, levels=c(0,1,2))
df<-data.frame(time, status)

fit <- cuminc(Surv(time, status) ~ 1, df) 

fit %>% 
  ggcuminc() +
  labs(x = "Years",
       y = "Probability") +
  add_risktable(risktable_stats=c("n.risk") )

I'm curious if the answer lies in the risktable themes.


Solution

  • ggsurvfit supports glue syntax, i.e. you can format the stats by wrapping in {}, e.g to format your numbers with commas you could do "{scales::comma(n.risk)}". However, to still get n.risk as the row label you have to set it manually using stats_label=

    library(ggplot2)
    library(survival)
    library(tidycmprsk)
    library(ggsurvfit)
    library(scales)
    
    time <- rep(1:10, times = 300)
    status <- rep(0:2, times = 1000)
    status <- factor(status, levels = c(0, 1, 2))
    df <- data.frame(time, status)
    
    fit <- cuminc(Surv(time, status) ~ 1, df)
    
    fit %>%
      ggcuminc() +
      labs(
        x = "Years",
        y = "Probability"
      ) +
      add_risktable(
        risktable_stats = c("{scales::comma(n.risk)}"),
        stats_label = c("n.risk")
      )