【问题标题】:How do I group a Kaplan Meier and a geom_line under one legend with linetype?如何将 Kaplan Meier 和 geom_line 分组在一个带有线型的图例下?
【发布时间】:2020-07-30 17:05:41
【问题描述】:

我正在尝试使用一些参数生存估计来绘制 KM。我必须保持情节黑白,所以我无法对线条进行颜色编码,但是,当我在geom_lineaes 函数中从color=key 更改为linetype=key 时,我得到两个标题为“ 1”和“Strata”,如下图所示。我还想知道是否可以将 KM 的颜色更改为黑色,置信区间更改为灰色。

treatment <- data.frame(
  treatment = c(1, 1, 1, 1, 1, 1), 
  t = c(5.525, 1.9493, 4.9473, 5.9466, 1.5797, 0.5038), 
  event = c(1, 1, 1, 0, 1, 1)
)

tempsurv1 <- c(1.0000000, 0.9129731, 0.8337045, 0.7614860, 0.6956758, 0.6356917, 0.50, 0.43, 0.37)
tempsurv2 <- c(1.0000000, 0.9324888, 0.8671987, 0.8042297, 0.7436717, 0.6856045, 0.6300962, 0.5772029, 0.5269681)
x<- c(0:8)
temp <- data.frame(x, tempsurv1, tempsurv2)

temp<- temp %>% 
  gather(key, value, -c(x))

f2 <- survfit(Surv(t, event)~1, data=treatment)
f2 <- ggsurvplot(f2, legend="right") 
f2 <- f2$plot +  geom_line(data = temp, aes(x=x, y=value, group=key, linetype=key))+
  theme(legend.position="bottom");f2

【问题讨论】:

  • 是的,有可能。您需要给colorlinetype 标度相同的name。我无法运行你的代码,因为你没有给我们control 数据框。
  • 对不起,数据是用来治疗的,我现在已经编辑过了

标签: r ggplot2 survival-analysis


【解决方案1】:

事实证明,通过使用 ggsurvplotcall. (I don't like having the legend label "in the middle".) working out how to get it right, may need looking at the guts ofggsurvplot` 的参数,你几乎可以得到你想要的,但我很高兴被告知不是这样。

f2 <- ggsurvplot(f2, legend="right", palette = c("#AAAAAA"), legend.name="Strata") 
f2 <- f2$plot +  geom_line(data = temp, aes(x=x, y=value, linetype=key))+
  theme_light() +
  theme(legend.position="bottom") +
  scale_linetype_discrete(name="Strata") 
f2

给予

我不确定您所说的“当我从 key=color 更改为 key=linetype 时,我得到两个单独的图例”是什么意思,因为您的代码中都没有出现任何选项。

你需要添加

library(tidyverse)
library(survival)
library(survminer)

在你的代码顶部使其成为 MWE。

【讨论】:

  • 对不起,我在切换 RHS 和 LHS 时没有说清楚,但是 key=linetype 是 geom_line 函数中的最后一个参数。是的,这与我需要的非常接近,我只是希望 KM 与两条 geom_lines 位于同一图例下,换句话说,All 组位于 Strata 的右侧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 2021-06-06
相关资源
最近更新 更多