ggsurvplot - 重新定位风险表的分层颜色指示器

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

导出为pdf格式时,是否可以调整风险表中黄色和蓝色分层指示器的位置?

这是我的代码:

library(survminer)
library(survival)
library(patchwork)

lung <- lung 

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

ggsurv <- ggsurvplot(fit,  size = 1,  
                     xlab = "Follow-up time (years)",
                     ylab = "Survival probability (%)",
                     legend.labs = c("A", "B"), 
                     linetype = "solid", 
                     break.time.by = 365, 
                     palette = c("#E7B800", "#2E9FDF"), 
                     risk.table = TRUE,
                     risk.table.title = "No. at risk",
                     risk.table.height = 0.2,
                     fontsize = 6,
                     tables.theme = theme_cleantable(),
                     tables.y.text = FALSE
)
ggsurv

ggsurv$plot <- ggsurv$plot + 
  scale_x_continuous(name = "Follow-up time", breaks = c(0, 365, 730)) +
  coord_cartesian(ylim = c(0, 1), xlim = c(0, 730), clip = 'on', expand = FALSE) +
  theme(panel.grid.major.y = element_line(color = "white", size = 0.5),
        plot.margin = margin(0, 1, 0, 1, "cm"))

ggsurv$table <- ggsurv$table +
  coord_cartesian(xlim = c(0, 730), clip = 'off', expand = FALSE) +
  theme(plot.margin = margin(0, 1, 0, 1, "cm"),
        plot.title = element_text(hjust = -0.1),
        panel.grid = element_blank())

ggsurv$table$layer[[1]]$data$llabels[ggsurv$table$layer[[1]]$data$time > 730] <- NA

combined_plot <- ggsurv$plot / ggsurv$table + plot_layout(heights = c(3,0.5))

ggsave("xmpl.pdf", combined_plot)

我知道

hjust
论点,但它只是重新定位了风险表标题(“有风险的人数”):

theme(plot.margin = margin(0, 1, 0, 1, "cm"),
        plot.title = element_text(hjust = -0.1),

这就是我得到的。但正如您所看到的,黄色和蓝色指示器需要向左移动。

This is the preview of my pdf

当我删除

coord_cartesian =
时,它是对齐的,但随后 y 轴和 x 轴的零点会像这样移动。此外,限制 x 轴不再存在。

enter image description here

r ggplot2 survminer
1个回答
0
投票

分层文本的彩色指示器是通过 y 轴文本添加的。因此,“移动”它们的一种选择是增加

axis.text.y
的右边距:

library(survminer)
library(survival)
library(patchwork)

ggsurv$table <- ggsurv$table +
  coord_cartesian(xlim = c(0, 730), clip = "off", expand = FALSE) +
  theme(
    plot.margin = margin(0, 1, 0, 1, "cm"),
    plot.title = element_text(margin = margin(l = -13, b = 11, t = 5.5)),
    panel.grid = element_blank(),
    axis.text.y = ggtext::element_markdown(margin = margin(r = 12))
  )

combined_plot <- ggsurv$plot / ggsurv$table + plot_layout(heights = c(3, 0.5))

combined_plot

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