【问题标题】:Is there a way to manually change label colors in nomogram drawn with the rms-package?有没有办法手动更改使用 rms-package 绘制的列线图中的标签颜色?
【发布时间】:2020-03-14 22:52:03
【问题描述】:

请在下面找到我的数据样本pp

问题:有没有办法改变下面显示的列线图中圈出的标签文本的颜色?如您所见,我圈出了三个我想手动更改的不同部分。

我没有通过查看r-documentation 找到解决方案

我的列线图:

这是用这段代码制作的

library(rms)
pp$uicc <- factor(pp$uicc,levels=c("1","2","3","4"),labels=c("1","2","3","4"))
pp$rt.kemo <- factor(pp$rt.kemo,levels=c("0","1"),labels=c("0","1"))


d <- datadist(pp)
options(datadist="d")

var.labels <- c(alder = "Age",
                n.fjernet = "LNY",
                uicc = "UICC Stage",
                rt.kemo = "Radiochemotherapy",
                mors = "mors",
                os.neck = "OS")

label(pp) = lapply(names(var.labels),
                   function(x) label(pp[,x]) <- var.labels[x])

a <- cph(Surv(os.neck,mors)~alder+n.fjernet+uicc+rt.kemo,data=pp,surv=TRUE,x=TRUE,y=TRUE)

surv <- Survival(a)  

nom <- nomogram(a, fun=list(function(x) surv(12, x),
                            function(x) surv(36, x),
                            function(x) surv(60, x)),
                funlabel=c("Probability of 1 year survival", 
                           "Probability of 3 years survival",
                           "Probability of 5 years survival"), lp=F)

plot(nom, xfrac=.2, 
     total.points.label="Sum of all points", 
     cex.axis = 1.05,
     force.label = TRUE,
     tcl = 0.8,
     lmgp = 0.1,
     vnames="labels",
     col.grid=gray(c(0.85,0.95)))

我的数据

pp <- structure(list(alder = structure(c(58.53, 51.43, 78.5, 48.44, 
68.61, 58.28, 55.06, 67.33, 86.51, 61.57, 76.98, 63.73, 63.72, 
55.29, 55.34, 60.85, 60.54, 56.13, 76.09, 71.54, 80.24, 81.67, 
59.49, 61.07, 58.28, 60.2, 58.57, 60, 71.95, 40.48), label = c(alder = "Age"), class = c("labelled", 
"numeric")), n.fjernet = structure(c(4L, 27L, 18L, 11L, 14L, 
15L, 9L, 6L, 3L, 16L, 4L, 6L, 10L, 13L, 33L, 16L, 6L, 9L, 15L, 
23L, 5L, 9L, 10L, 8L, 17L, 14L, 13L, 13L, 5L, 9L), label = c(n.fjernet = "LNY"), class = c("labelled", 
"integer")), uicc = structure(c(2L, 2L, 4L, 3L, 3L, 2L, 2L, 2L, 
2L, 4L, 1L, 1L, 2L, 1L, 4L, 2L, 1L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 
1L, 2L, 2L, 3L, 2L, 1L), .Label = c("1", "2", "3", "4"), class = c("labelled", 
"factor"), label = c(uicc = "UICC Stage")), rt.kemo = structure(c(2L, 
2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("0", 
"1"), class = c("labelled", "factor"), label = c(rt.kemo = "Radiochemotherapy")), 
    mors = structure(c(0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 
    0L, 0L, 1L, 1L, 0L), label = c(mors = "mors"), class = c("labelled", 
    "integer")), os.neck = structure(c(77.01, 75.96, 11.5, 74.38, 
    17.02, 7.89, 96.03, 40.48, 17.74, 14.65, 62.46, 12.55, 9.92, 
    26.05, 45.47, 17.38, 39.72, 51.45, 119, 8.61, 117.39, 76.98, 
    115.78, 67.09, 113.74, 113.22, 111.64, 94.79, 72.15, 110.23
    ), label = c(os.neck = "OS"), class = c("labelled", "numeric"
    ))), row.names = c(NA, 30L), class = "data.frame")

【问题讨论】:

    标签: r label survival-analysis cox-regression rms


    【解决方案1】:

    我不认为可以直接通过nomogramplot 完成,但总有修改一个grob:

    library(gridGraphics)
    library(grid)
    plot(nom, xfrac=.2, 
         total.points.label="Sum of all points", 
         cex.axis = 1.05,
         force.label = TRUE,
         tcl = 0.8,
         lmgp = 0.1,
         vnames="labels",
         col.grid=gray(c(0.85,0.95)))
    
    grid.echo()
    a <- grid.grab()
    

    要更改颜色,只需指定所需的标签,按从上到下的顺序编号:

    a$children$`graphics-plot-1-text-1`$gp$col <- "red"
    a$children$`graphics-plot-1-text-2`$gp$col <- "green"
    a$children$`graphics-plot-1-text-7`$gp$col <- "blue"
    
    grid.draw(a)
    

    【讨论】:

    • 嗨@Greg。谢谢,它肯定改变了标签颜色。但是,它也重新排列了轴,这些轴现在重叠并且错位了。尝试在应用您的方法之前和之后比较nom-plot。你知道这是否也可以解决?
    • @cmirian 嗯,我用相同的代码重新绘制了上面的图(并删除了不需要的 recordPlot 调用)。不同之处在于,我第二次在运行 R 3.6.2 的 Windows 10 机器上,而第一次(故障)我在运行 R 3.6.1 的 Mac 上。在 Mac 上,甚至?grid.echo 中的示例最终都被扭曲了。我还在 RStudio 中稍微弄乱了查看器窗口的大小,因为图形对象(如 plot 输出)是直接在设备中绘制的
    • 我在我的 Mac 上运行 R 3.6.2,不幸的是,在grid.draw(a) 之后,图形仍然不成比例。知道为什么 grid.draw() 在 Mac 上无法正常运行吗?太糟糕了,我喜欢这个解决方案。无论如何,非常感谢您,非常感谢您的帮助。
    【解决方案2】:

    最好的办法是加载 https://raw.githubusercontent.com/harrelfe/rms/master/R/plot.nomogram.s 并编辑到本地函数中,例如 plotnom 然后调用 plotnom

    更好的是:从github 分叉并在R 文件和man目录中的帮助文件中添加新选项,然后让我将其合并回主干。

    【讨论】:

    • 嗨@Frank Harrel。非常感谢您的评论,非常感谢。不幸的是,我对此很陌生,我不知道您的建议意味着什么。我很乐意提供帮助,但是我需要一些指导。
    猜你喜欢
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多