Is it possible to adjust the position of the yellow and blue strata indicators in the risk table when exporting to pdf format?
This is my code:
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)
I am aware of the hjust
argument but it just repositions the risk table title ("No. at risk"):
theme(plot.margin = margin(0, 1, 0, 1, "cm"),
plot.title = element_text(hjust = -0.1),
This is what I get. But as you can see, the yellow and blue indicators need to be shifted to the left.
It is in line when I delete coord_cartesian =
but then the y and x axis' zeros are shifted like this. Further, limiting the x axis is not present anymore.
The colored indicators for the strata text are added via the y axis text. Hence, one option to "shift" them would be to increase the right margin of 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