英文:
How to remove automated "strata"-text in ggsurvplot legend?
问题
请查看下面的数据样本 p
。我已经生成了以下的 ggsurvplot
图表:
[![enter image description here][1]][1]
我想要从图例中移除被圈出的 "Strata" 文本,因为它会自动打印出来。我认为这是多余的,会破坏图表的平衡。
当我运行以下脚本时会打印出来:
# 拟合数据
fit <- survfit(Surv(p$rfs, p$recurrence) ~ p$test, data=p)
# 绘图
j <- ggsurvplot(
fit,
data = p,
risk.table = TRUE,
pval = TRUE,
pval.coord = c(0, 0.25),
conf.int = T,
legend.labs=c("TERTp-wt (all)", "TERT-alt (all)"),
size=0.7,
xlim = c(0,50),
# alpha=c(0.4),
conf.int.alpha=c(0.1),
break.x.by = 6,
xlab="Time in months",
ylab="Probability of recurrence-free survival",
ggtheme = theme,
surv.median.line = "v",
ylim=c(0,1),
tables.theme=theme,
surv.scale="percent",
tables.col="strata",
risk.table.col = "strata",
risk.table.y.text = FALSE,
tables.y.text = FALSE)
j$table <- j$table + labs(x = NULL, y = NULL)
j$plot <- j$plot+annotate("text", x = 14, y = 0.005,
label = "14 (95% CI: 10 - 24)", cex=3.3, vjust=0,
hjust = 1.1, fontface=2)
j
数据样本:
p <- structure(list(rfs = c(38, 11.08, 49.5, 21.37, 73.5, 89, 0.72,
64.3, 78, 22.9, 50.5, 99.8, 102.48, 114.35, 16.44, 53, 41, 96.2,
113.42, 8.5, 25.7, 169.22, 1.97, 46.7, 71.5, 51.1, 88.5, 85.4,
23, 42.7, 90.9, 102.61, 29.2, 48.2, 120, 69.5, 75.16, 48, 0.13,
179.34, 70.19, 75.3, 22, 126.74, 69.8, 25.15, 42.35, 31.2, 2.04,
59.9, 106.88, 23.6, 364.73, 67, 160.83, 22.68, 7.5, 67.4, 20,
184.34, 72, 12, 386.19, 44.8, 42.9, 163.95, 63.4, 57.1, 0.46,
66.9, 128, 1.02, 43.5, 93, 81.3, 47.9, 72.4, 96.2, 90.1, 99.1,
90.8, 75.9, 88.01, 57.3, 97.2, 101.33, 136.27, 95.5, 97.1, 14,
3.3, 72, 56, 140, 12, 12, 31, 45, 2.9, 48), recurrence = c(1L,
0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), test = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0
<details>
<summary>英文:</summary>
Please, find a data sample `p` below. I have produced the following `ggsurvplot`:
[![enter image description here][1]][1]
I would like to remove the encircled "Strata"-text from the legend, that prints automatically. I think it is redundant and disrupt the " graphic balance".
It prints when I run this script:
#Fit the data
fit <- survfit(Surv(p$rfs, p$recurrence) ~ p$test, data=p)
#Plot
j <- ggsurvplot(
fit,
data = p,
risk.table = TRUE,
pval = TRUE,
pval.coord = c(0, 0.25),
conf.int = T,
legend.labs=c("TERTp-wt (all)", "TERT-alt (all)"),
size=0.7,
xlim = c(0,50),
#alpha=c(0.4),
conf.int.alpha=c(0.1),
break.x.by = 6,
xlab="Time in months",
ylab="Probability of recurrence-free survival",
ggtheme = theme,
surv.median.line = "v",
ylim=c(0,1),
tables.theme=theme,
surv.scale="percent",
tables.col="strata",
risk.table.col = "strata",
risk.table.y.text = FALSE,
tables.y.text = FALSE)
j$table <- j$table + labs(x = NULL, y = NULL)
j$plot <- j$plot+annotate("text", x = 14, y = 0.005,
label = "14 (95% CI: 10 - 24)", cex=3.3, vjust=0,
hjust = 1.1, fontface=2)
j
Data sample
p <- structure(list(rfs = c(38, 11.08, 49.5, 21.37, 73.5, 89, 0.72,
64.3, 78, 22.9, 50.5, 99.8, 102.48, 114.35, 16.44, 53, 41, 96.2,
113.42, 8.5, 25.7, 169.22, 1.97, 46.7, 71.5, 51.1, 88.5, 85.4,
23, 42.7, 90.9, 102.61, 29.2, 48.2, 120, 69.5, 75.16, 48, 0.13,
179.34, 70.19, 75.3, 22, 126.74, 69.8, 25.15, 42.35, 31.2, 2.04,
59.9, 106.88, 23.6, 364.73, 67, 160.83, 22.68, 7.5, 67.4, 20,
184.34, 72, 12, 386.19, 44.8, 42.9, 163.95, 63.4, 57.1, 0.46,
66.9, 128, 1.02, 43.5, 93, 81.3, 47.9, 72.4, 96.2, 90.1, 99.1,
90.8, 75.9, 88.01, 57.3, 97.2, 101.33, 136.27, 95.5, 97.1, 14,
3.3, 72, 56, 140, 12, 12, 31, 45, 2.9, 48), recurrence = c(1L,
0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), test = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(NA, 100L
), class = "data.frame")
[1]: https://i.stack.imgur.com/MC073.png
</details>
# 答案1
**得分**: 16
你只需将`legend.title=""`添加到`ggsurvplot`中即可:
```R
j <- ggsurvplot(
fit,
data = p,
risk.table = TRUE,
pval = TRUE,
pval.coord = c(0, 0.25),
conf.int = T,
legend.labs=c("TERTp-wt (all)", "TERT-alt (all)"),
size=0.7,
xlim = c(0,50),
#alpha=c(0.4),
conf.int.alpha=c(0.1),
break.x.by = 6,
xlab="Time in months",
ylab="Probability of recurrence-free survival",
ggtheme = theme,
surv.median.line = "v",
ylim=c(0,1),
tables.theme=theme,
surv.scale="percent",
tables.col="strata",
risk.table.col = "strata",
risk.table.y.text = FALSE,
tables.y.text = FALSE,
legend.title="")
英文:
All you need to do is to add legend.title=""
to ggsurvplot
:
j <- ggsurvplot(
fit,
data = p,
risk.table = TRUE,
pval = TRUE,
pval.coord = c(0, 0.25),
conf.int = T,
legend.labs=c("TERTp-wt (all)", "TERT-alt (all)"),
size=0.7,
xlim = c(0,50),
#alpha=c(0.4),
conf.int.alpha=c(0.1),
break.x.by = 6,
xlab="Time in months",
ylab="Probability of recurrence-free survival",
ggtheme = theme,
surv.median.line = "v",
ylim=c(0,1),
tables.theme=theme,
surv.scale="percent",
tables.col="strata",
risk.table.col = "strata",
risk.table.y.text = FALSE,
tables.y.text = FALSE,
legend.title="")
答案2
得分: 3
删除图例标题的另一种方法是在绘图主题中设置legend.title=element_blank()
。
在ggsurvplot()
中添加legend.title = ""
在图的顶部放置图例时效果很好。然而,如果图例位于图内部,使用legend.title = ""
会将标题替换为空字符串,因此导致图例框和图例文本之间存在空白空间。如果您的背景颜色或图例框与生存曲线重叠,这可能会变得可见。
因此,在将图例放置在图内部时,使用element_blank()
可能更有用:
j <- ggsurvplot(
fit,
data = p,
risk.table = TRUE,
legend = c(0.2, 0.13),
risk.table.y.text = FALSE)
j$plot <- j$plot +
theme(legend.title=element_blank())
英文:
An alternative way of removing the legend title is setting legend.title=element_blank()
in the plot theme.
Adding legend.title = ""
to ggsurvplot()
works well when the legend is on the top side of the plot. However, if the legend is positioned inside the plot, using legend.title = ""
replaces the title with an empty string and therefore causes empty space between the legend box and the legend text. This may become visible if you have a background colour or your legend box overlaps with the survival lines.
Therefore using element_blank()
may be more useful when your legend is positioned inside the plot:
j <- ggsurvplot(
fit,
data = p,
risk.table = TRUE,
legend = c(0.2, 0.13),
risk.table.y.text = FALSE)
j$plot <- j$plot +
theme(legend.title=element_blank())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论