Search code examples
rsurvival-analysisr-forestplot

Survminer - ggforest, how to change variable names?


Hello to everybody and thank you for your help. I'm managing to build a forest plot of a Cox model with the Survminer package, however, I've got stuck in trying to change the labels of the variables displayed. Is there a way to change the labels displayed without changing variable names? How could I update my string in order to change the names?

ggforest<-ggforest(coxph, data=data)

This image is obtained

If I try to use the legend.labs function I obtain this error

ggforest(coxph, data=data, legend.title = c("Prova"),legend.labs=c("Carried AED","Daytime", "Distance 1","Distance 2", "Distance 3"))
Error in ggforest(coxph, data = data, legend.title = c("Prova"), legend.labs = c("Carried AED",  : 
  unused arguments (legend.title = c("Prova"), legend.labs = c("Carried AED", "Daytime", "Distance 1", "Distance 2", "Distance 3"))

Can you help me with changing these labels?

Thank you again


Solution

  • Here is a workaround based on @Quintens answer:

    library(forestmodel)
    library(survival)
    
    labelled::var_label(colon) <- list(
        sex = "Sex", #this variable is a numeric -> label works
        rx = "RX", # factor -> label doesn't work!
        adhere = "ADHERE" # numeric -> label works...
    )
    
    model <- coxph(Surv(time, status) ~ sex + rx + adhere, data = colon)
    
    print(forest_model(model)) 
    
    

    # this does the trick
    model$xlevels <- unname(model$xlevels)
    
    print(forest_model(model))
    
    

    Created on 2023-01-24 with reprex v2.0.2