在 ggplot 中向生存曲线添加新曲线时出错?
Error adding new curve to survival curve in ggplot?
我想在使用 ggfortify
包中的 autoplot
创建的生存分析图(Kaplan-Meier 曲线)中添加一条新线和置信带。
但是,我在使用 geom_ribbon
时收到错误消息,但在使用 geom_line
时却没有收到错误消息。下面的最小示例说明了这个问题。
# Load packages and data
library(survival)
library(ggfortify)
data(aml, package = "survival")
# Fit the Kaplan-Meier curve
fit <- survfit(Surv(time, status) ~ x, data=aml)
# Create an additional dataset to plot on top of the Kaplan-Meier curve
df <- data.frame(x = seq(1, 150, length.out=10),
y = seq(0, 1, length.out=10),
ymin = seq(0, 1, length.out=10) - 0.1,
ymax = seq(0, 1, length.out=10) + 0.1)
这个有效
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_line(data = df, mapping = aes(x=x, y=y)) +
geom_line(data = df, mapping = aes(x=x, y=ymin)) +
geom_line(data = df, mapping = aes(x=x, y=ymax))
这行不通
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_ribbon(data = df, mapping = aes(x=x, ymin=ymin, ymax=ymax))
Error in FUN(X[[i]], ...) : object 'surv' not found
如果您在 geom_ribbon()
中指定 inherit.aes = FALSE
,则可以避免该特定错误,即
library(survival)
#install.packages("ggfortify")
library(ggfortify)
#> Warning: package 'ggfortify' was built under R version 4.1.2
#> Loading required package: ggplot2
data(aml, package = "survival")
#> Warning in data(aml, package = "survival"): data set 'aml' not found
# Fit the Kaplan-Meier curve
fit <- survfit(Surv(time, status) ~ x, data=aml)
# Create an additional dataset to plot on top of the Kaplan-Meier curve
df <- data.frame(x = seq(1, 150, length.out=10),
y = seq(0, 1, length.out=10),
ymin = seq(0, 1, length.out=10) - 0.1,
ymax = seq(0, 1, length.out=10) + 0.1)
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_line(data = df, mapping = aes(x=x, y=y)) +
geom_line(data = df, mapping = aes(x=x, y=ymin)) +
geom_line(data = df, mapping = aes(x=x, y=ymax))
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_ribbon(data = df, mapping = aes(x=x, ymin=ymin, ymax=ymax),
inherit.aes = FALSE)
由 reprex package (v2.0.1)
创建于 2022-02-21
这是否解决了您的问题?
我想在使用 ggfortify
包中的 autoplot
创建的生存分析图(Kaplan-Meier 曲线)中添加一条新线和置信带。
但是,我在使用 geom_ribbon
时收到错误消息,但在使用 geom_line
时却没有收到错误消息。下面的最小示例说明了这个问题。
# Load packages and data
library(survival)
library(ggfortify)
data(aml, package = "survival")
# Fit the Kaplan-Meier curve
fit <- survfit(Surv(time, status) ~ x, data=aml)
# Create an additional dataset to plot on top of the Kaplan-Meier curve
df <- data.frame(x = seq(1, 150, length.out=10),
y = seq(0, 1, length.out=10),
ymin = seq(0, 1, length.out=10) - 0.1,
ymax = seq(0, 1, length.out=10) + 0.1)
这个有效
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_line(data = df, mapping = aes(x=x, y=y)) +
geom_line(data = df, mapping = aes(x=x, y=ymin)) +
geom_line(data = df, mapping = aes(x=x, y=ymax))
这行不通
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_ribbon(data = df, mapping = aes(x=x, ymin=ymin, ymax=ymax))
Error in FUN(X[[i]], ...) : object 'surv' not found
如果您在 geom_ribbon()
中指定 inherit.aes = FALSE
,则可以避免该特定错误,即
library(survival)
#install.packages("ggfortify")
library(ggfortify)
#> Warning: package 'ggfortify' was built under R version 4.1.2
#> Loading required package: ggplot2
data(aml, package = "survival")
#> Warning in data(aml, package = "survival"): data set 'aml' not found
# Fit the Kaplan-Meier curve
fit <- survfit(Surv(time, status) ~ x, data=aml)
# Create an additional dataset to plot on top of the Kaplan-Meier curve
df <- data.frame(x = seq(1, 150, length.out=10),
y = seq(0, 1, length.out=10),
ymin = seq(0, 1, length.out=10) - 0.1,
ymax = seq(0, 1, length.out=10) + 0.1)
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_line(data = df, mapping = aes(x=x, y=y)) +
geom_line(data = df, mapping = aes(x=x, y=ymin)) +
geom_line(data = df, mapping = aes(x=x, y=ymax))
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_ribbon(data = df, mapping = aes(x=x, ymin=ymin, ymax=ymax),
inherit.aes = FALSE)
由 reprex package (v2.0.1)
创建于 2022-02-21这是否解决了您的问题?