简体   繁体   English

为 ggsurvplot 中的 plot 和表格设置不同的 x 轴中断(R 中的 survminer package)

[英]Set different x-axis breaks for plot and table in ggsurvplot (survminer package in R)

I can set different x-axis breaks in ggplot and survminer::ggsurvplot.我可以在 ggplot 和 survminer::ggsurvplot 中设置不同的 x 轴中断。 I am currently setting this by using break.time.by = 365.25 / 12 (monthly).我目前通过使用break.time.by = 365.25 / 12 (每月)来设置它。 However, I need to set axis labels for the table every 12 months (to meet formatting requirements that I can't change).但是,我需要每 12 个月为表格设置一次轴标签(以满足我无法更改的格式要求)。 I tried formatting just the table component of the overall plot and using scale_x_continuous(breaks = c(values here)) but that didn't work.我尝试仅格式化整个 plot 的表格组件并使用 scale_x_continuous(breaks = c(values here)) 但这不起作用。 I get a message saying that Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.我收到一条消息说Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale. Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

You can substitute a scale in the object created by ggsurvplot , which is a list-like object containing two ggplot objects (among other things).您可以在 ggsurvplot 创建的ggsurvplot中替换一个比例,这是一个类似列表的 object 包含两个ggplot对象(除其他外)。 One of these ggplot objects is called table .这些ggplot对象之一称为table You can either add a second scale_x_continuous to that (and get the warning about adding a second scale), or just over-write the original.您可以添加第二个scale_x_continuous (并获得有关添加第二个比例的警告),或者只是覆盖原始文件。

As an example, we'll use one of the built in data sets from the survival package:例如,我们将使用来自survival package 的内置数据集之一:

library(survival)
library(survminer)

fit3 <- survfit( Surv(time, status) ~ sex, data = colon)

ggsurv <- ggsurvplot(fit3, data = colon,
  fun = "cumhaz", conf.int = TRUE,
  risk.table = TRUE, risk.table.col="strata",
  ggtheme = theme_bw())

ggsurv

We can find out which scale object is the x-scale by doing:我们可以通过执行以下操作找出哪个比例 object 是 x 比例:

x <- which(ggsurv$table$scales$find("x"))

And over-write it with our own scale.并用我们自己的规模覆盖它。 For example, we can make breaks every 200 rather than every 1000 units:例如,我们可以每 200 个而不是每 1000 个单位休息:

ggsurv$table$scales$scales[[x]] <- scale_x_continuous(breaks = 0:15 * 200)

And our plot is now as we want it, with the new scale in the table's x axis:我们的 plot 现在是我们想要的,在表格的 x 轴上有新的比例:

ggsurv

Created on 2022-01-26 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 1 月 26 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM