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.)

Share Improve this question edited Feb 25 at 15:19 jpsmith 17.7k6 gold badges22 silver badges45 bronze badges asked Feb 24 at 16:11 andrewjandrewj 3,0578 gold badges38 silver badges38 bronze badges 1
  • 1 Have you tried the following tables.y.text = FALSE in ggsurvplot (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
Add a comment  | 

3 Answers 3

Reset to default 2

Try

 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 of T;
  • 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 need df$...

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