forked from hcp4715/R4Psy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_11.html
807 lines (666 loc) · 26.2 KB
/
chapter_11.html
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>chapter_11.knit</title>
<meta charset="utf-8" />
<meta name="author" content="" />
<script src="libs/header-attrs-2.20/header-attrs.js"></script>
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link rel="stylesheet" href="css/Font_Style.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle
<span style="font-size: 60px;">第十一章</span> <br>
<span style="font-size: 50px;">如何得到可发表的图像</span> <br>
<span style="font-size: 50px;">数据可视化进阶</span> <br>
<span style="font-size: 45px;">ggplot2</span> <br>
<br>
<br>
<span style="font-size: 30px;">胡传鹏</span> <br>
<span style="font-size: 30px;">2023/05/15</span> <br>
---
# 为什么要作图?
- “一图胜千言”
- 直觉地传达信息
- “信达雅”
- 信息量
- 逻辑性
- 美观(简洁、对称、和谐)
---
# 用什么做图?
- ggplot 2
---
class: center, middle
<span style="font-size: 60px;">11.1 Layers and Facets</span> <br>
---
<br>
<div style="display:flex; align-items:center; justify-content:center;">
<img src="https://setscholars.net/wp-content/uploads/2022/02/ggplot.png" style="width:140%; height: 140%; margin-right: -15px;">
<img src="https://pngimg.com/d/photoshop_PNG7.png" style="width:60%; height: 60%; margin-left: -40px;">
</div>
---
# <h1 lang="en">Layers</h1>
# <h4 lang="en"> </h4>
<div style="display:flex; align-items:center;">
<img src="https://cdn.educba.com/academy/wp-content/uploads/2019/09/Layers-in-Photoshop.jpg.webp" style="width:110%; height: 110%; margin-top: 20px;">
</div>
<br>
_<h4 lang="zh-CN">相信大家都用过PhotoShop,对图层这个概念一定已经非常熟悉了 </h4>_
---
# <h1 lang="en">Layers</h1>
# <h4 lang="en"> </h4>
<div style="display:flex; align-items:center; justify-content:center;">
<img src="https://static.vecteezy.com/system/resources/previews/000/363/962/original/vector-plus-sign-line-black-icon.jpg" style="width:60%; height: 60%;">
</div>
<br>
_<h4 lang="zh-CN">在ggplot2中,用 + 加号来表示一个个图层 </h4>_
---
# <h1 lang="en">Layers</h1>
<div style="display:flex; align-items:center; justify-content:center;">
<img src="https://r.qcbs.ca/workshop03/book-en/images/gglayers.png" style="width:90%; height: 90%;">
</div>
<br>
_<h4 lang="zh-CN">和PS类似,ggplot2也是通过修改每个图层的信息来美化图片 </h4>_
---
# <h1 lang="en">Facets</h1>
<div style="display:flex; align-items:center; justify-content:center;">
<img src="https://i.stack.imgur.com/EQ2sk.jpg" style="width:80%; height: 80%;">
</div>
<br>
_<h4 lang="zh-CN">Facet 也可以被认为是图层的一种,也是通过"+"加号叠加在原始图片上 </h4>_
---
# <h1 lang="zh-CN">载入包</h1>
```r
# 检查是否已安装 pacman
if (!requireNamespace("pacman", quietly = TRUE)) {
install.packages("pacman") } # 如果未安装,则安装包
# 使用p_load来载入需要的包
pacman::p_load("tidyverse", "ggplot2", "ggdist", "patchwork", "papaja")
# ggdist: plot distribution
# patchwork: 拼接图片
# papaja: APA的主题
```
---
# 传统的图
- 条形图
- 点图 + 折线图
---
# <h1 lang="zh-CN">读取数据</h1>
```r
df.mt.raw <- read.csv('./data/match/match_raw.csv',
header = T, sep=",", stringsAsFactors = FALSE)
```
---
# <h1 lang="zh-CN">清洗数据</h1>
```r
# get the mean RT of correct trials for each participants for each condition
df.mt.rt.subj <- df.mt.raw %>%
dplyr::filter(ACC == 1 & RT > 0.2) %>%
tidyr::extract(Shape, into = c("Valence", "Identity"),
regex = "(moral|immoral)(Self|Other)", remove = FALSE) %>%
dplyr::mutate(Valence = case_when(Valence == "moral" ~ "Good",
Valence == "immoral" ~ "Bad"),
RT_ms = RT * 1000) %>%
dplyr::mutate(Valence = factor(Valence, levels = c("Good", "Bad")),
Identity = factor(Identity, levels = c("Self", "Other"))) %>%
dplyr::group_by(Sub, Match, Identity, Valence) %>%
dplyr::summarise(RT_mean = mean(RT_ms)) %>%
dplyr::ungroup()
# get the overall mean RT
df.mt.rt.sum <- df.mt.rt.subj %>%
dplyr::group_by(Match, Identity, Valence) %>%
dplyr::summarise(grand_mean_RT = mean(RT_mean),
SD_RT = sd(RT_mean),
SE_RT = SD_RT/sqrt(n()-1),
n = n()) %>%
dplyr::ungroup()
```
---
# <h1 lang="zh-CN">清洗数据</h1>
```r
# get the accuracy for each participants for each condition
df.mt.acc.subj <- df.mt.raw %>%
tidyr::extract(Shape, into = c("Valence", "Identity"),
regex = "(moral|immoral)(Self|Other)", remove = FALSE) %>%
dplyr::mutate(Valence = case_when(Valence == "moral" ~ "Good",
Valence == "immoral" ~ "Bad"),
) %>%
dplyr::mutate(Valence = factor(Valence, levels = c("Good", "Bad")),
Identity = factor(Identity, levels = c("Self", "Other"))) %>%
dplyr::group_by(Sub, Match, Identity, Valence) %>%
dplyr::summarise(ACC_mean = mean(ACC)) %>%
dplyr::ungroup()
# get the overall mean acc
df.mt.acc.sum <- df.mt.acc.subj %>%
dplyr::group_by(Match, Identity, Valence) %>%
dplyr::summarise(grand_mean_ACC = mean(ACC_mean),
SD_ACC = sd(ACC_mean),
SE_ACC = SD_ACC/sqrt(n()-1),
n = n()) %>%
dplyr::ungroup()
df.mt.acc.sum.tmp <- df.mt.acc.sum %>%
dplyr::rename(grand_mean = grand_mean_ACC,
SD = SD_ACC,
SE = SE_ACC) %>%
dplyr::mutate(DV = "ACC")
df.mt.rt.sum.tmp <- df.mt.rt.sum %>%
dplyr::rename(grand_mean = grand_mean_RT,
SD = SD_RT,
SE = SE_RT) %>%
dplyr::mutate(DV = "RT")
df.mt.sum.tmp <- rbind(df.mt.acc.sum.tmp, df.mt.rt.sum.tmp)
```
---
# <h1 lang="zh-CN">查看数据</h1>
```r
df.mt.sum <- df.mt.rt.sum %>%
dplyr::left_join(., df.mt.acc.sum)
df.mt.sum
```
<img src="./picture/chp11/df.mt.sum.png" width="100%">
---
## First plot (old-fashion)
```r
p1 <- df.mt.sum %>%
dplyr::filter(Match == "match") %>%
ggplot2::ggplot(.,
aes(x = Identity,
y = grand_mean_RT,
fill = Valence)) +
ggplot2::geom_bar(stat = "identity",
position=position_dodge(),
) +
ggplot2::geom_errorbar(aes(ymin=grand_mean_RT-SE_RT,
ymax=grand_mean_RT+SE_RT),
width=.1,
position = position_dodge(.9)) +
papaja::theme_apa()
ggsave(filename = "./picture/chp11/p1.png", plot = p1, height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/p1.png" width="60%">
---
## <h1 lang="en">修改图题</h1>
```r
# 修改标题和坐标的题目
p2 <- p1 +
labs(title="Mean RT for different conditions",
x="Identity",
y="Reaction times (ms)")
ggsave(filename = "./picture/chp11/p2.png", plot = p2, height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/p2.png" width="100%">
---
## <h1 lang="en">定义y轴的range</h1>
```r
p3 <- p2 +
coord_cartesian(ylim = c(500, 800)) # +
# scale_y_continuous(limits = c(500,800))
ggsave(filename = "./picture/chp11/p3.png", plot = p3, height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/p3.png" width="100%">
---
## <h1 lang="en">定义y轴的range</h1>
```r
df.mt.sum %>%
dplyr::filter(Match == "match") %>%
ggplot2::ggplot(.,
aes(x = Identity,
y = grand_mean_ACC,
fill = Valence)) +
ggplot2::geom_bar(stat = "identity",
position=position_dodge(),
) +
ggplot2::geom_errorbar(aes(ymin=grand_mean_ACC-1.96*SE_ACC,
ymax=grand_mean_ACC+1.96*SE_ACC),
width=.1,
position = position_dodge(.9)) +
papaja::theme_apa()+
labs(title="Mean ACC for different conditions",
x="Identity",
y="Accuracy")+
coord_cartesian(ylim = c(0.5, 1))
ggsave(filename = "./picture/chp11/p3.1.png", plot = last_plot(), height = 5, width = 10, dpi = 300)
```
---
## <h1 lang="en">定义y轴的range</h1>
<img src="./picture/chp11/p3.1.png" width="100%">
---
## <h1 lang="en">定义y轴的range</h1>
```r
df.mt.sum.tmp %>%
dplyr::filter(Match == "match") %>%
ggplot2::ggplot(.,
aes(x = Identity,
y = grand_mean,
fill = Valence)) +
ggplot2::geom_bar(stat = "identity",
position=position_dodge(),
) +
ggplot2::geom_errorbar(aes(ymin=grand_mean-1.96*SE,
ymax=grand_mean+1.96*SE),
width=.1,
position = position_dodge(.9)) +
papaja::theme_apa()+
facet_wrap(~DV, scales = "free_y")
# labs(title="Mean ACC for different conditions",
# x="Identity",
# y="Accuracy")+
# coord_cartesian(ylim = c(0.5, 1))
ggsave(filename = "./picture/chp11/p3.2.png",plot = last_plot(), height = 5, width = 10, dpi = 300)
```
---
## <h1 lang="en">定义y轴的range</h1>
<img src="./picture/chp11/p3.2.png" width="100%">
---
## <h1 lang="en">点加上error bars</h1>
```r
p4 <- df.mt.sum %>%
dplyr::filter(Match == "match") %>%
ggplot2::ggplot(.,
aes(x = Identity,
y = grand_mean_RT,
group = Valence,
color = Valence)) +
ggplot2::geom_line() +
ggplot2::geom_point(size = 5) +
ggplot2::geom_errorbar(aes(ymin=grand_mean_RT-SE_RT,
ymax=grand_mean_RT+SE_RT),
width=.1,
) +
ggplot2::scale_y_continuous(limits = c(450, 900)) +
papaja::theme_apa()
ggsave(filename = "./picture/chp11/p4.png", plot = p4, height = 5, width = 10, dpi = 300)
```
---
## <h1 lang="en">点加上error bars</h1>
<img src="./picture/chp11/p4.png" width="100%">
---
## <h1 lang="en">加入个体的数据</h1>
```r
p5 <- p4 +
ggplot2::geom_point(data = df.mt.rt.subj,
aes(x=Identity,
y=RT_mean,
group = Valence))
ggsave(filename = "./picture/chp11/p5.png", plot = p5, height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/p5.png" width="100%">
---
## <h1 lang="en">jitter points' position</h1>
```r
# 太拥挤了,三种图应该错开一定的距离, 以及调整每个图形的宽度
p6 <- p4 +
ggplot2::geom_point(data = df.mt.rt.subj,
aes(x=Identity,
y=RT_mean,
group = Valence),
position = position_jitter(width = 0.1),
alpha = 0.5)
ggsave(filename = "./picture/chp11/p6.png", plot = p6, height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/p6.png" width="90%">
---
## <h1 lang="en">jitter points' position</h1>
```r
p7 <- df.mt.sum %>%
dplyr::filter(Match == "match") %>%
ggplot2::ggplot(.,
aes(x = Identity,
y = grand_mean_RT,
group = Valence,
color = Valence)) +
ggplot2::geom_line(position = position_dodge(0.5)) +
ggplot2::geom_point(size = 5,
position = position_dodge(0.5)) +
ggplot2::geom_errorbar(aes(ymin=grand_mean_RT-SE_RT,
ymax=grand_mean_RT+SE_RT),
width=.1,
position = position_dodge(0.5),
) +
ggplot2::scale_y_continuous(limits = c(450, 900)) +
papaja::theme_apa()
ggsave(filename = "./picture/chp11/p7.png", plot = p7, height = 5, width = 10, dpi = 300)
```
---
## <h1 lang="en">jitter points' position</h1>
<img src="./picture/chp11/p7.png" width="100%">
---
## <h1 lang="en">jitter points' position</h1>
```r
df.mt.rt.subj.m.plot <- df.mt.rt.subj %>%
dplyr::mutate(conds = case_when(Identity == "Self" & Valence == "Good" ~ "0.88",
Identity == "Self" & Valence == "Bad" ~ "1.12",
Identity == "Other" & Valence == "Good" ~ "1.88",
Identity == "Other" & Valence == "Bad" ~ "2.12"),
conds = as.numeric(conds)) %>%
dplyr::filter(Match == "match")
```
---
## <h1 lang="en">jitter points' position</h1>
```r
p8 <- p7 +
ggplot2::geom_point(data = df.mt.rt.subj.m.plot,
aes(x=conds,
y=RT_mean,
group = Valence),
position = position_jitter(width = 0.08),
alpha = 0.5)
ggsave(filename = "./picture/chp11/p8.png", plot = p8, height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/p8.png" width="100%">
---
## <h1 lang="en">显示个体趋势</h1>
```r
p9 <- p8 +
ggplot2::geom_line(data = df.mt.rt.subj.m.plot,
aes(x=conds,
y=RT_mean,
group=Sub),
linetype = 1,
size=0.8,
color="#000000",
alpha=0.1)
ggsave(filename = "./picture/chp11/p9.png", plot = p9, height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/p9.png" width="90%">
---
## <h1 lang="en">显示个体趋势</h1>
```r
pd <- position_dodge(0.08)
p10 <- p7 +
ggplot2::geom_point(data = df.mt.rt.subj.m.plot,
aes(x=conds,
y=RT_mean,
group=as.factor(Sub)),
position = pd,
color="#000000",
alpha = 0.05) +
ggplot2::geom_line(data = df.mt.rt.subj.m.plot,
aes(x=conds,
y=RT_mean,
group=as.factor(Sub)),
position = pd,
linetype = 1,
size=0.8,
color="#000000",
alpha=0.05)
ggsave(filename = "./picture/chp11/p10.png", plot = p10, height = 5, width = 10, dpi = 300)
```
---
## <h1 lang="en">显示个体趋势</h1>
<img src="./picture/chp11/p10.png" width="100%">
---
## <h1 lang="en">显示个体趋势</h1>
```r
library(patchwork)
p11 <- p9 + p10 +
plot_layout(guides = 'collect')
ggsave(filename = "./picture/chp11/p11.png", plot = p11, height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/p11.png" width="100%">
---
class: center, middle
<span style="font-size: 60px;">11.2 APA theme</span> <br>
---
## <h1 lang="en">APA theme: theme_bw()</h1>
```r
# theme_bw() 会改变很多地方的颜色,让主题变成黑白,方便接下来的细节修改
p[[17]] <- p[[16]] +
ggplot2::theme_bw()
names(p)[17] <- "APA theme: theme_bw"; p[[17]]
ggsave(filename = "./picture/chp11/plot17.png", plot = p[[17]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot17.png" width="100%">
---
## <h1 lang="en">APA theme: legend.position</h1>
```r
# APA默认图例局右
p[[18]] <- p[[17]] +
ggplot2::theme(legend.position = "right",) # 也可以去掉图例
names(p)[18] <- "APA theme: legend.position"; p[[18]]
ggsave(filename = "./picture/chp11/plot18.png", plot = p[[18]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot18.png" width="100%">
---
## <h1 lang="en">APA theme: font_size</h1>
```r
# 除了字体大小,标题粗体居中
p[[19]] <- p[[18]] +
ggplot2::theme(axis.text = element_text(size = 12), # 坐标轴字体大小
axis.title = element_text(size = 12), # 坐标轴标题字体大小
strip.text = element_text(size = 12), # facet标题字体大小
plot.title = element_text(size = 12, face = "bold", hjust = 0.5)) # 图片整体标题字体大小 title粗体,居中
names(p)[19] <- "APA theme: font_size"; p[[19]]
ggsave(filename = "./picture/chp11/plot19.png", plot = p[[19]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot19.png" width="90%">
---
## <h1 lang="en">APA theme: strip.background()</h1>
```r
# 去掉facet标题底色
p[[20]] <- p[[19]] +
ggplot2::theme(strip.background = element_blank())
names(p)[20] <- "APA theme: facet_title_background"; p[[20]]
ggsave(filename = "./picture/chp11/plot20.png", plot = p[[20]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot20.png" width="100%">
---
## <h1 lang="en">APA theme: panel.grid.major</h1>
```r
# 隐藏水平网格线
p[[21]] <- p[[20]] +
ggplot2::theme(panel.grid.major=element_blank())
names(p)[21] <- "APA theme: panel.grid.major"; p[[21]]
ggsave(filename = "./picture/chp11/plot21.png", plot = p[[21]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot21.png" width="100%">
---
## <h1 lang="en">APA theme: panel.grid.minor</h1>
```r
# 隐藏垂直网格线
p[[22]] <- p[[21]] +
ggplot2::theme(panel.grid.minor=element_blank())
names(p)[22] <- "APA theme: panel.grid.minor"; p[[22]]
ggsave(filename = "./picture/chp11/plot22.png", plot = p[[22]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot22.png" width="100%">
---
## <h1 lang="en">APA theme: panel.border</h1>
```r
# 隐藏边框
p[[23]] <- p[[22]] +
ggplot2::theme(panel.border=element_blank()) # 也可以不去除边框,保留坐标轴
names(p)[23] <- "APA theme: panel.border"; p[[23]]
ggsave(filename = "./picture/chp11/plot23.png", plot = p[[23]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot23.png" width="100%">
---
## <h1 lang="en">APA theme: font_family</h1>
```r
# 改变图上字体family
p[[24]] <- p[[23]] +
ggplot2::theme(text=element_text(family='Times New Roman'))
names(p)[24] <- "APA theme: font_family"; p[[24]]
ggsave(filename = "./picture/chp11/plot24.png", plot = p[[24]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot24.png" width="100%">
---
## <h1 lang="en">APA theme: font_family</h1>
```r
# 使用windowsFonts()查看字体对应的名字是什么, 运气好,里面就有times new roman
windowsFonts()
# 如果没有,可以library(extrafont)
library(extrafont)
# 这个包很笨,只能添加一个文件夹内的字体,
# 默认添加"C:/WINDOWS/FONT/",这里面有你电脑里所有的字体
# 因此建议你把需要的字体放到桌面,
# 然后设置导入路径font_import(path = "C:/user/desktop/")
font_import(path = "C:/user/desktop/")
# 最后使用 fonttable(),查看字体是否被导入了,
# 记住times new roman对应的FamilyName
fonttable()
# 填在上面的text=element_text(family='Times New Roman')
```
---
## <h1 lang="en">Save APA theme</h1>
```r
# 将刚刚制作的apa格式保存
theme_apa <- ggplot2::theme_bw() +
ggplot2::theme(#legend.position = "none",
axis.text = element_text(size = 12), # 坐标轴字体大小
axis.title = element_text(size = 12), # 坐标轴标题字体大小
strip.text = element_text(size = 12), # facet标题字体大小
plot.title = element_text(size = 12, face = "bold", hjust = 0.5), # 图片整体标题字体大小
strip.background = element_blank(), # facet标题的背景
panel.grid.major=element_blank(), # 水平参考线
panel.grid.minor=element_blank(), # 垂直参考线
panel.border=element_blank(), # 边框
text=element_text(family='Times New Roman'), #字体family,
# papaja::theme_apa()会warning找不到Windows中字体,但其实如果你载入了,就可以在R中找到
)
# 你可以在载入了papaja(jtools)包之后,在画完图后加入 theme_apa(),获得一样的效果
```
---
## <h1 lang="en">APA theme: font_family</h1>
```r
# 和直接用papaja::theme_apa()制作的对比一下,坐标轴更好看了,其他几乎一样
p[[24]] <- p[[16]] +
papaja::theme_apa(base_family = "Times New Roman")
names(p)[24] <- "APA theme: font_family"; p[[24]]
ggsave(filename = "./picture/chp11/plot24_apa.png", plot = p[[24]], height = 5, width = 10, dpi = 300)
```
<img src="./picture/chp11/plot24_apa.png" width="100%">
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
// add `data-at-shortcutkeys` attribute to <body> to resolve conflicts with JAWS
// screen reader (see PR #262)
(function(d) {
let res = {};
d.querySelectorAll('.remark-help-content table tr').forEach(tr => {
const t = tr.querySelector('td:nth-child(2)').innerText;
tr.querySelectorAll('td:first-child .key').forEach(key => {
const k = key.innerText;
if (/^[a-z]$/.test(k)) res[k] = t; // must be a single letter (key)
});
});
d.body.setAttribute('data-at-shortcutkeys', JSON.stringify(res));
})(document);
(function() {
"use strict"
// Replace <script> tags in slides area to make them executable
var scripts = document.querySelectorAll(
'.remark-slides-area .remark-slide-container script'
);
if (!scripts.length) return;
for (var i = 0; i < scripts.length; i++) {
var s = document.createElement('script');
var code = document.createTextNode(scripts[i].textContent);
s.appendChild(code);
var scriptAttrs = scripts[i].attributes;
for (var j = 0; j < scriptAttrs.length; j++) {
s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
}
scripts[i].parentElement.replaceChild(s, scripts[i]);
}
})();
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};
for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>