1 图形布局

patchwork

1.1 布局方式

Code
library(tidyverse)
#> Warning: package 'purrr' was built under R version 4.5.2
#> Warning: package 'stringr' was built under R version 4.5.2
#> Warning: package 'forcats' was built under R version 4.5.2
#> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
#> ✔ dplyr     1.1.4     ✔ readr     2.1.6
#> ✔ forcats   1.0.1     ✔ stringr   1.6.0
#> ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
#> ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
#> ✔ purrr     1.2.0     
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
#> ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(patchwork)
#> Warning: package 'patchwork' was built under R version 4.5.2
p1 <- ggplot(mpg, aes(x = drv, y = cty, color = drv)) + 
    geom_boxplot(show.legend = FALSE) + 
    labs(title = "Plot 1")

p2 <- ggplot(mpg, aes(x = drv, y = hwy, color = drv)) + 
    geom_boxplot(show.legend = FALSE) + 
    labs(title = "Plot 2")

p3 <- ggplot(mpg, aes(x = cty, color = drv, fill = drv)) + 
    geom_density(alpha = 0.5) + 
    labs(title = "Plot 3")

p4 <- ggplot(mpg, aes(x = hwy, color = drv, fill = drv)) + 
    geom_density(alpha = 0.5) + 
    labs(title = "Plot 4")

p5 <- ggplot(mpg, aes(x = cty, y = hwy, color = drv)) + 
    geom_point(show.legend = FALSE) + 
    facet_wrap(~drv) +
    labs(title = "Plot 5")
Code
p1 + p2

Code
p1 | p2

Code

p1 / p2

Code
p1 + p2 / p3 + p4

Code
p1 + p2 + p3 + plot_layout(ncol = 2)

Code
p1 | (p2 / (p3 | p4))

Code
layout <- "
AAB
C#B
CDD
"
p1 + p2 + p3 + p4 + plot_layout(design = layout)

Code
p1 + p2 + p3 + plot_layout(ncol = 2, guides = "collect")

1.2 修改子图

Code
p12 <- p1 + p2
p12[[2]] <- p12[[2]] + theme_light()
p12

Code
p1 + p4 & theme_minimal()

Code
p1 + p4 & scale_y_continuous(limits = c(NA, 45))

1.3 添加注释

Code
(p1 | p2) / p3 +                                      
    plot_annotation(title = '主标题',caption = "脚注",
                    theme = theme_gray(base_family = "mono")) &theme_minimal()+
    theme(axis.title = element_text(size=8),
          axis.text = element_text(size=8)
    )

Code

(guide_area() / (p1 + p2) / (p3 + p4) / p5) +
    plot_annotation(
        title = "City and highway mileage for cars with different drive trains",
        caption = "Source: https://fueleconomy.gov."
    ) +
    plot_layout(
        guides = "collect",
        heights = c(1, 3, 2, 4)
    ) &
    theme(legend.position = "top",
          plot.title = element_text(hjust = 0.5))

1.4 图形编号

Code
p123 <- p1 | (p2 / p3)
p123 + plot_annotation(tag_levels = "I") # Uppercase roman numerics i

Code


p123 + plot_annotation(tag_levels = "a") # lowercase letters A

Code
p123 + plot_annotation(tag_levels = "1") #  numbers

Code

p123[[2]] 

Code
p123[[2]] <- p123[[2]] + plot_layout(tag_level = "new")
p123 + plot_annotation(tag_levels = c("I","a"))

1.5 图形插入

Code
p1 + inset_element(p2, left = 0.6, bottom = 0.6, right = 1, top = 1)

Code
p24 <- p2 / p4 + plot_layout(guides = "collect")
p1 + inset_element(p24, left = 0.5, bottom = 0.05, right = 0.95, top = 0.9)

Code
p12 <- p1 + inset_element(p2, left = 0.5, bottom = 0.5, right = 0.9, top = 0.95)
p12 & theme_bw()

Code
p12 + plot_annotation(tag_levels = "A")

Back to top