我如何将多条绘图线(如细蓝线)添加到本文底部代码中显示的现有ggplot
代码中,并包含在图例“Simulations”中,用于我的数据框simPaths_df
中的模拟运行?模拟在时段500中开始。该simPaths_df
Dataframe 具有针对该时间段的单个“x”列,并且多个y轴模拟输出位于 Dataframe 中标记为y1、y2的右侧列中。..等等
抱歉,代码看起来很长。实际的代码很短,但是ggplot
部分很长,而且很复杂,因为我对ggplot
不是很熟悉。我愿意接受任何简化这个烂摊子的建议!
下面的图片显示了我正在尝试做的事情:
验证码:
library(dplyr)
library(ggplot2)
library(MASS)
library(survival)
lung1 <- lung %>%
mutate(time1 = ifelse(time >= 500, 500, time)) %>%
mutate(status1 = ifelse(status == 2 & time >= 500, 1, status))
weibCurve <- function(time, survregCoefs) {exp(-(time/exp(survregCoefs[1]))^exp(-survregCoefs[2]))}
fit1 <- survreg(Surv(time1, status1) ~ 1, data = lung1)
lung1.survfit <- survfit(Surv(time1, status1) ~ 1, data = lung1)
lung1.df <- data.frame(time = lung1.survfit$time,
survival = lung1.survfit$surv,
upper_95 = lung1.survfit$upper,
lower_95 = lung1.survfit$lower)
# generate simulation paths
n_simulations <- 10
simPaths <- data.frame(x = seq(from = 500, to = 1000, by = 5))
simPathList <- lapply(1:n_simulations, function(i) {
newCoef <- MASS::mvrnorm(n = 1, fit1$icoef, vcov(fit1))
y <- weibCurve(simPaths$x, newCoef)
simPaths[[paste0("y", i)]] <- y
simPaths
})
simPaths_df <- Reduce(function(x, y) merge(x, y, by = "x", all = TRUE), simPathList)
lung1.df %>%
ggplot(aes(x = time, y = survival)) +
geom_ribbon(aes(ymin = lower_95, ymax = upper_95, fill = "Confidence Interval"),
alpha = 0.2) +
scale_fill_manual(values = c("Confidence Interval" = "grey50"), name = NULL) +
guides(color = guide_legend(order = 1), fill = guide_legend(order = 2)) +
geom_line(aes(y = survival, color = "Historical data"), size = 1) +
scale_x_continuous(limits = c(0, 1500)) +
scale_y_continuous(limits = c(0, 1), expand = c(0, 0.05)) +
labs(x = "Time", y = "Survival probability", color = NULL) +
theme_classic() +
stat_function(fun = weibCurve, args = list(survregCoefs = fit1$icoef),
aes(color = "Weibull distribution fit"), size = 1, n = 1000) +
scale_color_manual(values = c("blue", "red", "grey50"),
labels = c("Historical data", "Weibull distribution fit", "Confidence intervals")) +
labs(color = NULL) +
theme(legend.position = c(0.95, 0.95), legend.justification = c(1, 1),
legend.title.align = 0.5, legend.box.spacing = unit(0.3, "lines"),
legend.margin = margin(t = 0, r = 0, b = 0, l = 0), legend.title = element_text(size = 12),
legend.text = element_text(size = 10))
2条答案
按热度按时间sgtfey8w1#
为了实现这一点,需要做一些改变:
"Confidence Interval"
-〉"...vals"
scale_color_manual
中的命名值而不是值和标签;它应该是一样的,我发现自己在做循环在过去的决议是这样做。simPaths_df
数据进行透视/整形,使其“更长”。秩序很重要。如果您希望蓝色线条更像背景,并且“低于”所有其他元素,那么将其放置在第一个
geom_line
之前,例如zbdgwd5y2#
有个办法
创建于2023-05-01使用reprex v2.0.2