拼凑 - inset_element():大小和位置

问题描述 投票:0回答:1

我有两个地块。
第一个 (

big
) 应该充当“背景图”。
第二个 (
small
) 应该是插图。

  • 如何调整插图大小?
  • 如何将插图定位在右上角?

两幅图:

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()
)

我的尝试:

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)
r ggplot2 patchwork survminer
1个回答
1
投票

由于您添加了风险表,因此您必须将小$图放在

big$plot
内,而不是
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

© www.soinside.com 2019 - 2024. All rights reserved.