使用 ggsurvfit 包移动 x 轴而不切割风险表

问题描述 投票:0回答:1

当我使用scale_x_continuous移动ggplot的x轴时,风险表第一列中的值被部分覆盖。

library(tidyverse)
library(ggsurvfit)

p <- 
  survfit2(Surv(time, status) ~ sex, data = df_lung) %>%
  ggsurvfit(linewidth = 1) + 
  scale_ggsurvfit() + 
  add_risktable(
    risktable_stats = c("n.risk")) +
  theme_classic()+
  scale_x_continuous(expand=c(0, 1), limits = c(0, 34), breaks = seq(0, 30, 6))


p

如何移动x轴而不裁剪部分风险表?

r survival-analysis ggsurvfit
1个回答
0
投票

使用基础 R,您可以使用原始的

survival
包,
aggregate
适合的分层生存表并将其添加为
text
plot
将带有特定的
axTicks(1)
,我们将用于
summary
和 x 位置。使用
xpd=TRUE
允许在绘图区域之外使用
text

> library(survival)
> fit <- survfit(Surv(time, status) ~ sex, data=transform(lung, sex=factor(sex, labels=c('Male', 'Female'))))
> par(mar=c(8, 4, 1, 1)+.1, xpd=TRUE)
> plot(fit, col=c(2, 4))
> (tb <- with(summary(fit, time=axTicks(1)), aggregate(surv ~ strata, FUN=I)$surv) |>
+     sapply(`length<-`, length(axTicks(1))) |> apply(2, \(x) replace(x, is.na(x), 0)) |> 
+     `colnames<-`(names(fit$strata)) |> round(2))
     sex=Male sex=Female
[1,]     1.00       1.00
[2,]     0.61       0.79
[3,]     0.30       0.51
[4,]     0.15       0.34
[5,]     0.07       0.08
[6,]     0.04       0.00
> legend('topright', legend=names(fit$strata), lwd=1, col=c(2, 4))
> text(-10, -.4, labels='At Risk', adj=1, cex=.9)
> Map(text, x=list(axTicks(1)), y=c(-0.55, -0.65), labels=asplit(tb, 2), cex=.8)
[[1]]
NULL

[[2]]
NULL

> Map(text, x=-150, y=c(-0.55, -0.65), labels=gsub('sex=', '', names(fit$strata)), 
+     adj=0, cex=.8)
[[1]]
NULL

[[2]]
NULL

© www.soinside.com 2019 - 2024. All rights reserved.