admin管理员组文章数量:1279182
I'm making a Kaplan-Meier curve with four different groups. I would like to remove group from the risk table but retain the names a, b, c, and d.
library('survival')
library('survminer')
df <- data.frame(group=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'), time=c(1,2,3,4,5,6,1,2,3,4,5,6), status = c(1,1,0,1,0,1,0,1,0,1,1,0))
result <- survfit(Surv(df$time, df$status==1) ~ group, data=df)
ggsurvplot(result, risk.table=T)
I would like to remove the variable name group
from the risk table (which is encircled). I've been looking around, but haven't found an obvious answer.
(Yes I know that there's an alignment issue in the risk table where it's shifted to the right, and which can be addressed with the patchwork
library.)
I'm making a Kaplan-Meier curve with four different groups. I would like to remove group from the risk table but retain the names a, b, c, and d.
library('survival')
library('survminer')
df <- data.frame(group=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'), time=c(1,2,3,4,5,6,1,2,3,4,5,6), status = c(1,1,0,1,0,1,0,1,0,1,1,0))
result <- survfit(Surv(df$time, df$status==1) ~ group, data=df)
ggsurvplot(result, risk.table=T)
I would like to remove the variable name group
from the risk table (which is encircled). I've been looking around, but haven't found an obvious answer.
(Yes I know that there's an alignment issue in the risk table where it's shifted to the right, and which can be addressed with the patchwork
library.)
3 Answers
Reset to default 2Try
library(survival)
library(survminer)
df0 = data.frame(group=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'), time=c(1,2,3,4,5,6,1,2,3,4,5,6), status=c(1,1,0,1,0,1,0,1,0,1,1,0))
p = survfit(Surv(time, status) ~ group, data = df0) |>
ggsurvplot(risk.table=TRUE)
p$table$theme$axis.text.y = ggplot2::element_blank()
p
if you like to modify by hand. You might want to modify contents of p$table
for further changes.
Notes
- Please use
TRUE
instead ofT
; df()
is a function from{stats}
. Most programmers try to avoid storing objects in masked names;- you do not need to quote package (names) in
library()
; - for
Surv
you do not needdf$...
Please find a solution, depending on whether you want to remove or update the names of the groups
##### Update the names (please note that it also update the top legend)
ggsurvplot(result, risk.table=T, legend.labs=c("a", "b", "c", "d"))
##### Remove the names (please note that it doesn't affect the legend)
ggsurvplot(result, risk.table=T, tables.y.text=FALSE)
The following shows an updated figure which incorporates the above recommendation by Yacine Hajji. And it also shows how you can line up the risk table with the KM curve better, using patchwork
.
Moreover there appears to be a bug where the number at risk at time 0 is not being shown, but this can be addressed by adding break.time.by
.
library(survival)
library(survminer)
library(patchwork)
df <- data.frame(group=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'), time=c(1,2,3,4,5,6,1,2,3,4,5,6), status = c(1,1,0,1,0,1,0,1,0,1,1,0))
result <- survfit(Surv(df$time, df$status==1) ~ group, data=df)
p1 <- ggsurvplot(result,
risk.table=T,
legend.labs=c("a", "b", "c", "d"),
break.time.by=1)
p1$plot
p1$plot/p1$table + plot_layout(heights=c(8,2))
本文标签: rRemoving variable name from risk table of ggsurvplotStack Overflow
版权声明:本文标题:r - Removing variable name from risk table of ggsurvplot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741253897a2366302.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
tables.y.text = FALSE
inggsurvplot
(if you want to remove all text). If you want to update the names,legend.labs=c("a", "b", "c", "d")
may work – Yacine Hajji Commented Feb 24 at 16:35