I am generating a kaplan-meir with a number at risk table using ggsurvplot. I want my axes expanded which is shown in the graph. However, this then means that the table does not line up with the plot. I want the axes expanded in the table as well so that they are aligned. How does one do this?
library(survival)
library(survminer)
library(ggpp)
df <- lung
# surv time in months
df$time_months <- df$time/(365.25/12)
km_fit<- survfit(Surv(time_months, status) ~ sex, data = df)
time_int <- 2
# SURV FIT
km_plot <- ggsurvplot(km_fit,
data = df,
risk.table = TRUE,
fontsize = 3,
cumevents = FALSE,
# add.all = TRUE,
# cumevents.title = "Cumulative No. of Events",
risk.table.height = 0.18,
cumevents.height = 0.18,
break.x.by = time_int, # Set x-axis breaks to X months intervals (90 days)
x.tick.by = time_int, # Set x-axis tick marks to X months intervals (90 days)
time.inc = time_int, # Set risk table to group by X months intervals (90 days)
risk.table.by = time_int, # Set risk table to group by X months intervals (90 days)
legend.title = "", # Set legend title
tables.y.text = FALSE, # Remove text next to risk table and just have colours
surv.median.line = "hv",
xlim = c(0, max(km_fit$time*1.2)),
xlab = "Time (months)")
# Customise KM aesthetics
km_plot$table <- km_plot$table +
theme(plot.title = element_text(size = 10, color = "black", face = "bold"))
km_plot$cumevents <- km_plot$cumevents +
theme(plot.title = element_text(size = 10, color = "black", face = "bold"))
km_plot$table$theme$axis.text.y$size <- 8
km_plot$cumevents$theme$axis.text.y$size <- 8
km_plot$table$theme$axis.text.x$size <- 8
km_plot$cumevents$theme$axis.text.x$size <- 8
km_plot$table$theme$axis.title.x$size <- 8
km_plot$cumevents$theme$axis.title.x$size <- 8
km_plot$plot <- km_plot$plot +
scale_y_continuous(expand = c(0,0), breaks = seq(0,1,by=0.1), labels = seq(0,100,by=10), minor_breaks = seq(0, 1, 0.1)) +
scale_x_continuous(expand = c(0,0), breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int), minor_breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int / time_int)) +
theme(panel.grid.minor.y = element_line(color = "grey80",
linewidth = 0.5,
linetype = 1),
panel.grid.minor.x = element_line(color = "grey80",
linewidth = 0.5,
linetype = 1),
panel.grid.major.y = element_line(color = "grey80",
linewidth = 0.5,
linetype = 1),
panel.grid.major.x = element_line(color = "grey80",
linewidth = 0.5,
linetype = 1)) +
theme(legend.text = element_text(size = 12, color = "black", face = "bold"))
km_plot
Just use the same x
scale on km_plot$table
as you did with km_plot$plot
km_plot$table <- km_plot$table + layer_scales(km_plot$plot)$x
km_plot