【问题标题】:plot survival curves with confident interval bars用置信区间条绘制生存曲线
【发布时间】:2018-05-05 00:05:35
【问题描述】:

您好,我有一个问题。 我试图在同一个图上绘制 3 条生存曲线。 对于每个图,我想添加置信区间。

为此我有一个数据框

          dput(df)
    structure(list(lowerA = c(4.1, 4.6, 5.5, 4.7, 5), Group1 = c(4.8, 
5.4, 6.4, 5.5, 5.7), upperA = c(5.7, 6.3, 7.3, 6.3, 6.6), lowerB = c(6.2, 
7.1, 8.7, 7.4, 8.2), Group2 = c(7.3, 8.3, 10, 8.6, 9.5), upperB = c(8.6, 
9.6, 11.4, 9.9, 10.8), lowerC = c(18.3, 19.5, 24.3, 22.9, 25.5
), Group3 = c(21.4, 22.5, 27.6, 26.3, 29), upperC = c(24.7, 25.8, 
31, 29.8, 32.7)), .Names = c("lowerA", "Group1", "upperA", "lowerB", 
"Group2", "upperB", "lowerC", "Group3", "upperC"), row.names = c("year1", 
"year2", "year3", "year4", "year5"), class = c("tbl_df", "tbl", 
"data.frame"))

我试过这个。它有效,但并不漂亮。

ya <- rownames(df)
xa <- Group1
z1a <- lowerA
z2a <- upperA


xb <- Group2
z1b <- lowerB
z2b <- upperB


xc <- Group3
z1c <- lowerC
z2c <- upperC
#
plot(ya, xa, ylim = c(0,40))
lines(ya, xa, c)
# now the confidence bands
lines(ya, z1a, lty = "dotted")
lines(ya, z2a, lty = "dotted")

lines(yb, xb, col="green")
# now the confidence bands
lines(yb, z1b, lty = "dotted",col="green")
lines(yb, z2b, lty = "dotted", col="green")

lines(yc, xc, col="red")
# now the confidence bands
lines(yc, z1c, lty = "dotted", col="red")
lines(yc, z2c, lty = "dotted", col="red")

有什么帮助吗?建议?

最好, 彼得

【问题讨论】:

  • 请使用dput() 提供一些示例数据,如here 所述。
  • 假设您的 data.frame 名为 df,编辑您的问题并添加 dput(df) 的输出以提供示例数据。
  • 数据框添加完毕。
  • 1/2/3/4年的数据丢失
  • 抱歉,year1/2/3/4/5 只是行名。我修改了数据集。现在应该好了?谢谢tabiasegli_te

标签: r plot survival-analysis


【解决方案1】:

你可以这样做:

colnames(df) <- gsub("A","1",colnames(df))
colnames(df) <- gsub("B","2",colnames(df))
colnames(df) <- gsub("C","3",colnames(df))

colors <- c("blue","green","red")

library(scales)
plot(df$Group1, type = "n",ylim = c(0,40))
for(i in 1:3){
    lines(df[,paste0("Group",i)], col = colors[i])

    polygon(x = c(1:nrow(df),nrow(df):1),
        y = c(df[,paste0("lower",i)],rev(df[,paste0("upper",i)])),
        col = alpha(colors[i], 0.1),
        border = NA)

    arrows(x0 = c(1:nrow(df)),
        x1 = c(1:nrow(df)),
        y0 = df[,paste0("lower",i)],
        y1 = df[,paste0("upper",i)],
        col = colors[i],
        angle = 90,
        length = 0.05,
        code = 3)
}

legend("topleft", legend = c("A","B","C"),
    col = c("blue","green","red"),
    lty = 1,
    bty = "n")

编辑: 请注意,多边形的颜色是半透明的,置信区间的重叠仍然可见。我在您的示例中更改了一个值来说明这一点。

编辑 2: 我添加了一个图例和错误栏

【讨论】:

  • 嗨 tobiasegli_te,这正是我最初的脚本所拥有的。我觉得它不那么性感,我的意思是如何获得一个好身材。
  • 我明白了,从你的问题中我不清楚。我相应地编辑了我的答案。
  • 亲爱的 tobiasegli_te。不幸的是我无法重现你的身材。运行脚本时出现错误消息。 Error in xy.coords(x, y) : 'x' and 'y' lengths differ。谢谢
  • 如果您没有使用与您提供的完全相同的数据,您应该使用polygon(x = c(1:nrow(df),nrow(df):1), ...) 这可能会解决错误
  • 太棒了!现在完美了!错误信息不够清楚。我可以运行 scales 包而没有任何错误消息。谢谢你,亲爱的 tobiasegli_te。问题解决了 !!!只需要添加一个适当的图例。一个额外的问题是如何将 95% 置信区间误差线添加到每个值而不是半透明多边形?再次感谢
猜你喜欢
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
相关资源
最近更新 更多