Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

  • How can i adjust the size of the inset?
  • How to position the inset at the upper right corner?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading