math1234567890
math1234567890

Reputation: 11

How to change the color of the group names in the risk table to be all black in R survminer package using ggsurvplot?

I am working on a survival plot using the survminer package and ggsurvplot. I am using the option to get a risk table (i.e. risk_table = T). The risk table immediately outputs with rows for each group in my survival plot. Each label for the risk table is the color of the group in the survival plot. I want the entirety of the risk table to be black, even the names of the groups for each row in the risk table. Is there an easy way to do this?

I tried to do something like the following:

surv_pfs=ggsurvplot(data = data, fit=fit_os,xlab="Time in Months",
                     risk.table=T, size=0.2, censor.size=3,
                     legend.title="Group", tables.theme=clean_theme(),
                      title = "OS by group",
                      ylab = "Survival Probability",
                      break.time.by = 12,
                      risk.table.title="Number at Risk",risk.table.col="black", risk.table.height=0.2,
                      risk.table.y.text = T,  
                      censor.shape="l", linetype = c(1,1,1,1),  pval = F,
                      ggtheme = custom_theme(),
                      legend.labs=c("group1","group2", "group3", "group4"),
                      palette = c("black", "#8c8c8c", "#4b4b4b", "#bebebe"))
surv_pfs
surv_pfs$plot = surv_pfs$plot + theme(legend.position = c(0.5,0.7),
                                        legend.text = element_text(size=8),
                                        legend.title = element_text(size=8)) 

**surv_pfs$table=surv_pfs$table+theme(plot.title=element_text(hjust=0,size=14),
                                      plot.margin=margin(l=0),
                                      axis.text.y = element_text(color = "black", size=14)
                                    )**

where the bold part is my attempt to make the number at risk table have all black rows for group1, 2, 3, and 4 instead of the default text color by group. I get an error saying "problem merging the 'axis.text.y' theme element.

Without the error, what I get looks like the following: enter image description here

What I want is the G1, G2, G3, and G4 to all be black text.

Upvotes: 0

Views: 790

Answers (2)

math1234567890
math1234567890

Reputation: 11

This works instead:

surv_pfs$table$theme$axis.text.y$colour <- "black"

Upvotes: 0

stefan
stefan

Reputation: 124183

The issue is that ggsurvplot uses ggtext::element_markdown for the axis.text under the hood. That's the reason why you an error when using element_text.

Using a minimal reproducible example based on the lung dataset:

library(survminer)
library(survival)

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

surv_pfs <- ggsurvplot(fit,
  data = lung, xlab = "Time in Months",
  risk.table = T, size = 0.2, censor.size = 3,
  legend.title = "Group", tables.theme = clean_theme(),
  title = "OS by group",
  ylab = "Survival Probability",
  break.time.by = 12,
  risk.table.title = "Number at Risk", risk.table.col = "black", risk.table.height = 0.2,
  risk.table.y.text = T,
  censor.shape = "l", linetype = c(1, 1, 1, 1), pval = F,
  legend.labs = c("group1", "group2"),
  palette = c("black", "#8c8c8c", "#4b4b4b", "#bebebe")
)

surv_pfs$plot <- surv_pfs$plot + theme(
  legend.position = c(0.5, 0.7),
  legend.text = element_text(size = 8),
  legend.title = element_text(size = 8)
)

surv_pfs$table <- surv_pfs$table + theme(
  plot.title = element_text(hjust = 0, size = 14),
  plot.margin = margin(l = 0),
  axis.text.y = ggtext::element_markdown(color = "black", size = 14)
)

surv_pfs

Upvotes: 0

Related Questions