Aligning figure x-axis tics with table columns

It is traditional, in plotting survival curves, to have a table under the survival plot listing numbers at risk at intervals corresponding to the tick times on the plot. I have found code that lines up plots with similar x ticks and code that lines up tables with similar column names. But I haven't found how to aline an at.risk table with the x ticks on a plot. The following code generates a plot and a table. I want to be able to align the table columns with the x-axis ticks.

Thanks in advance for any suggestions how I can accomplish this.

df1 <- data.frame(x = 0:10, y = 0:10)
p <- ggplot(df1, aes(x = x, y = y)) + geom_line(col = 'red') +
scale_x_continuous("Number", breaks = 0:9,
limits = c(0, 10))
table1 <- matrix(1:40, nrow = 4)
rownames(table1) <- paste("row", 1:4)
table2 <- tableGrob(table1, rows = rownames(table1),
theme = ttheme_minimal(base_size = 9,
padding = unit(c(0.5, 0.2), 'lines')))
b <- arrangeGrob(p, table2,
nrow = 2, heights = unit(c(2, .35),c("null", "null")))

Not an answer to your question, per se, but thought it worth nothing that survminer has functions for this (e.g. ggsurvplot()):
Drawing Survival Curves Using ggplot2 — ggsurvplot • survminer (figure below is from the post)

Thanks, mara, for the suggestion. Unfortunately the journals that I use wouldn't accept the style of graphics found in survminer. Any suggestions about how I can figure out how the survminer folks managed this? I need to make it work for the style of figure my target journal is expecting.

1 Like

This isn't really something I've done myself, so I don't know many details. Have you seen baptiste's vignette on tables in grid?

All the code for survminer is, of course, open source. It's on GitHub here, and a good place to start might be in ggsurvtable.R

1 Like

How far off is the default ggsurvplot output from the rules you have to comply with? Have you looked into the customization options survminer already has? Colors, font sizes, etc can all be changed with arguments to ggsurvplot() or to the underlying theme_survminer() and theme_cleantable(). See:

But the theming is also compatible with the usual ggplot2 methods, so there's not very many limits to what you can do. Some silly examples...

#> Loading required package: ggplot2
#> Loading required package: ggpubr
#> Loading required package: magrittr

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

# Hideous vintage Excel survival plot!
  fit, data = lung, risk.table = TRUE,
  palette = ggthemes::excel_pal()(2),
  ggtheme = ggthemes::theme_excel(),
  tables.theme = survminer::theme_cleantable() + 
    theme(panel.grid = element_blank())

# Slightly less ridiculous example
  fit, data = lung, risk.table = TRUE,
  palette = "lancet",
  ggtheme = ggthemes::theme_stata() + theme(legend.title = element_text(size = 12, vjust = 0.5)),
  tables.theme = survminer::theme_cleantable() + 
    theme(panel.grid = element_blank(),
          axis.text.y = element_text(angle = 0)

Created on 2018-10-18 by the reprex package (v0.2.1)