Reputation: 723
-- Possible SOLUTION as answer
The labels are quite long resulting in shifting the No at risk to the right.
Further, they are not left-aligned anymore.
Thus, they are not in line with the x-axis of the plot.
How to reposition the strata labels on top, above the number at risk? Is there any possibility?
library(survminer)
library(survival)
library(dplyr)
lung <- lung %>%
mutate(timeyr = time / 365.25)
fit <- survfit(Surv(timeyr, status) ~ sex, data = lung)
ggsurv <- ggsurvplot(fit, size = 1,
xlab = "Follow-up time (years)",
ylab = "Survival probability (%)",
legend.labs = c("Drug 1234234", "Drug 25324"),
linetype = "strata",
xlim = c(0,2),
break.time.by = 1,
palette = c("#E7B800", "#2E9FDF"),
conf.int = FALSE,
pval = FALSE,
censor = FALSE,
risk.table = TRUE,
risk.table.title = "No. at risk",
risk.table.height = 0.2,
fontsize = 6,
tables.theme = theme_cleantable()
)
ggsurv
Upvotes: 2
Views: 665
Reputation: 723
Got a workaround.
Just add the lines of code and play around until it fits your needs:
library(patchwork)
ggsurv$table <- ggsurv$table +
theme(
plot.title = element_text(margin = margin(l = 16, r = 0, t = 0), hjust = 0, vjust = 5), #Adjust "No. at risk"
axis.text.y = ggtext::element_markdown(margin = margin(l = 20, r = -100, t = -30), hjust = 0)) #Adjust Strata labels
Combine the plot and the table:
combined_plot <- ggsurv$plot / ggsurv$table + plot_layout(heights = c(3,0.8))
combined_plot
Upvotes: 0
Reputation: 4370
If you are open to it I would consider using a different plotting package called ggsurvfit. It has a few benefits but one of the main ones is that it creates a true ggplot object and you can add all the ggplot2 customizations after the survival plot is created. This will allow you to switch and manipulate the plot much easier. Here is how I would do it below. You don't have to worry about long labels in the risk table because it will put them on top of the group instead of the side.
library(survival)
library(ggsurvfit)
lung <- lung %>%
mutate(timeyr = time / 365.25,
sex2 = ifelse(sex == 1 , "Drug 1234234", "Drug 25324"))
fit <- survfit2(Surv(timeyr, status) ~ sex2, data = lung)
ggsurvfit::ggsurvfit(fit) +
add_risktable() +
xlab("Follow-up time (years)") +
ylab("Survival probability (%)")
ggsurvfit::ggsurvfit(fit,
linetype_aes = TRUE) +
scale_color_manual(values = c("red", "blue")) +
add_risktable() +
xlab("Follow-up time (years)") +
ylab("Survival probability (%)")
Upvotes: 2