ggsurvplot在生存图右侧进行注解后,风险表未对齐

brqmpdu1  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(456)

我想在曲线的末尾添加生存百分比。这样做会导致风险表与图形不对齐(图形宽度相对于风险表宽度减小)。

用于重现图形的代码:

library(survival)
library(survminer)

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

ggsurvplot(fit, data = lung,

  risk.table = TRUE,
  tables.height = 0.2,
  ggtheme = theme_bw() # Change ggplot2 theme
)

time_cutoff = 600
survs = summary(fit, times=time_cutoff)$surv
labels = paste(round(survs*100), '%', sep='')
  
my_plot$plot <- my_plot$plot + coord_cartesian(ylim=c(0,1), xlim = c(0,time_cutoff), clip = 'on', expand=FALSE)
my_plot$plot <- my_plot$plot + scale_y_continuous(name=NULL, sec.axis=sec_axis(~., name=NULL, breaks =  survs, labels= labels)) 
my_plot
dwbf0jvd

dwbf0jvd1#

有几种方法可以做到这一点。也许最简单的方法是将coord_cartesian添加到my_plot$table对象,然后使用patchwork绘制图和表,这将对齐面板

library(survival)
library(survminer)

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

my_plot <- ggsurvplot(fit, data = lung,
           risk.table = TRUE,
           tables.height = 0.2,
           ggtheme = theme_bw() 
)

time_cutoff = 600
survs = summary(fit, times=time_cutoff)$surv
labels = paste(round(survs*100), '%', sep='')

my_plot$plot <- my_plot$plot + 
  coord_cartesian(ylim = c(0, 1), 
                  xlim = c(0, time_cutoff), 
                  clip = 'on') + 
  scale_y_continuous(name = NULL, 
                     sec.axis = sec_axis(~., name = NULL, breaks = survs, 
                                         labels = labels)) 

my_plot$table <- my_plot$table +
  coord_cartesian(xlim = c(0, time_cutoff), 
                  clip = 'on')

library(patchwork)

(my_plot$plot / my_plot$table) + plot_layout(heights = c(3, 1))

创建日期:2022年12月8日,使用reprex v2.0.2

ygya80vv

ygya80vv2#

感谢Allen Cameron's answer让我得到了95%的答案,加上他的回答,我不得不像修改生存图那样修改表:

library(survival)
library(survminer)

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

# Customized survival curves
my_plot = ggsurvplot(fit, data = lung,
 # Add p-value and tervals
 risk.table = TRUE,
 tables.height = 0.2,
 ggtheme = theme_bw() # Change ggplot2 theme
)

time_cutoff = 600
survs = summary(fit, times=time_cutoff)$surv
labels = paste(round(survs*100), '%', sep='')
  
my_plot$plot <- my_plot$plot + coord_cartesian(ylim=c(0,1), xlim = c(-50,time_cutoff), clip = 'on', expand=FALSE)
my_plot$plot <- my_plot$plot + scale_y_continuous(name=NULL, sec.axis=sec_axis(~., name=NULL, breaks = survs, labels= labels)) 
table_ylim = ggplot_build(my_plot$table)$layout$panel_params[[1]]$y.range
my_plot$table <- my_plot$table + coord_cartesian(ylim=table_ylim, xlim = c(-50,time_cutoff), clip = 'on', expand=FALSE)
library(patchwork)
(my_plot$plot / my_plot$table) + plot_layout(heights = c(3,1))

相关问题