加载包

library(tidyverse)
## -- Attaching packages ------- tidyverse 1.3.0 --
## √ ggplot2 3.3.0     √ purrr   0.3.3
## √ tibble  2.1.3     √ dplyr   0.8.5
## √ tidyr   1.0.2     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.5.0
## -- Conflicts ---------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(DT)
library(patchwork)
library(skimr)
library(explore)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date

创建图形

mtcars %>% explore::describe()
## # A tibble: 11 x 8
##    variable type     na na_pct unique   min   mean    max
##    <chr>    <chr> <int>  <dbl>  <int> <dbl>  <dbl>  <dbl>
##  1 mpg      dbl       0      0     25 10.4   20.1   33.9 
##  2 cyl      dbl       0      0      3  4      6.19   8   
##  3 disp     dbl       0      0     27 71.1  231.   472   
##  4 hp       dbl       0      0     22 52    147.   335   
##  5 drat     dbl       0      0     22  2.76   3.6    4.93
##  6 wt       dbl       0      0     29  1.51   3.22   5.42
##  7 qsec     dbl       0      0     30 14.5   17.8   22.9 
##  8 vs       dbl       0      0      2  0      0.44   1   
##  9 am       dbl       0      0      2  0      0.41   1   
## 10 gear     dbl       0      0      3  3      3.69   5   
## 11 carb     dbl       0      0      6  1      2.81   8
mtcars %>% glimpse()
## Observations: 32
## Variables: 11
## $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17...
## $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4,...
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8,...
## $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, ...
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3....
## $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150,...
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90,...
## $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,...
## $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,...
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3,...
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1,...
mtcars %>% 
  ggplot(aes(mpg,disp)) +
  geom_point() +
  labs(title = "plot 1") +
  theme(plot.title = element_text(family = "Times New Roman",hjust = 0.5,size = 15),
        text = element_text(family = "Times New Roman"))->p1
p1

mtcars %>% 
  ggplot(aes(gear %>% as_factor() %>% fct_infreq(),disp)) +
  geom_boxplot() +
  labs(x = "gear",title = "plot 2") +
  theme(plot.title = element_text(family = "Times New Roman",hjust = 0.5,size = 15),
        text = element_text(family = "Times New Roman"))->p2
p2

mtcars %>% 
  ggplot(aes(hp,wt)) +
  geom_point(aes(col = mpg),size = 2) +
  theme(plot.title = element_text(family = "Times New Roman",hjust = 0.5,size = 15),
        text = element_text(family = "Times New Roman")) +
  labs(title = "plot 3")->p3
p3

mtcars %>% 
  ggplot(aes(gear %>% as_factor())) +
  geom_bar(aes(fill = cyl)) +
  facet_wrap(~cyl) +
  theme(plot.title = element_text(family = "Times New Roman",hjust = 0.5,size = 15),
        text = element_text(family = "Times New Roman")) +
  labs(title = "plot 4",x = "gear")->p4
p4

+

p1 + p2 + p3 + p4

p1 + p2

p1 + p2 + labs(subtitle = 'This will appear in the last plot')

p1 + (p2 + p3)
p1 + grid::textGrob("Some really important text")

p1 + gridExtra::tableGrob(mtcars[1:10, c('mpg', 'disp')])

plot_layout

p1 + p2 + p3 + p4 + plot_layout(nrow = 3,byrow = FALSE)

/

p1/p2

(p1 + p2) / p3

|

(p1/p2)|p3

p1 | (p2 / p3)

plot_annotation

((p1 / p2) | (p3 / p4)) + plot_annotation(title = "The surprising story about mtcars")

(p1 + p2 + p3) +
  plot_annotation(tag_levels = "I")

library(gridGraphics)
## Loading required package: grid
p1 + ~plot(mtcars$mpg,mtcars$disp)

old_par <- par(mar = c(0, 2, 0, 0), bg = NA)
p1 + wrap_elements(panel = ~plot(mtcars$mpg, mtcars$disp), clip = FALSE)
par(old_par)

-

p1 + p2 - p3 + plot_layout(ncol = 1)
(p1 + p2)/p3

(p1/(p2 + p3)/p4 + plot_layout(ncol = 1))