R survminer::ggsurvplot无法合并,因为存在”atomic vector”。

huangapple go评论172阅读模式
英文:

R survminer::ggsurvplot can't combine due to "atomic vector"?

问题

可复现的示例:

  1. require(Survival)
  2. require(survminer)
  3. require(ggplot2)
  4. set.seed(42)
  5. a <- c(32,291,545, 44,3,792,352, 20,615,169, 61,156, 88,863,255, 33,132,5,
  6. 63,8,964,831, 55,133, 12, 54,261,867, 17, 12,699,233,251,446, 43,223,
  7. 374, 75, 34, 69,120, 84,134,8,806,8, 48, 86,211, 2436, 61, 81, 87, 12,
  8. 838, 34, 17,141, 44,155,128,6, 29, 16, 16, 34,130,430,325, 41, 28, 53,
  9. 32,291,545, 44,3,792,352, 20,615,169, 61,156, 88,863,255, 33,132,5,
  10. 63,8,964,831, 55,133, 12, 54,261,867, 17, 12,699,233,251,446, 43,223,
  11. 86, 54,3,630, 93,699, 25,746,6, 46, 22, 60,395,402,151, 26, 38,125,
  12. 75, 34, 69,120, 84,134,8,806,8, 48, 86,211, 2436, 61,
  13. 191, 49, 59,6, 34, 56,2, 96, 422, 45, 70)
  14. b <- sample(c("Alpha", "Beta", "Gamma"), length(a), replace = TRUE)
  15. c <- sample(0:1, length(a), replace = TRUE)
  16. df <- data.frame(a, b, c)
  17. avg <- survfit(Surv(time = a, event = c) ~ 1, data = df)
  18. survminer::ggsurvplot(
  19. avg,
  20. conf.int = FALSE,
  21. censor = FALSE,
  22. palette = "black",
  23. linetype = "dashed"
  24. )
  25. surv_b <- survfit(Surv(time = a, event = c) ~ b, data = df)
  26. survminer::ggsurvplot(
  27. surv_b,
  28. conf.int = FALSE,
  29. censor = FALSE,
  30. )
  31. surv_comb <- c(avg, surv_b)
  32. survminer::ggsurvplot_combine(
  33. surv_comb,
  34. data = df,
  35. combine = TRUE,
  36. censor = FALSE,
  37. legend = "right"
  38. )

两条生存曲线分别绘制时没有错误,但当我尝试将它们组合时,出现了一个错误,指出“$”运算符对原子向量无效。我已经检查了几乎所有的东西,包括is.atomic(),但我找不到问题的原因。能否有人帮助我解决这个问题?

英文:

Reproducible example:

  1. require(Survival)
  2. require(survminer)
  3. require(ggplot2)
  4. set.seed(42)
  5. a &lt;- c(32,291,545, 44,3,792,352, 20,615,169, 61,156, 88,863,255, 33,132,5,
  6. 63,8,964,831, 55,133, 12, 54,261,867, 17, 12,699,233,251,446, 43,223,
  7. 374, 75, 34, 69,120, 84,134,8,806,8, 48, 86,211, 2436, 61, 81, 87, 12,
  8. 838, 34, 17,141, 44,155,128,6, 29, 16, 16, 34,130,430,325, 41, 28, 53,
  9. 32,291,545, 44,3,792,352, 20,615,169, 61,156, 88,863,255, 33,132,5,
  10. 63,8,964,831, 55,133, 12, 54,261,867, 17, 12,699,233,251,446, 43,223,
  11. 86, 54,3,630, 93,699, 25,746,6, 46, 22, 60,395,402,151, 26, 38,125,
  12. 75, 34, 69,120, 84,134,8,806,8, 48, 86,211, 2436, 61,
  13. 191, 49, 59,6, 34, 56,2, 96, 422, 45, 70)
  14. b &lt;- sample(c(&quot;Alpha&quot;, &quot;Beta&quot;, &quot;Gamma&quot;), length(a), replace = T)
  15. c &lt;- sample(0:1, length(a), replace = T)
  16. df &lt;- data.frame(a, b, c)
  17. avg &lt;- survfit(Surv(time = a, event = c) ~ 1, data = df)
  18. survminer::ggsurvplot(
  19. avg,
  20. conf.int = FALSE,
  21. censor = FALSE,
  22. palette = &quot;black&quot;,
  23. linetype = &quot;dashed&quot;
  24. )
  25. surv_b &lt;- survfit(Surv(time = a, event = c) ~ b, data = df)
  26. survminer::ggsurvplot(
  27. surv_b,
  28. conf.int = FALSE,
  29. censor = FALSE,
  30. )
  31. surv_comb &lt;- c(avg, surv_b)
  32. survminer::ggsurvplot_combine(
  33. surv_comb,
  34. data = df,
  35. combine = TRUE,
  36. censor = FALSE,
  37. legend = &quot;right&quot;
  38. )

The two survival curves separately plot without error, but when I try to combine them, I get an error that ! $ operator is invalid for atomic vectors. I have checked just about everything with is.atomic() and I can't find what causes the issue. Could someone help me figure this out, please?

答案1

得分: 1

fits <- list(avg= avg, surv_b = surv_b)

survminer::ggsurvplot_combine(
fits,
data = df,
combine = TRUE,
censor = FALSE,
legend = "right"
)

英文:

You need to make a list, with your two fits in it, not concatenate the fits.

  1. fits &lt;- list(avg= avg, surv_b = surv_b)
  2. survminer::ggsurvplot_combine(
  3. fits,
  4. data = df,
  5. combine = TRUE,
  6. censor = FALSE,
  7. legend = &quot;right&quot;
  8. )

huangapple
  • 本文由 发表于 2023年3月7日 03:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定