注解生存图右侧时停止剪切ggsurvplot风险表文本(长标签)

6vl6ewon  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(156)

我想在ggsurvplot中指定结束时间,如下所示注解生存曲线的端点,但这会导致number-at-risk表中最右边的文本被截断,请参见下面的代码和输出图形,注意在图形中,最右边的值是20和21,但您看到的只是2和2。
如果我增加右侧x轴限值(将xlim=c(-50, time_cutoff)替换为xlim=c(-50, time_cutoff+50)),则风险表文本不会被剪切,但生存曲线也会绘制到time_cutoff+50,并且我在生存曲线右侧显示的标签不会与生存曲线的端点对齐。
如果我只为风险表设置clip ='off',我会打印超过xlim截止值的额外值。

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,
 break.x.by=100,
 ggtheme = theme_bw() # Change ggplot2 theme              
)

time_cutoff = 400
xticks = c(0, 100, 200, 300, 400)

survs = summary(fit, times=time_cutoff)$surv
labels = paste(round(survs*100), '% this label is really long', sep='')
  
my_plot$plot <- my_plot$plot + 
                coord_cartesian(ylim=c(0,1), xlim = c(-50,time_cutoff), clip = 'on', expand=FALSE) +
                scale_x_continuous(name=NULL, breaks = xticks) +
                scale_y_continuous(name=NULL, sec.axis=sec_axis(~., name=NULL, breaks = survs, labels= labels))  +
                theme(
                  panel.border = element_blank(),
                  axis.line.y.left = element_line(color = 'black'),
                  axis.line.x.bottom = element_line(color = 'black'),
                )

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) +
                scale_x_continuous(name=NULL, breaks = xticks) +
                theme(panel.border = element_blank())
library(patchwork)
(my_plot$plot / my_plot$table) + plot_layout(heights = c(3,1))

x1c 0d1x现在仅针对风险表使用clip ='off':

另请参见this带有短标签的类似问题

46scxncf

46scxncf1#

可以关闭剪裁,但在时间〉400时移除表的图层数据中的标签:

my_plot$table$layer[[1]]$data$llabels[
  my_plot$table$layer[[1]]$data$time > 400] <- NA
  
(my_plot$plot / my_plot$table) + plot_layout(heights = c(3,1))

相关问题