r - Conflict between multiple uses of inset_element and function plot_annotation - Stack Overflow

时间: 2025-01-06 admin 业界

A situation was encountered when merging subgraphs in using patchwork. It is necessary to use inset_element to merge multiple times and then label at the end. But in the case of labeling the following problem occurs: the diagram that was inset_element the second time is missing the annotation. does anyone know why? Does anyone know how to solve this problem?

library(ggplot2)
p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')

p1n <- p1 + inset_element(
  p4,
  left = 0.05,
  bottom = 0.05,
  right = 0.25,
  top = 0.25,
  ignore_tag = TRUE)

p2n <- p2 + inset_element(
  p4,
  left = 0.05,
  bottom = 0.05,
  right = 0.25,
  top = 0.25,
  ignore_tag = TRUE)

patchwork <- (p1n + p2n) / p3
patchwork + plot_annotation(tag_levels = 'A')