Home > Enterprise >  Survminer - ggforest, how to change variable names?
Survminer - ggforest, how to change variable names?

Time:08-01

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

CodePudding user response:

Based on this GitHub issue it seems not possible to change the printed variables' names in a ggforest without changing the variable names. They suggest to use the labelled package to label the variable names with the forestmodel. One problem here is that changing the names only works for numerical variables. I don't have your data, so I will give a reproducible example using the colon dataset:

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)) 
#> Warning in recalculate_width_panels(panel_positions, mapped_text =
#> mapped_text, : Unable to resize forest panel to be smaller than its heading;
#> consider a smaller text size

Created on 2022-07-31 by the reprex package (v2.0.1)

Check this conversation for extra info.

  • Related