-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtidyverse_ggplot2_guides.Rmd
264 lines (212 loc) · 6.21 KB
/
tidyverse_ggplot2_guides.Rmd
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# ggplot2之图例系统 {#tidyverse-ggplot2-guides}
这一章,我们一起学习ggplot2中的图例系统,内容相对简单,但还是推荐大家阅读[ggplot2官方文档](https://cran.r-project.org/web/packages/ggplot2/index.html)
## 图例系统
为了方便演示,我们还是用熟悉的配方`ggplot2::mpg`
```{r ggplot2-guides-1}
library(tidyverse)
mpg %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point()
```
如果想调整图例的样式,可以使用`guides()`函数,用法类似上节课中的`theme`函数, 具体参数为:
- 要么是`字符串` (i.e. `"color = colorbar"` or `"color = legend"`),
- 要么是`特定的函数` (i.e. `color = guide_colourbar()` or `color = guide_legend()`)
```{r ggplot2-guides-2, out.width = '99%', echo = FALSE}
knitr::include_graphics("images/ggplot2_guides.jpg")
```
以下 `guides()` 函数族用于控制图例的外观
- `guide_colorbar()`: continuous colors
- `guide_legend()`: discrete values (shapes, colors)
- `guide_axis()`: control axis text/spacing, add a secondary axis
- `guide_bins()`: creates "bins" of values in the legend
- `guide_colorsteps()`: makes colorbar discrete
```{r out.width = "80%"}
knitr::include_graphics("images/img/guides_examples.png")
```
## 案例详解
```{r ggplot2-guides-3}
mpg %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
ggtitle("This is my title") +
labs(x = "x_displ", y = "y_hwy") +
guides(color = "legend")
```
```{r ggplot2-guides-4}
mpg %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
ggtitle("This is my title") +
labs(x = "x_displ", y = "y_hwy") +
guides(color = guide_legend(
title = "my title",
label.hjust = 1
)
)
```
```{r ggplot2-guides-5}
mpg %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
ggtitle("This is my title") +
labs(x = "x_displ", y = "y_hwy") +
guides(color = guide_legend(
ncol = 4
)
)
```
```{r ggplot2-guides-51}
mpg %>%
ggplot() +
geom_jitter(aes(x = cty, y = hwy, color = class), key_glyph = draw_key_pointrange) + #<<
guides(color = guide_legend(nrow = 1)) +
theme(legend.position = "top",
axis.text = element_text(face = "italic", color = "navy"),
plot.background = element_rect(fill = "#a0d1f2"),
panel.background = element_blank(),
panel.grid = element_line(linetype = "dotdash"))
```
```{r ggplot2-guides-6}
mpg %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
ggtitle("This is my title") +
labs(x = "x_displ", y = "y_hwy") +
guides(color = guide_legend(
title = "title is too high",
title.position = "top",
title.vjust = 5,
label.position = "left",
label.hjust = 1,
label.theme = element_text(size = 15,
face = "italic",
colour = "red",
angle = 0),
keywidth = 5,
reverse = TRUE
)
)
```
## 删除其中一个图例
```{r ggplot2-guides-7}
mpg %>%
ggplot(aes(x = displ, y = hwy, color = class, size = cyl)) +
geom_point()
```
比如,我们想删除size这个图例,那么需要这样做
```{r ggplot2-guides-8}
mpg %>%
ggplot(aes(x = displ, y = hwy, color = class, size = cyl)) +
geom_point() +
guides(
color = guide_legend("type"), # keep
size = "none" # remove
)
```
或者
```{r}
mpg %>%
ggplot(aes(x = displ, y = hwy, color = class, size = cyl)) +
geom_point() +
guides(
color = guide_legend("type"), # keep
size = guide_none() # remove
)
```
## 合并图例
```{r}
library(tidyverse)
library(palmerpenguins)
penguins %>%
ggplot(
aes(x = bill_length_mm, y = bill_depth_mm,
color = body_mass_g, size = body_mass_g)
) +
geom_point(alpha = 0.6) +
scale_color_viridis_c()
```
这里color 和 size 都使用了body_mass_g映射,我们可以将两者合并
```{r}
# merge similar guides
penguins %>%
ggplot(
aes(x = bill_length_mm, y = bill_depth_mm,
color = body_mass_g,size = body_mass_g)
) +
geom_point(alpha = 0.6) +
scale_color_viridis_c() +
guides(color = guide_legend())
```
更直观的方法
```{r}
penguins %>%
ggplot(
aes(x = bill_length_mm, y = bill_depth_mm,
color = body_mass_g, size = body_mass_g)
) +
geom_point(alpha = 0.6) +
scale_color_viridis_c() +
guides(
color = guide_legend(),
size = guide_legend()
)
```
或者
```{r}
penguins %>%
ggplot(
aes(x = bill_length_mm, y = bill_depth_mm,
color = body_mass_g, size = body_mass_g)
) +
geom_point(alpha = 0.6) +
scale_color_viridis_c() +
guides(
colour = guide_legend("title"),
size = guide_legend("title")
)
```
最省力的是
```{r}
penguins %>%
ggplot(
aes(x = bill_length_mm, y = bill_depth_mm,
color = body_mass_g, size = body_mass_g)
) +
geom_point(alpha = 0.6) +
scale_color_viridis_c(guide = "legend")
```
## 小结
到了这里,ggplot2内容的差不多介绍完了,最后做下自我测试,能读懂下面代码(来源 Emi Tanaka)的意思?
```{r ggplot2-guides-9, eval=FALSE}
mtcars %>%
as_tibble() %>%
ggplot(aes(x = wt, y = mpg, shape = factor(vs), color = hp)) +
geom_point(size = 3) +
colorspace::scale_color_continuous_sequential(palette = "Dark Mint") +
scale_shape_discrete(labels = c("V-shaped", "Straight")) +
labs(
x = "Weight (1000 lbs)", y = "Miles per gallon",
title = "Motor Trend Car Road Tests",
shape = "Engine", color = "Horsepower"
) +
theme(
text = element_text(size = 18, color = "white"),
rect = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
legend.key = element_rect(fill = "black"),
axis.text = element_text(color = "white"),
plot.title.position = "plot",
plot.margin = margin(10, 10, 10, 10)
) +
guides(
shape =
guide_legend(override.aes = list(color = "white"))
)
```
```{r ggplot2-guides-10, echo = F}
# remove the objects
# rm(list=ls())
```
```{r ggplot2-guides-11, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```