rggplot2survminer

ggsurvplot - risk table fontsize


How to adjust the fontsize of the risk.table?
I want the fontsize of the numbers at risk to be 20.
Not the title of the risk.table.

library(survminer)
library(survival)

data(lung)
fit <- survfit(Surv(time, status) ~ sex, data = lung)

p <- ggsurvplot(
  fit,  
  size = 1,  
  legend.labs = c("A", "B"),
  ylim = c(0, 1),
  linetype = "strata", 
  break.time.by = 365, 
  palette = c("#E7B800", "#2E9FDF"), 
  risk.table = TRUE,
  risk.table.title = "No. at risk",
  risk.table.height = 0.2,
  tables.theme = theme_cleantable() + 
    theme(plot.title = element_text(size = 40))
)
p$plot <- p$plot +
  theme(
    text = element_text(size = 20),  
    axis.text.y = element_text(size = 20),
    axis.text.x = element_text(size = 20),
    axis.text = element_text(size = 20), 
    legend.text = element_text(size = 20)  
  )

I am aware of the risk.table.fontsize = argument but when I specify 20 it looks like this: enter image description here

Using risk.table.fontsize = 7 looks somewhat the same. But this is an odd solution via eyeballing. enter image description here


Solution

  • The issue is that in ggplot2 the size of an element_text is measured in pts whereas the font size of a geom_text or geom_label (which are used for the risk table) are measured in mm (see here). For this reason ggplot2 provides a helper constant .pt to convert from points to mm, i.e. use 20 / .pt (which approximately equals 7) to get identical font sizes for the risk table and the non-data ink:

    library(survminer)
    library(survival)
    
    fit <- survfit(Surv(time, status) ~ sex, data = lung)
    
    p <- ggsurvplot(
      fit,
      size = 1,
      legend.labs = c("A", "B"),
      ylim = c(0, 1),
      linetype = "strata",
      break.time.by = 365,
      palette = c("#E7B800", "#2E9FDF"),
      risk.table = TRUE,
      risk.table.title = "No. at risk",
      risk.table.height = 0.2,
      risk.table.fontsize = 20 / .pt,
      tables.theme = theme_cleantable() +
        theme(plot.title = element_text(size = 40))
    )
    p$plot <- p$plot +
      theme(
        text = element_text(size = 20),
        axis.text.y = element_text(size = 20),
        axis.text.x = element_text(size = 20),
        axis.text = element_text(size = 20),
        legend.text = element_text(size = 20)
      )
    
    p
    

    enter image description here