ggsurvplot:有风险的图例数字

问题描述 投票:0回答:1

我用 ggsurvplot 生成了以下寿命曲线:

但是,我无法为处于风险的数字图例显示与曲线相同的颜色。另外,您知道是否可以显示线条而不是处于风险的数字的图例(实线或点线)?

非常感谢您的帮助!

下面是我用来获取结果的代码:


subjid <- 1:n

status_os <- sample(c(0,1), n, replace = TRUE)
time_os <- ifelse(status_os=="vivant", rnorm(n,mean=365, sd=30), rnorm(n, mean=180, sd=30))

groupe <- sample(c(1,2), n, replace=TRUE)
mutation <- sample(c(1, 0), n, replace=TRUE)

data <- data.frame(Identifiant=subjid, status_os=status_os, time_os=time_os, groupe=groupe, mutation=mutation)

fit <- survfit(Surv(time_os, status_os)~groupe+mutation, data=data)

p <- ggsurvplot(fit, data = data, break.time.by = 12, risk.table = "nrisk_cumevents",
                legend.title = "", legend = c(0.2, 0.2), linetype = c("strata"),
                risk.table.y.text=TRUE, break.x.by = 50)

colors <-  rep(c("red", "red","blue","blue"))
lines <-  rep(c("solid","dashed","solid", "dashed"))

p$plot <- p$plot + scale_linetype_manual(values = lines) +
  scale_colour_manual(values = colors)

p$plot <- p$plot / p$table + plot_layout(ncol = 1, heights = c(2, 1))
r survival-analysis
1个回答
0
投票

您所说的“图例”是表格图的轴文本。由于轴集的样式是通过

theme()
设置的,设置颜色的一个选项是通过
color=
axis.text.y
参数,它使用
ggtext::element_markdown
而不是
element_text

注意:我添加了缺失的部分以使您的示例和数据可重现。

library(survival)
library(survminer)

p <- ggsurvplot(fit,
  data = data, break.time.by = 12, 
  risk.table = "nrisk_cumevents",
  legend = c(0.2, 0.2), 
  linetype = c("strata"),
  risk.table.y.text = TRUE, 
  break.x.by = 50,
  tables.height = 1/3
)

colors <- rep(c("red", "red", "blue", "blue"))
lines <- rep(c("solid", "dashed", "solid", "dashed"))

p$plot <- p$plot + 
  theme(
    legend.position = c(0.05, 0.05),
    legend.justification = c(0.05, 0.05)
  ) +
  scale_linetype_manual(values = lines) +
  scale_colour_manual(values = colors) +
  labs(color = NULL, linetype = NULL)

p$table <- p$table + 
  theme(
    axis.text.y = ggtext::element_markdown(
      color = rev(colors)
    )
  )

p

数据

set.seed(123)

n <- 100
subjid <- seq(n)

status_os <- sample(c(0, 1), n, replace = TRUE)
time_os <- ifelse(
  status_os == 1,
  rnorm(n, mean = 365, sd = 30),
  rnorm(n, mean = 180, sd = 30)
)

groupe <- sample(c(1, 2), n, replace = TRUE)
mutation <- sample(c(1, 0), n, replace = TRUE)

data <- data.frame(
  Identifiant = subjid,
  status_os = status_os,
  time_os = time_os,
  groupe = groupe,
  mutation = mutation
)

fit <- survfit(Surv(time_os, status_os) ~ groupe + mutation, data = data)
© www.soinside.com 2019 - 2024. All rights reserved.