簡體   English   中英

如何將 Kaplan Meier 和 geom_line 分組在一個帶有線型的圖例下?

[英]How do I group a Kaplan Meier and a geom_line under one legend with linetype?

我正在嘗試使用一些參數生存估計來 plot 一個 KM。 我必須將 plot 保持為黑白,因此我無法對線條進行顏色編碼,但是,當我在 geom_line 中的aes function 中從color=key更改為geom_line linetype=key時,我得到了兩個名為“1”和“地層”,如下圖所示。 我還想知道是否可以將 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

事實證明,通過使用 ggsurvplot call. (I don't like having the legend label "in the middle".) working out how to get it right, may need looking at the guts of call. (I don't like having the legend label "in the middle".) working out how to get it right, may need looking at the guts of內容,但我很高興被告知其他情況。

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM