如何在 ggplotsurv Kaplan Meier 上绘制曲线
How do I plot a curve on top of a ggplotsurv Kaplan Meier
我已经在生存对象上使用 ggsurvplot 绘制了 Kaplan Meier,我正在尝试在顶部绘制另一条曲线。该线是平均生存时间的向量。我的 KM 是根据 IPD 估计的,有 225 个观察值,最大生存时间为 7.2 年,我的平均生存时间是根据贝叶斯分析估计的,样本以 6 个月为间隔从后验生存函数中抽取。问题似乎是我试图从平均生存时间绘制的附加线与我的 IPD 数据帧的长度不同,除了将最后的生存时间除以我的 IPD 中的观察次数并以这些间隔从后验分布我想知道这附近是否有任何东西?
我的数据是这样的,
## IPD data
treatment t event
2 1 5.5250 1
3 1 1.9493 1
4 1 4.9473 1
7 1 5.9466 0
11 1 1.5797 1
12 1 0.5038 1
. . .
. . .
. . .
## mean survival times from the Bayesian analysis
1.0000000 0.9129731 0.8337045 0.7614860 0.6956758 0.6356917 .....
我正在尝试的代码,
f <- survfit(Surv(t, event)~1, data=treatment)
f1 <- ggsurvplot(f)
f2 <- f1$plot + geom_line(aes(c(0:16), meansurv))
这样做我得到以下错误,
<error/rlang_error>
Aesthetics must be either length 1 or the same as the data (225): x and y
附加问题,我不确定如何生成只有 1 个图例的图,下面会生成两个,如果我使用 legend="none",两个图例都会被删除。
meansurv1 <- c(1.0000000, 0.9129731, 0.8337045, 0.7614860, 0.6956758, 0.6356917, 0.50, 0.43, 0.37)
meansurv2 <- 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, meansurv1, meansurv2)
temp<- temp %>%
gather(key, value, -c(x))
f1 <- survfit(Surv(t, event)~1, data=control)
f1 <- ggsurvplot(f1, legend="right")
f1 <- f1$plot + geom_line(data = temp, aes(x=x, y=value, group=key, color=key));f1
问题出在 geom_line
的使用上。由于您没有在 geom_line
中指定 data
参数,因此它是 NULL
。在 geom_line
的帮助中指出,如果 data = NULL
inside geom_line
.
,数据是从绘图数据继承的
您可以在 geom_line
中指定 data
参数来解决这个问题。
附件是一个可重现的例子。我采用了你问题中给出的数字,但我调整了向量 meansurv
以便该向量的条目数多于数据帧的行数 treatment
.
首先我重现了你的错误,然后我展示了如何使用函数 geom_line()
的参数 data
正确地做到这一点
library(survival)
library(survminer)
#> Lade nötiges Paket: ggplot2
#> Lade nötiges Paket: ggpubr
# a preview of the data frame.
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)
)
# modified vector:
meansurv <- c(1.0000000, 0.9129731, 0.8337045, 0.7614860, 0.6956758, 0.6356917, 0.50, 0.43, 0.37)
f <- survfit(Surv(t, event)~1, data=treatment)
f1 <- ggsurvplot(f); f1
# data is not used in geom_line ->> raises error:
f2 <- f1$plot + geom_line(aes(seq.int(meansurv) - 1, meansurv)); f2
#> Error: Aesthetics must be either length 1 or the same as the data (7): x and y
# using argument data:
f2 <- f1$plot + geom_line(data = data.frame(x = seq.int(meansurv) - 1,
y = meansurv),
aes(x = x, y = y)); f2
由 reprex package (v0.3.0)
于 2020-07-21 创建
我已经在生存对象上使用 ggsurvplot 绘制了 Kaplan Meier,我正在尝试在顶部绘制另一条曲线。该线是平均生存时间的向量。我的 KM 是根据 IPD 估计的,有 225 个观察值,最大生存时间为 7.2 年,我的平均生存时间是根据贝叶斯分析估计的,样本以 6 个月为间隔从后验生存函数中抽取。问题似乎是我试图从平均生存时间绘制的附加线与我的 IPD 数据帧的长度不同,除了将最后的生存时间除以我的 IPD 中的观察次数并以这些间隔从后验分布我想知道这附近是否有任何东西?
我的数据是这样的,
## IPD data
treatment t event
2 1 5.5250 1
3 1 1.9493 1
4 1 4.9473 1
7 1 5.9466 0
11 1 1.5797 1
12 1 0.5038 1
. . .
. . .
. . .
## mean survival times from the Bayesian analysis
1.0000000 0.9129731 0.8337045 0.7614860 0.6956758 0.6356917 .....
我正在尝试的代码,
f <- survfit(Surv(t, event)~1, data=treatment)
f1 <- ggsurvplot(f)
f2 <- f1$plot + geom_line(aes(c(0:16), meansurv))
这样做我得到以下错误,
<error/rlang_error>
Aesthetics must be either length 1 or the same as the data (225): x and y
附加问题,我不确定如何生成只有 1 个图例的图,下面会生成两个,如果我使用 legend="none",两个图例都会被删除。
meansurv1 <- c(1.0000000, 0.9129731, 0.8337045, 0.7614860, 0.6956758, 0.6356917, 0.50, 0.43, 0.37)
meansurv2 <- 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, meansurv1, meansurv2)
temp<- temp %>%
gather(key, value, -c(x))
f1 <- survfit(Surv(t, event)~1, data=control)
f1 <- ggsurvplot(f1, legend="right")
f1 <- f1$plot + geom_line(data = temp, aes(x=x, y=value, group=key, color=key));f1
问题出在 geom_line
的使用上。由于您没有在 geom_line
中指定 data
参数,因此它是 NULL
。在 geom_line
的帮助中指出,如果 data = NULL
inside geom_line
.
您可以在 geom_line
中指定 data
参数来解决这个问题。
附件是一个可重现的例子。我采用了你问题中给出的数字,但我调整了向量 meansurv
以便该向量的条目数多于数据帧的行数 treatment
.
首先我重现了你的错误,然后我展示了如何使用函数 geom_line()
data
正确地做到这一点
library(survival)
library(survminer)
#> Lade nötiges Paket: ggplot2
#> Lade nötiges Paket: ggpubr
# a preview of the data frame.
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)
)
# modified vector:
meansurv <- c(1.0000000, 0.9129731, 0.8337045, 0.7614860, 0.6956758, 0.6356917, 0.50, 0.43, 0.37)
f <- survfit(Surv(t, event)~1, data=treatment)
f1 <- ggsurvplot(f); f1
# data is not used in geom_line ->> raises error:
f2 <- f1$plot + geom_line(aes(seq.int(meansurv) - 1, meansurv)); f2
#> Error: Aesthetics must be either length 1 or the same as the data (7): x and y
# using argument data:
f2 <- f1$plot + geom_line(data = data.frame(x = seq.int(meansurv) - 1,
y = meansurv),
aes(x = x, y = y)); f2
由 reprex package (v0.3.0)
于 2020-07-21 创建