autoplot我想向使用ggfortify包創建的生存分析圖(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
uj5u.com熱心網友回復:
如果您指定避免inherit.aes = FALSE該geom_ribbon()特定錯誤,即
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 包于 2022-02-21 創建(v2.0.1)
這能解決你的問題嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429478.html
上一篇:django.core.exceptions.FieldError:無法將關鍵字“日期”決議為欄位。不允許在“日期”加入
下一篇:在經驗密度上繪制理論密度
