探索其它的 ggplot2 图层

探索其它的 ggplot2 图层

ggplot2 包自带了很多图层,基本可以满足我们的各种绘图需要,但是有时候我们需要绘制一些“古怪”的图,就需要使用一些 ggplot2 的拓展包了,本文将介绍一些好用的 ggplot2 拓展包。

加载 R 包

1
2
3
library(tidyverse)
# install.packages('wesanderson')
library(wesanderson)

wesanderson 包里面提供了很多好看的调色板。

流图:geom_stream()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 安装
# remotes::install_github("davidsjoberg/ggstream")
library(ggstream)

# 示例数据
blockbusters

#> # A tibble: 157 x 3
#> year genre box_office
#> <dbl> <chr> <dbl>
#> 1 1977 Action 2.98
#> 2 1977 Adventure 0.209
#> 3 1977 Comedy 0.516
#> 4 1977 Drama 2.54
#> 5 1978 Action 1.92
#> 6 1978 Adventure 0.760
#> 7 1978 Comedy 1.04
#> 8 1978 Drama 0.202
#> 9 1979 Action 1.15
#> 10 1979 Adventure 0.312
#> # … with 147 more rows

# 绘图示例
ggplot(blockbusters, aes(year, box_office, fill = genre)) +
geom_stream() +
scale_fill_manual(values = wes_palette("Darjeeling2"))

山岭图:geom_density_ridges()

1
2
3
4
5
6
7
# 安装
# install.packages("ggridges")
library(ggridges)

ggplot(blockbusters, aes(x = box_office, y = genre, fill = genre)) +
geom_density_ridges(scale = 4) +
scale_fill_manual(values = wes_palette("Darjeeling2", n = 5))

桑基图:geom_sankey()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 安装
# devtools::install_github("davidsjoberg/ggsankey")
library(ggsankey)

# 准备示例数据:
mtcars %>%
as_tibble()
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows

mtcars %>%
make_long(cyl, vs, am, gear, carb) -> example_dat

example_dat %>%
as_tibble()
#> # A tibble: 160 x 4
#> x node next_x next_node
#> <fct> <dbl> <fct> <dbl>
#> 1 cyl 6 vs 0
#> 2 vs 0 am 1
#> 3 am 1 gear 4
#> 4 gear 4 carb 4
#> 5 carb 4 <NA> NA
#> 6 cyl 6 vs 0
#> 7 vs 0 am 1
#> 8 am 1 gear 4
#> 9 gear 4 carb 4
#> 10 carb 4 <NA> NA
#> # … with 150 more rows

ggplot(example_dat,
aes(x = x,
next_x = next_x,
node = node,
next_node = next_node,
fill = factor(node))) +
geom_sankey(flow.alpha = 0.6)

另一个用来绘制冲积图的包是 ggalluvial 包:

1
2
3
4
5
6
7
8
# 安装
# install.packages("ggalluvial")
library(ggalluvial)

ggplot(as.data.frame(UCBAdmissions),
aes(y = Freq, axis1 = Gender, axis2 = Dept)) +
geom_alluvium(aes(fill = Admit), width = 1/12) +
scale_fill_manual(values = wes_palette("Darjeeling2"))

凹凸图:geom_bump()

可以用于展示排名的变化。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装
# devtools::install_github("davidsjoberg/ggbump")
library(ggbump)

blockbusters %>%
dplyr::filter(genre %in% c("Action", "Comedy", "Drama")) %>%
group_by(year) %>%
mutate(rank = rank(box_office)) -> blockbusters2

ggplot(blockbusters2, aes(year, rank, color = genre)) +
geom_point(size = 7) +
geom_bump() +
scale_color_manual(values = wes_palette("Darjeeling2"))

华夫图:geom_waffle

1
2
3
4
5
6
7
8
# 安装
# install.packages("waffle", repos = "https://cinc.rud.is")
library(waffle)

ggplot(as_tibble(Titanic), aes(fill = Sex, values = n)) +
geom_waffle(n_rows = 20, color = "white") +
facet_wrap(~ Survived, ncol = 1) +
scale_fill_manual(values = wes_palette("Darjeeling2"))

蜂巢图:geom_quasirandom()

1
2
3
4
5
6
7
# 安装
# install.packages("ggbeeswarm")
library(ggbeeswarm)

ggplot(blockbusters, aes(x = genre, y = box_office, color = genre)) +
geom_quasirandom() +
scale_color_manual(values = wes_palette("Darjeeling2"))

镶嵌图

1
2
3
4
5
6
7
8
# 安装
# devtools::install_github("haleyjeppson/ggmosaic")
library(ggmosaic)

ggplot(as.data.frame(UCBAdmissions)) +
geom_mosaic(aes(x = product(Admit, Dept), fill = Gender, weight = Freq)) +
scale_fill_manual(values = wes_palette("Darjeeling2")) +
coord_flip()

该课程为免费课程,点击阅读原文即可从平台上下载推文相关文档和材料。

探索其它的 ggplot2 图层

https://tidyfriday.cn/posts/52693/

作者

Painter

发布于

2021-06-08

更新于

2021-06-23

许可协议

评论