Survminer - ggforest,如何更改变量名?

z31licg0  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(170)

大家好,谢谢你们的帮助。我正在用Survminer软件包创建一个考克斯模型的森林图,但是,我一直在尝试更改显示变量的标签。有没有方法可以在不更改变量名称的情况下更改显示的标签?我如何更新字符串来更改名称?

ggforest<-ggforest(coxph, data=data)

如果我尝试使用legend.labs函数,就会出现此错误

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"))

你能帮我换一下这些标签吗?
再次谢谢你

ssm49v7z

ssm49v7z1#

基于这个GitHub issue,似乎不可能在ggforest中更改打印变量的名称而不更改变量名称。他们建议使用labelled包来用forestmodel标记变量名称。这里的一个问题是更改名称只适用于数值变量。我没有你的数据,我将使用colon数据集给予一个可重现的示例:

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

reprex package(v2.0.1)于2022年7月31日创建
检查此对话以获取额外信息。

ttisahbt

ttisahbt2#

以下是基于@Quintens答案的解决方案:

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))

创建于2023年1月24日,使用reprex v2.0.2

相关问题