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 ggtheme

近年来,R语言开始流行,我也了解了一下R语言里ggplot2的作图,知道了ggthemes主题包。它将一些期刊、软件的图表风格进行了整理,做成扩展包,方便地格式化出不同风格的图表.

ggthemes有多种风格的主题,其中最有商务气质的当属 wsjeconomist 主题,也就是华尔街日报和经济学人的风格。这一篇帖子,就是要从ggthemes里提取华尔街日报的图表配色方案。

iris %>% 
  ggplot(aes(x = Sepal.Length,Sepal.Width,col = Species)) +
  geom_point() +
  ggthemes::theme_wsj()  -> p
p

scale_fill_wsj()设置序列的填充颜色,有5种参数,分别是 colors6rgbyred_greenblack_greendem_rep 五组颜色,显示效果如下。

p+scale_fill_wsj("colors6", "")+theme_wsj()

p+scale_fill_wsj("rgby", "")+theme_wsj()

p+scale_fill_wsj("red_green", "")+theme_wsj()

p+scale_fill_wsj("black_green", "")+theme_wsj()

p+scale_fill_wsj("dem_rep", "")+theme_wsj()

theme_wsj() 里面可以设置图表的背景色,有4种参数,分别是 browngraygreenblue,显示效果如下。序列颜色和背景颜色可以有不同的搭配组合。

p+scale_fill_wsj("colors6", "")+theme_wsj(color="brown")

p+scale_fill_wsj("colors6", "")+theme_wsj(color="gray")

p+scale_fill_wsj("colors6", "")+theme_wsj(color="green")

p+scale_fill_wsj("colors6", "")+theme_wsj(color="blue")

print(ggthemes_data$wsj$palettes)
## $rgby
## # A tibble: 4 x 2
##   name   value  
##   <chr>  <chr>  
## 1 yellow #d3ba68
## 2 red    #d5695d
## 3 blue   #5d8ca8
## 4 green  #65a479
## 
## $red_green
## # A tibble: 2 x 2
##   name  value  
##   <chr> <chr>  
## 1 green #088158
## 2 red   #ba2f2a
## 
## $black_green
## # A tibble: 4 x 2
##   name    value  
##   <chr>   <chr>  
## 1 black   #000000
## 2 gray    #595959
## 3 ltgreen #59a77f
## 4 green   #008856
## 
## $dem_rep
## # A tibble: 3 x 2
##   name  value  
##   <chr> <chr>  
## 1 blue  #006a8e
## 2 red   #b1283a
## 3 gray  #a8a6a7
## 
## $colors6
## # A tibble: 6 x 2
##   name   value  
##   <chr>  <chr>  
## 1 red    #c72e29
## 2 blue   #016392
## 3 gold   #be9c2e
## 4 green  #098154
## 5 orange #fb832d
## 6 black  #000000
ggthemes_data$wsj$palettes$rgby
df <- data.frame(x=1:5, y=1:5)

p <- ggplot(df, aes(x=x, y=y, fill=factor(y))) +geom_bar(stat="identity")

p + scale_fill_economist() + theme_economist()

p + theme_economist(dkpanel=TRUE) + scale_fill_economist()

p + scale_fill_economist() + theme_economist_white() 

3 ggtech

ggtech 也是 ggplot 的扩展主题包,它从著名科技公司 LOGO 取色做成图表主题

library(ggtech)
df <- data.frame(x=1:5, y=1:5)
df %>% ggplot(aes(x,y,fill = factor(x))) +
  geom_col() +
  theme_tech(theme="Airbnb") +
  scale_fill_tech(theme="airbnb") +ggtitle("Airbnb, ExcelPro")

df <- data.frame(x=1:4, y=1:4)
df %>% ggplot(aes(x,y,fill = factor(x))) +
  geom_col() +
  theme_tech(theme="google") +
  scale_fill_tech(theme="google") +ggtitle("google, ExcelPro")
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows字体数据
## 库里没有这样的字体系列

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows字体数据
## 库里没有这样的字体系列
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows字体数据
## 库里没有这样的字体系列
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

df <- data.frame(x=1:4, y=1:4)
df %>% ggplot(aes(x,y,fill = factor(x))) +
  geom_col() +
  theme_tech(theme="twitter") +
  scale_fill_tech(theme="twitter") +ggtitle("twitter, ExcelPro")
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows字体数据
## 库里没有这样的字体系列

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows字体数据
## 库里没有这样的字体系列
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows字体数据
## 库里没有这样的字体系列
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows字体数据库里没有这样的字体系列