rggplot2patchworksurvminer

patchwork - inset_element(): size and position


I have two plots.
The first one (big) is the one which should function as the "background plot".
The second (small) should be the inset.

Both plots:

library(survminer)
library(survival)
library(patchwork)

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

# Big plot
big <- ggsurvplot(
  fit,  
  size = 1,  
  legend.labs = c("A", "B"),
  ylim = c(0, 1),
  linetype = "strata", 
  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()
)

# Small plot (inset)
small <- ggsurvplot(
  fit,  
  size = 1,  
  legend.labs = c("A", "B"),
  legend = "none",
  ylab = "",
  ylim = c(0.7, 1),
  linetype = "strata", 
  xlim = c(0, 365),
  break.time.by = 365, 
  palette = c("#E7B800", "#2E9FDF"), 
  fontsize = 6,
  tables.theme = theme_cleantable()
)

My attempt:

big_plot <- big$plot / big$table + plot_layout(heights = c(3, 0.5))
small_plot <- small$plot

combined <- big_plot + inset_element(small_plot, left = 0.2, bottom = 1, right = 0.95, top = 1, align_to = 'plot')

ggsave("combined.pdf", combined)

Solution

  • Since you added a risk table, you have to put the small$plot inside the big$plot, not the big_plot.

    big$plot <- big$plot +
      inset_element(small$plot, left = 0.6, bottom = 0.5, right = 1, top = 1, align_to = 'plot') 
    
    big_plot <- big$plot / big$table + plot_layout(heights = c(3, 0.5))
    big_plot
    

    enter image description here