使用ggplot2绘制区域轮廓
这里要使用到ggforce, 它是ggplot2的扩展包,擅长绘制数据轮廓以及区域放大。
# LIBRARIES ----
library(ggforce)
library(tidyquant)
library(tidyverse)
# DATA -----
mpg
# 1.0 HULL PLOT PROGRESSION ----
# - Hull Plots are used to indicate clusters / group assignment
# 1.1 Make the Base Plot ----
g1 <- mpg %>%
mutate(engine_size = str_c("Cylinder: ", cyl)) %>%
ggplot(aes(displ, hwy)) +
geom_point()
g1
# 1.2 Add Cluster Assignments by Engine Size (Cyl) ----
g2 <- g1 +
geom_mark_hull(
aes(fill = engine_size, label = engine_size),
concavity = 2.8
)
g2
# 1.3 Add Theme and Formatting ----
g3 <- g2 +
geom_smooth(se = FALSE, span = 1.0) +
expand_limits(y = 50) +
theme_tq() +
scale_fill_tq() +
labs(
title = "Fuel Economy (MPG) Trends by Engine Size and Displacement",
subtitle = "Hull plot to indicate clusters / group assignment",
y = "Highway Fuel Economy (MPG)",
x = "Engine Displacement Volume (Liters)",
fill = "",
caption = "Engine size has a negative relationship to fuel economy."
)
g3
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
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
编辑 (opens new window)
上次更新: 2021/08/10, 16:16:11