forked from perlatex/R_for_Data_Science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggplot2_theme.Rmd
373 lines (299 loc) · 9.87 KB
/
ggplot2_theme.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# ggplot2之主题设置 {#ggplot2-theme}
这一章我们一起学习ggplot2中的theme elements
语法,感谢[Henry Wang](https://henrywang.nl/ggplot2-theme-elements-demonstration/)提供了很好的思路。如果需要详细了解,可以参考Hadley Wickham最新版的[《ggplot2: Elegant Graphics for Data Analysis》](https://ggplot2-book.org/),最推荐的是[ggplot2官方文档](https://cran.r-project.org/web/packages/ggplot2/index.html)
```{r ggplot2-theme-1, eval=FALSE}
theme(element_name = element_function())
```
这里`element_function()`有四个
```{r ggplot2-theme-2, eval=FALSE}
element_text()
element_line()
element_rect()
element_blank()
```
望文生义吧,内置元素函数有四个基础类型:
- `element_text()`, 文本,一般用于控制标签和标题的字体风格
- `element_line()`, 线条,一般用于控制线条或线段的颜色或线条类型
- `element_rect()`, 矩形区域,一般用于控制背景矩形的颜色或者边界线条类型
- `element_blank()` , 空白,就是不分配相应的绘图空间,即删去这个地方的绘图元素。
每个元素函数都有一系列控制外观的参数,下面我们通过具体的案例来一一介绍吧。
```{r ggplot2-theme-3}
library(tidyverse)
```
还是用让人生厌的`ggplot2::mpg`数据包吧,具体介绍请见\@ref(visual) 章。
```{r ggplot2-theme-4}
glimpse(mpg)
```
稍微做点数据整理
```{r ggplot2-theme-5}
df <- mpg %>%
as_tibble() %>%
filter(class != "2seater", manufacturer %in% c("toyota", "volkswagen"))
df
```
我相信这种图你们已经会画了吧
```{r ggplot2-theme-6}
df %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
facet_grid(vars(manufacturer), vars(class)) +
ggtitle("这是我的标题") +
labs(x = "x_displ", y = "y_hwy")
```
想让这张图,符合你的想法?如何**控制**呢?come on
## 图表整体元素
图表整体元素包括:
| 描述 | 主题元素 | 类型 |
|--------------|------------------|----------------|
|整个图形背景 | plot.background | element_rect()|
|图形标题 | plot.title | element_text()|
|图形边距 | plot.margin | margin()|
```{r ggplot2-theme-7}
df %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
facet_grid(vars(manufacturer), vars(class)) +
ggtitle("这是我的标题") +
labs(x = "x_displ", y = "y_hwy") +
theme(
plot.background = element_rect(fill = "orange", color = "black", size = 10),
plot.title = element_text(hjust = 1, color = "red", face = "italic"),
plot.margin = margin(t = 20, r = 20, b = 20, l = 20, unit = "pt")
)
```
## 坐标轴元素
坐标轴元素包括:
| 描述 | 主题元素 | 类型 |
|--------------|------------------|----------------|
|坐标轴刻度 | axis.ticks | element_line()|
|坐标轴标题 | axis.title | element_text()|
|坐标轴标签 | axis.text | element_text()|
|直线和坐标轴 | axis.line | element_line()|
```{r ggplot2-theme-8}
df %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
facet_grid(vars(manufacturer), vars(class)) +
ggtitle("这是我的标题") +
labs(x = "x_displ", y = "y_hwy") +
theme(
axis.line = element_line(color = "orange", size = 2),
axis.title = element_text(color = "red", face = "italic"),
axis.ticks = element_line(color = "purple", size = 3),
axis.text = element_text(color = "blue"),
axis.text.x = element_text(angle = 45, hjust = 1)
)
```
## 面板元素
面板元素包括:
| 描述 | 主题元素 | 类型 |
|--------------|------------------|----------------|
|面板背景 | panel.background | element_rect() |
|面板网格线 | panel.grid | element_line() |
|面板边界 | panel.border | element_rect() |
```{r ggplot2-theme-9}
df %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
facet_grid(vars(manufacturer), vars(class)) +
ggtitle("这是我的标题") +
labs(x = "x_displ", y = "y_hwy") +
theme(
panel.background = element_rect(fill = "orange", color = "red"),
panel.grid = element_line(color = "grey80", size = 0.5)
)
```
或者
```{r ggplot2-theme-10}
df %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
facet_grid(vars(manufacturer), vars(class)) +
ggtitle("这是我的标题") +
labs(x = "x_displ", y = "y_hwy") +
theme(
panel.background = element_rect(fill = "orange"),
panel.grid = element_line(color = "grey80", size = 0.5),
panel.border = element_rect(color = "red", fill = NA)
)
```
## 图例元素
图例元素包括:
| 描述 | 主题元素 | 类型 |
|--------------|------------------|----------------|
|图例背景 | legend.background| element_rect() |
|图例符号 | legend.key | element_rect() |
|图例标签 | legend.text | element_text() |
|图例标题 | legend.title | element_text() |
|图例边距 | legend.margin | margin |
|图例位置 | legend.postion |"top", "bottom", "left", "right"|
```{r ggplot2-theme-11}
df %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
facet_grid(vars(manufacturer), vars(class)) +
ggtitle("这是我的标题") +
labs(x = "x_displ", y = "y_hwy") +
theme(
legend.background = element_rect(fill = "orange"),
legend.title = element_text(color = "blue", size = 10),
legend.key = element_rect(fill = "grey80"),
legend.text = element_text(color = "red"),
legend.margin = margin(t = 20, r = 20, b = 20, l = 20, unit = "pt"),
legend.position = "bottom"
)
```
## 分面元素
分面元素包括:
| 描述 | 主题元素 | 类型 |
|--------------|------------------|----------------|
| 分面标签背景 | strip.background | element_rect() |
| 条状文本 | strip.text | element_text() |
| 分面间隔 | panel.spacing | unit |
```{r ggplot2-theme-12}
df %>%
ggplot(aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
facet_grid(vars(manufacturer), vars(class)) +
ggtitle("这是我的标题") +
labs(x = "x_displ", y = "y_hwy") +
theme(
strip.background = element_rect(fill = "orange"),
strip.text = element_text(color = "red"),
panel.spacing = unit(0.3, "inch") # ,
# strip.switch.pad.grid =
)
```
## 案例
```{r ggplot2-theme-13}
diamonds %>%
ggplot(aes(carat, price)) +
geom_hex() +
labs(title = "Diamond") +
theme(
axis.title.x = element_text(
size = 30,
color = "red",
face = "bold",
angle = 10
),
legend.title = element_text(
size = 25,
color = "#ff6361",
margin = margin(b = 5)
),
plot.title = element_text(
size = 35,
face = "bold",
color = "blue"
)
)
```
你肯定不会觉得这图好看。
```{r ggplot2-theme-14}
library(palmerpenguins)
penguins %>%
ggplot(aes(bill_length_mm, bill_depth_mm)) +
geom_point() +
theme(
axis.line.y = element_line(
color = "black",
size = 1.2,
arrow = grid::arrow()
),
axis.line.x = element_line(
linetype = "dashed",
color = "brown",
size = 1.2
),
axis.ticks = element_line(color = "red", size = 1.1),
axis.ticks.length = unit(3, "mm"),
panel.grid.major = element_line(
color = "blue",
size = 1.2
),
panel.grid.minor = element_line(
color = "#58508d",
size = 1.2,
linetype = "dotted"
)
)
```
```{r ggplot2-theme-15}
penguins %>%
ggplot(aes(bill_length_mm, bill_depth_mm)) +
geom_point(aes(color = species)) +
theme(
legend.background = element_rect(
fill = "#fff6c2",
color = "black",
linetype = "dashed"
),
legend.key = element_rect(fill = "grey", color = "brown"),
panel.background = element_rect(
fill = "#005F59",
color = "red", size = 3
),
panel.border = element_rect(
color = "black",
fill = "transparent",
linetype = "dashed", size = 3
),
plot.background = element_rect(
fill = "#a1dce9",
color = "black",
size = 1.3
),
legend.position = "bottom"
)
```
## 小结
```{r ggplot2-theme-16, out.width = '99%', echo = FALSE}
knitr::include_graphics("images/ggplot2-themes.jpg")
```
## 提问
- ggplot2中 plot 与 panel 有区别?
- 假定数据是这样
```{r ggplot2-theme-17}
library(tidyverse)
set.seed(12)
d1 <- data.frame(x = rnorm(50, 10, 2), type = "Island #1")
d2 <- data.frame(x = rnorm(50, 18, 1.2), type = "Island #2")
dd <- bind_rows(d1, d2) %>%
set_names(c("Height", "Location"))
head(dd)
```
你画图后,交给老板看
```{r ggplot2-theme-18}
dd %>%
ggplot(aes(x = Height, fill = Location)) +
geom_histogram(binwidth = 1, color = "white") +
scale_fill_manual(values = c("green3", "turquoise3"))
```
然而,老板有点不满意,希望你要这样改
```{r ggplot2-theme-19, echo=FALSE, out.width='85%'}
knitr::include_graphics("images/advisor_email.png")
```
请用前后两章学到的内容让老板满意吧
```{r ggplot2-theme-20, eval=FALSE, include=FALSE}
ggplot(data = dd, aes(x = Height, fill = Location)) +
geom_histogram(binwidth = 1, color = "white") +
scale_fill_manual(values = c("green3", "turquoise3")) +
theme_light() +
scale_y_continuous(expand = c(0, 0)) +
labs(x = "Teacup Giraffe heights", y = "Frequency", fill = NULL) +
theme(panel.border = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "top",
legend.justification='left',
legend.background = element_rect(color = "white")
)
```
```{r ggplot2-theme-21, echo = F}
# remove the objects
# rm(list=ls())
rm(d1, d2, dd, df)
```
```{r ggplot2-theme-22, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```