如何在 R studio ggsurvplot 中添加文本?

问题描述 投票:0回答:1

我是 R-studio 的新手,现在在使用

ggsurvplot
功能时遇到困难。

我想做的只是在

ggsurvplot
上添加两行文本。

由于那些‘gg-’的东西不接受‘+’,我的学习最后一步就卡了一个星期了。

帮助我。

谢谢。

1 条文字

text(x = 20, y = 0.8, label = median_survival_ia_UFS, col = "blue", cex = 1.2)
text(x = 20, y = 0.7, label = median_survival_ia_NAT, col = "red", cex = 1.2)

2 格罗

library(gridExtra)
combined_plot <- grid.arrange(p$plot, p_text, heights = c(0.9, 0.1))
text_grob <- grobTree(textGrob(label = text_data$label, 
                      x = text_data$x, y = text_data$y, just = "centre"))

p <- arrangeGrob(nullGrob(), p, text_grob, 
         heights = unit.c(unit(0.9, "npc"), unit(0.1, "npc")), nrow = 2)
> print(p)   #Where p represents ggsurvplot
ggplot2 rstudio ggsurvfit
1个回答
0
投票

下面,我提出一个不太优雅的解决方案,但很有效。
当然有一种更简单的解决方案,但即使从这个解决方案中,也可以吸取一些有用的教训。

library(survminer)
library(survival)

fit <- survfit(Surv(time, status) ~ sex, data = lung)
p <- ggsurvplot(fit, data = lung)

df_txt <- data.frame(x=700, y=0.8, lab="Kaplan-Meier curves\n by sex")
p$plot$layers[[4]] <- layer(geom="text", position="identity", stat="identity",
                            mapping=aes(x=x, y=y, label=lab), 
                            params=list(size=6, color="blue"), data=df_txt) 
print(p)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.