1 加载经常用的R包

library(pacman)
# 读数据
p_load(readxl,writexl,data.table,openxlsx,haven,rvest)
# 数据探索
p_load(tidyverse,DT,skimr,DataExplorer,explore,vtable,stringr,kableExtra,lubridate)
# 模型
p_load(grf,glmnet,caret,tidytext,fpp2,forecast,car,tseries,hdm,tidymodels,broom)
# 可视化
p_load(patchwork,ggrepel,ggcorrplot,gghighlight,ggthemes,shiny)
# 其它常用包
p_load(magrittr,listviewer,devtools,here,janitor,reticulate,jsonlite)

2 Theme Elements Reference

knitr::include_graphics(here::here("EZ9KYESUYAAzxpg.png"))

knitr::include_graphics(here::here("theme_elements.png"))

3 基础图形

mpg$class %>% unique()
## [1] "compact"    "midsize"    "suv"        "2seater"    "minivan"   
## [6] "pickup"     "subcompact"
mpg %>% 
  filter(class != "2seater",
         manufacturer  %in% c("toyota", "volkswagen")) -> mpg_df
mpg_df %>% str()
## tibble [61 x 11] (S3: tbl_df/tbl/data.frame)
##  $ manufacturer: chr [1:61] "toyota" "toyota" "toyota" "toyota" ...
##  $ model       : chr [1:61] "4runner 4wd" "4runner 4wd" "4runner 4wd" "4runner 4wd" ...
##  $ displ       : num [1:61] 2.7 2.7 3.4 3.4 4 4.7 2.2 2.2 2.4 2.4 ...
##  $ year        : int [1:61] 1999 1999 1999 1999 2008 2008 1999 1999 2008 2008 ...
##  $ cyl         : int [1:61] 4 4 6 6 6 8 4 4 4 4 ...
##  $ trans       : chr [1:61] "manual(m5)" "auto(l4)" "auto(l4)" "manual(m5)" ...
##  $ drv         : chr [1:61] "4" "4" "4" "4" ...
##  $ cty         : int [1:61] 15 16 15 15 16 14 21 21 21 21 ...
##  $ hwy         : int [1:61] 20 20 19 17 20 17 29 27 31 31 ...
##  $ fl          : chr [1:61] "r" "r" "r" "r" ...
##  $ class       : chr [1:61] "suv" "suv" "suv" "suv" ...
mpg_df
p <- mpg_df %>% 
  ggplot(aes(displ,hwy,col = factor(cyl))) +
  geom_point() +
  facet_grid(manufacturer ~ class) +
  labs(title = "hwy vs. displ") +
  scale_color_discrete(name = "Cylinders") +
  mytheme

p 

4 plot元素

p1 <- p + 
  theme(plot.background = element_rect(fill = "lemonchiffon",
                                       color = "black",
                                       size = 2),
        plot.title = element_text(hjust = 0.5, colour = "red", face = "bold"),
        plot.margin = margin(20, 20, 20, 20))
p1

5 Axis元素

p2 <- p1 + theme(axis.line = element_line(colour = "yellow", size = 1),
                 axis.text = element_text(colour = "red"),
                 axis.ticks = element_line(colour = "purple", size = 1))
p2

6 Legend元素

p3 <- p2 + theme(legend.background = element_rect(fill =  "deepskyblue2"),
                 legend.key = element_rect(fill = "lemonchiffon"),
                 legend.title = element_text(colour = "white", face = "bold"),
                 legend.text = element_text(colour = "blue"),
                 legend.margin = margin(10, 10, 10, 10))
p3

7 Panel元素

p4 <- p3 + theme(panel.background = element_rect(fill = "lightblue"),
      panel.grid = element_line(colour = "grey60", size = 0.2),
      panel.border = element_rect(colour = "black", fill = NA))
 
p4

8 Facetting Elements

p5 <- p4 + theme(strip.background = element_rect(fill = "steelblue"),
      strip.text = element_text(colour = "white"),
      panel.spacing = unit(0.3, "in"))
 
p5

9 Final Notes

从示例中可以看到,要修改plot中的单个元素,只需遵循参数: theme(element.name = element function())。这个例子中的最后一个图并不一定很漂亮,因为我使得识别相应的元素变得更加容易。