简体   繁体   English

更改 r 中 ggcompetingrisks 图中的图例标签

[英]Change legend labels in ggcompetingrisks plot in r

I have made a cumulative incidence plot using cmprsk::cuminc and then ggcompetingrisks for plotting.我使用cmprsk::cuminc制作了累积发生率图,然后使用ggcompetingrisks进行绘图。 I want to change legend labels for the group.我想更改组的图例标签。 I've tried with legend.lab = c("A", "B", "C") .我试过legend.lab = c("A", "B", "C") This works when I do a ggsurvplot without competing risk.当我在没有竞争风险的情况下进行ggsurvplot时,这有效。 But it does not work now.但它现在不起作用。 Any suggestions?有什么建议?

My code is like:我的代码是这样的:

    fit <- cuminc(df$Time, df$Event, group = df$genotype)

    p <- ggcompetingrisks(fit, multiple_panels = FALSE, palette = "black",
                          legend.title = c("genotype"),
                          legend.labs = c("A", "B", "C"))

The thing is that the legend show both the Event (0, 1) and the Group (1, 2, 3).问题是图例同时显示了事件 (0, 1) 和组 (1, 2, 3)。 I want only the group to be shown in the legend and I want it to be named A, B and C..我只想在图例中显示该组,我希望将其命名为 A、B 和 C..

Please help me!!请帮我!!

If I get your fit correct, group will be plotted as linetype so you can use scale_linetype_manual() to set the title of the legend, and the names.如果我的拟合正确,组将被绘制为线型,因此您可以使用scale_linetype_manual()设置图例的标题和名称。 To turn of the other legend about event, you can use guides()要关闭有关事件的其他图例,您可以使用 guides()

See below for an example using some simulated data:请参阅以下使用一些模拟数据的示例:

library(survminer)
library(ggplot2)
library(cmprsk)

df = data.frame(
Time = rexp(100),
genotype = factor(sample(1:3,100,replace=TRUE)),
Event = factor(sample(0:1,100,replace=TRUE),0:1,c('no event', 'death'))
)

fit <- cuminc(df$Time, df$Event, group = df$genotype)

ggcompetingrisks(fit)
p <- ggcompetingrisks(fit, multiple_panels = FALSE)

p + scale_linetype_manual(name="genotype",values=1:3,labels=c("A","B","C"))+
guides(col="none")

Please do dput(df) and paste the output as part of your question if the df I have above is different from yours.如果我上面的 df 与您的不同,请执行 dput(df) 并将输出粘贴为您的问题的一部分。 This way others can help as well.这样其他人也可以提供帮助。

If you need thicker lines, do:如果您需要更粗的线条,请执行以下操作:

old_geom <- ggplot2:::check_subclass("line", "Geom")$default_aes
update_geom_defaults("line", list(size = 1.5))
p + scale_linetype_manual(name="genotype",values=1:3,labels=c("A","B","C"))+
    guides(col="none")
update_geom_defaults("line", list(size = old_geom$size))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM