-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathggplot2-plot.html
1063 lines (1024 loc) · 116 KB
/
ggplot2-plot.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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>4 使用 ggplot2 包绘图 | 庄闪闪的可视化手册</title>
<meta name="description" content="这是用R的bookdown功能制作中文图书的模板,输出格式为bookdown::gitbook和bookdown::pdf_book." />
<meta name="generator" content="bookdown 0.26 and GitBook 2.6.7" />
<meta property="og:title" content="4 使用 ggplot2 包绘图 | 庄闪闪的可视化手册" />
<meta property="og:type" content="book" />
<meta property="og:description" content="这是用R的bookdown功能制作中文图书的模板,输出格式为bookdown::gitbook和bookdown::pdf_book." />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="4 使用 ggplot2 包绘图 | 庄闪闪的可视化手册" />
<meta name="twitter:description" content="这是用R的bookdown功能制作中文图书的模板,输出格式为bookdown::gitbook和bookdown::pdf_book." />
<meta name="author" content="庄闪闪" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="causal.html"/>
<link rel="next" href="main-diagram-types.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<script src="libs/htmlwidgets-1.5.4/htmlwidgets.js"></script>
<script src="libs/plotly-binding-4.10.0/plotly.js"></script>
<script src="libs/typedarray-0.1/typedarray.min.js"></script>
<link href="libs/crosstalk-1.2.0/css/crosstalk.min.css" rel="stylesheet" />
<script src="libs/crosstalk-1.2.0/js/crosstalk.min.js"></script>
<link href="libs/plotly-htmlwidgets-css-2.5.1/plotly-htmlwidgets.css" rel="stylesheet" />
<script src="libs/plotly-main-2.5.1/plotly-latest.min.js"></script>
<link href="libs/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="libs/datatables-binding-0.23/datatables.js"></script>
<link href="libs/dt-core-1.11.3/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="libs/dt-core-1.11.3/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="libs/dt-core-1.11.3/js/jquery.dataTables.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
jax: ["input/TeX","output/SVG"],
extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"],
TeX: {
extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
}
});
</script>
<script type="text/javascript"
src="../../../MathJax/MathJax.js">
</script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>简介</a></li>
<li class="part"><span><b>I R 语言基础</b></span></li>
<li class="chapter" data-level="1" data-path="base-tutorial.html"><a href="base-tutorial.html"><i class="fa fa-check"></i><b>1</b> Base 系列</a></li>
<li class="chapter" data-level="2" data-path="tidyverse-tutorial.html"><a href="tidyverse-tutorial.html"><i class="fa fa-check"></i><b>2</b> Tidyverse 系列</a></li>
<li class="part"><span><b>II R 语言可视化</b></span></li>
<li class="chapter" data-level="3" data-path="causal.html"><a href="causal.html"><i class="fa fa-check"></i><b>3</b> 使用基础包绘图</a>
<ul>
<li class="chapter" data-level="3.1" data-path="causal.html"><a href="causal.html#绘制基本图形"><i class="fa fa-check"></i><b>3.1</b> 绘制基本图形</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="causal.html"><a href="causal.html#绘制分布关系"><i class="fa fa-check"></i><b>3.1.1</b> 绘制分布关系</a></li>
<li class="chapter" data-level="3.1.2" data-path="causal.html"><a href="causal.html#绘制数据间关系"><i class="fa fa-check"></i><b>3.1.2</b> 绘制数据间关系</a></li>
<li class="chapter" data-level="3.1.3" data-path="causal.html"><a href="causal.html#绘制其他图形"><i class="fa fa-check"></i><b>3.1.3</b> 绘制其他图形</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="causal.html"><a href="causal.html#修改图形参数"><i class="fa fa-check"></i><b>3.2</b> 修改图形参数</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="causal.html"><a href="causal.html#修改颜色"><i class="fa fa-check"></i><b>3.2.1</b> 修改颜色</a></li>
<li class="chapter" data-level="3.2.2" data-path="causal.html"><a href="causal.html#修改点符号与线条"><i class="fa fa-check"></i><b>3.2.2</b> 修改点符号与线条</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="causal.html"><a href="causal.html#绘制组合图形"><i class="fa fa-check"></i><b>3.3</b> 绘制组合图形</a>
<ul>
<li class="chapter" data-level="3.3.1" data-path="causal.html"><a href="causal.html#par"><i class="fa fa-check"></i><b>3.3.1</b> par()</a></li>
<li class="chapter" data-level="3.3.2" data-path="causal.html"><a href="causal.html#layout"><i class="fa fa-check"></i><b>3.3.2</b> layout</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="causal.html"><a href="causal.html#保存图形"><i class="fa fa-check"></i><b>3.4</b> 保存图形</a>
<ul>
<li class="chapter" data-level="3.4.1" data-path="causal.html"><a href="causal.html#使用代码"><i class="fa fa-check"></i><b>3.4.1</b> 使用代码</a></li>
<li class="chapter" data-level="3.4.2" data-path="causal.html"><a href="causal.html#在-rstudio-窗口点击按钮保存"><i class="fa fa-check"></i><b>3.4.2</b> 在 Rstudio 窗口点击按钮保存</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html"><i class="fa fa-check"></i><b>4</b> 使用 ggplot2 包绘图</a>
<ul>
<li class="chapter" data-level="4.1" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#简介-1"><i class="fa fa-check"></i><b>4.1</b> 简介</a></li>
<li class="chapter" data-level="4.2" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#qplot"><i class="fa fa-check"></i><b>4.2</b> qplot</a></li>
<li class="chapter" data-level="4.3" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#ggplot2包图形语法"><i class="fa fa-check"></i><b>4.3</b> ggplot2包图形语法</a>
<ul>
<li class="chapter" data-level="4.3.1" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#对比不同画图语法"><i class="fa fa-check"></i><b>4.3.1</b> 对比不同画图语法</a></li>
<li class="chapter" data-level="4.3.2" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#思想介绍"><i class="fa fa-check"></i><b>4.3.2</b> 思想介绍</a></li>
<li class="chapter" data-level="4.3.3" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#全局变量-vs.-局部变量"><i class="fa fa-check"></i><b>4.3.3</b> 全局变量 vs. 局部变量</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#几何对象"><i class="fa fa-check"></i><b>4.4</b> 几何对象</a>
<ul>
<li class="chapter" data-level="4.4.1" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#参考书籍"><i class="fa fa-check"></i><b>4.4.1</b> 参考书籍</a></li>
</ul></li>
<li class="chapter" data-level="4.5" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#统计变换"><i class="fa fa-check"></i><b>4.5</b> 统计变换</a></li>
<li class="chapter" data-level="4.6" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#刻度scale"><i class="fa fa-check"></i><b>4.6</b> 刻度scale</a>
<ul>
<li class="chapter" data-level="4.6.1" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#丰富的刻度体系"><i class="fa fa-check"></i><b>4.6.1</b> 丰富的刻度体系</a></li>
</ul></li>
<li class="chapter" data-level="4.7" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#坐标系"><i class="fa fa-check"></i><b>4.7</b> 坐标系</a></li>
<li class="chapter" data-level="4.8" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#分面"><i class="fa fa-check"></i><b>4.8</b> 分面</a></li>
<li class="chapter" data-level="4.9" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#标题标注指南拼接"><i class="fa fa-check"></i><b>4.9</b> 标题、标注、指南、拼接</a>
<ul>
<li class="chapter" data-level="4.9.1" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#标题"><i class="fa fa-check"></i><b>4.9.1</b> 标题</a></li>
<li class="chapter" data-level="4.9.2" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#标注功能"><i class="fa fa-check"></i><b>4.9.2</b> 标注功能</a></li>
<li class="chapter" data-level="4.9.3" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#指南"><i class="fa fa-check"></i><b>4.9.3</b> 指南</a></li>
<li class="chapter" data-level="4.9.4" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#主题"><i class="fa fa-check"></i><b>4.9.4</b> 主题</a></li>
</ul></li>
<li class="chapter" data-level="4.10" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#保存图片"><i class="fa fa-check"></i><b>4.10</b> 保存图片</a></li>
<li class="chapter" data-level="4.11" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#例子"><i class="fa fa-check"></i><b>4.11</b> 例子</a>
<ul>
<li class="chapter" data-level="4.11.1" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#柱状图误差项"><i class="fa fa-check"></i><b>4.11.1</b> 柱状图+误差项</a></li>
<li class="chapter" data-level="4.11.2" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#有正值和负值的柱形图"><i class="fa fa-check"></i><b>4.11.2</b> 有正值和负值的柱形图</a></li>
<li class="chapter" data-level="4.11.3" data-path="ggplot2-plot.html"><a href="ggplot2-plot.html#合并两图"><i class="fa fa-check"></i><b>4.11.3</b> 合并两图</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="main-diagram-types.html"><a href="main-diagram-types.html"><i class="fa fa-check"></i><b>5</b> 常用图形</a>
<ul>
<li class="chapter" data-level="5.1" data-path="main-diagram-types.html"><a href="main-diagram-types.html#散点图-1"><i class="fa fa-check"></i><b>5.1</b> 散点图</a></li>
<li class="chapter" data-level="5.2" data-path="main-diagram-types.html"><a href="main-diagram-types.html#气泡图"><i class="fa fa-check"></i><b>5.2</b> 气泡图</a></li>
<li class="chapter" data-level="5.3" data-path="main-diagram-types.html"><a href="main-diagram-types.html#线性图"><i class="fa fa-check"></i><b>5.3</b> 线性图</a></li>
<li class="chapter" data-level="5.4" data-path="main-diagram-types.html"><a href="main-diagram-types.html#相关系数图热力图"><i class="fa fa-check"></i><b>5.4</b> 相关系数图/热力图</a></li>
<li class="chapter" data-level="5.5" data-path="main-diagram-types.html"><a href="main-diagram-types.html#条形图-1"><i class="fa fa-check"></i><b>5.5</b> 条形图</a></li>
<li class="chapter" data-level="5.6" data-path="main-diagram-types.html"><a href="main-diagram-types.html#箱线图-1"><i class="fa fa-check"></i><b>5.6</b> 箱线图</a></li>
<li class="chapter" data-level="5.7" data-path="main-diagram-types.html"><a href="main-diagram-types.html#小提琴图-1"><i class="fa fa-check"></i><b>5.7</b> 小提琴图</a></li>
<li class="chapter" data-level="5.8" data-path="main-diagram-types.html"><a href="main-diagram-types.html#饼图-1"><i class="fa fa-check"></i><b>5.8</b> 饼图</a></li>
<li class="chapter" data-level="5.9" data-path="main-diagram-types.html"><a href="main-diagram-types.html#冲积图"><i class="fa fa-check"></i><b>5.9</b> 冲积图</a></li>
<li class="chapter" data-level="5.10" data-path="main-diagram-types.html"><a href="main-diagram-types.html#系谱图"><i class="fa fa-check"></i><b>5.10</b> 系谱图</a></li>
<li class="chapter" data-level="5.11" data-path="main-diagram-types.html"><a href="main-diagram-types.html#哑铃图"><i class="fa fa-check"></i><b>5.11</b> 哑铃图</a></li>
<li class="chapter" data-level="5.12" data-path="main-diagram-types.html"><a href="main-diagram-types.html#斜率图"><i class="fa fa-check"></i><b>5.12</b> 斜率图</a></li>
<li class="chapter" data-level="5.13" data-path="main-diagram-types.html"><a href="main-diagram-types.html#堆叠面积图"><i class="fa fa-check"></i><b>5.13</b> 堆叠面积图</a></li>
<li class="chapter" data-level="5.14" data-path="main-diagram-types.html"><a href="main-diagram-types.html#棒棒糖图"><i class="fa fa-check"></i><b>5.14</b> 棒棒糖图</a></li>
<li class="chapter" data-level="5.15" data-path="main-diagram-types.html"><a href="main-diagram-types.html#树状图"><i class="fa fa-check"></i><b>5.15</b> 树状图</a></li>
<li class="chapter" data-level="5.16" data-path="main-diagram-types.html"><a href="main-diagram-types.html#瀑布图"><i class="fa fa-check"></i><b>5.16</b> 瀑布图</a></li>
<li class="chapter" data-level="5.17" data-path="main-diagram-types.html"><a href="main-diagram-types.html#双标图"><i class="fa fa-check"></i><b>5.17</b> 双标图</a></li>
<li class="chapter" data-level="5.18" data-path="main-diagram-types.html"><a href="main-diagram-types.html#雷达图星图蜘蛛图"><i class="fa fa-check"></i><b>5.18</b> 雷达图/星图/蜘蛛图</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="other-packages.html"><a href="other-packages.html"><i class="fa fa-check"></i><b>6</b> 其他相关拓展包</a>
<ul>
<li class="chapter" data-level="6.1" data-path="other-packages.html"><a href="other-packages.html#ggplot2-官网-109-种拓展包"><i class="fa fa-check"></i><b>6.1</b> ggplot2 官网 109 种拓展包</a></li>
<li class="chapter" data-level="6.2" data-path="other-packages.html"><a href="other-packages.html#整理的相关扩展包"><i class="fa fa-check"></i><b>6.2</b> 整理的相关扩展包</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="interactive-ploting.html"><a href="interactive-ploting.html"><i class="fa fa-check"></i><b>7</b> 认识交互式绘图工具</a>
<ul>
<li class="chapter" data-level="7.1" data-path="interactive-ploting.html"><a href="interactive-ploting.html#leaflet包"><i class="fa fa-check"></i><b>7.1</b> leaflet包</a></li>
<li class="chapter" data-level="7.2" data-path="interactive-ploting.html"><a href="interactive-ploting.html#dygraphs包"><i class="fa fa-check"></i><b>7.2</b> dygraphs包</a></li>
<li class="chapter" data-level="7.3" data-path="interactive-ploting.html"><a href="interactive-ploting.html#plotly包"><i class="fa fa-check"></i><b>7.3</b> plotly包</a></li>
<li class="chapter" data-level="7.4" data-path="interactive-ploting.html"><a href="interactive-ploting.html#dt包"><i class="fa fa-check"></i><b>7.4</b> DT包</a></li>
<li class="chapter" data-level="7.5" data-path="interactive-ploting.html"><a href="interactive-ploting.html#networkd3包"><i class="fa fa-check"></i><b>7.5</b> networkD3包</a></li>
<li class="chapter" data-level="7.6" data-path="interactive-ploting.html"><a href="interactive-ploting.html#利用shiny包实现可交互的web应用"><i class="fa fa-check"></i><b>7.6</b> 利用Shiny包实现可交互的Web应用</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="some-tips-alls.html"><a href="some-tips-alls.html"><i class="fa fa-check"></i><b>8</b> 常用案例</a>
<ul>
<li class="chapter" data-level="8.1" data-path="some-tips-alls.html"><a href="some-tips-alls.html#绘制不同分布的-qq-图"><i class="fa fa-check"></i><b>8.1</b> 绘制不同分布的 QQ 图</a>
<ul>
<li class="chapter" data-level="8.1.1" data-path="some-tips-alls.html"><a href="some-tips-alls.html#简单版本"><i class="fa fa-check"></i><b>8.1.1</b> 简单版本</a></li>
<li class="chapter" data-level="8.1.2" data-path="some-tips-alls.html"><a href="some-tips-alls.html#进阶版本"><i class="fa fa-check"></i><b>8.1.2</b> 进阶版本</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="some-tips-alls.html"><a href="some-tips-alls.html#绘制混合密度函数图以及添加分位数线"><i class="fa fa-check"></i><b>8.2</b> 绘制混合密度函数图以及添加分位数线</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="some-tips-alls.html"><a href="some-tips-alls.html#加载包"><i class="fa fa-check"></i><b>8.2.1</b> 加载包</a></li>
<li class="chapter" data-level="8.2.2" data-path="some-tips-alls.html"><a href="some-tips-alls.html#产生数据集"><i class="fa fa-check"></i><b>8.2.2</b> 产生数据集</a></li>
<li class="chapter" data-level="8.2.3" data-path="some-tips-alls.html"><a href="some-tips-alls.html#绘制密度函数图并添加分位数线"><i class="fa fa-check"></i><b>8.2.3</b> 绘制密度函数图并添加分位数线</a></li>
<li class="chapter" data-level="8.2.4" data-path="some-tips-alls.html"><a href="some-tips-alls.html#合并两图使用cowplot包"><i class="fa fa-check"></i><b>8.2.4</b> 合并两图(使用cowplot包)</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="some-tips-alls.html"><a href="some-tips-alls.html#绘制不同参数的密度函数"><i class="fa fa-check"></i><b>8.3</b> 绘制不同参数的密度函数</a>
<ul>
<li class="chapter" data-level="8.3.1" data-path="some-tips-alls.html"><a href="some-tips-alls.html#beta-密度函数"><i class="fa fa-check"></i><b>8.3.1</b> Beta 密度函数</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="some-tips-alls.html"><a href="some-tips-alls.html#散点图中加入第三变量的密度函数"><i class="fa fa-check"></i><b>8.4</b> 散点图中加入第三变量的密度函数</a></li>
<li class="chapter" data-level="8.5" data-path="some-tips-alls.html"><a href="some-tips-alls.html#绘制-gamma-分布相关图形"><i class="fa fa-check"></i><b>8.5</b> 绘制 Gamma 分布相关图形</a></li>
<li class="chapter" data-level="8.6" data-path="some-tips-alls.html"><a href="some-tips-alls.html#纯手动复现密度图"><i class="fa fa-check"></i><b>8.6</b> 纯手动复现密度图</a></li>
<li class="chapter" data-level="8.7" data-path="some-tips-alls.html"><a href="some-tips-alls.html#绘制贝叶斯分析相关图形"><i class="fa fa-check"></i><b>8.7</b> 绘制贝叶斯分析相关图形</a>
<ul>
<li class="chapter" data-level="8.7.1" data-path="some-tips-alls.html"><a href="some-tips-alls.html#模型建模"><i class="fa fa-check"></i><b>8.7.1</b> 模型建模</a></li>
<li class="chapter" data-level="8.7.2" data-path="some-tips-alls.html"><a href="some-tips-alls.html#后验预测检查"><i class="fa fa-check"></i><b>8.7.2</b> 后验预测检查</a></li>
<li class="chapter" data-level="8.7.3" data-path="some-tips-alls.html"><a href="some-tips-alls.html#诊断"><i class="fa fa-check"></i><b>8.7.3</b> 诊断</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>III R 语言文档沟通</b></span></li>
<li class="chapter" data-level="9" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html"><i class="fa fa-check"></i><b>9</b> R Markdown 入门教程</a>
<ul>
<li class="chapter" data-level="9.1" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#rmarkdown-简介"><i class="fa fa-check"></i><b>9.1</b> Rmarkdown 简介</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#官网视频介绍"><i class="fa fa-check"></i><b>9.1.1</b> 官网视频介绍</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#rmarkdown-流程演示"><i class="fa fa-check"></i><b>9.2</b> Rmarkdown 流程演示</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#安装"><i class="fa fa-check"></i><b>9.2.1</b> 安装</a></li>
<li class="chapter" data-level="9.2.2" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#rstudio界面介绍"><i class="fa fa-check"></i><b>9.2.2</b> Rstudio界面介绍</a></li>
<li class="chapter" data-level="9.2.3" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#代码输出"><i class="fa fa-check"></i><b>9.2.3</b> 代码输出</a></li>
<li class="chapter" data-level="9.2.4" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#图片输出"><i class="fa fa-check"></i><b>9.2.4</b> 图片输出</a></li>
<li class="chapter" data-level="9.2.5" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#表格输出"><i class="fa fa-check"></i><b>9.2.5</b> 表格输出</a></li>
<li class="chapter" data-level="9.2.6" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#表格渲染"><i class="fa fa-check"></i><b>9.2.6</b> 表格渲染</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#rmarkdown-的主题格式"><i class="fa fa-check"></i><b>9.3</b> Rmarkdown 的主题格式</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#rmdformats-包"><i class="fa fa-check"></i><b>9.3.1</b> rmdformats 包</a></li>
<li class="chapter" data-level="9.3.2" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#tufte-包"><i class="fa fa-check"></i><b>9.3.2</b> tufte 包</a></li>
<li class="chapter" data-level="9.3.3" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#cerulean-包"><i class="fa fa-check"></i><b>9.3.3</b> cerulean 包</a></li>
</ul></li>
<li class="chapter" data-level="9.4" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#总结一些常用技巧"><i class="fa fa-check"></i><b>9.4</b> 总结一些常用技巧</a>
<ul>
<li class="chapter" data-level="9.4.1" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#修改某些字体颜色"><i class="fa fa-check"></i><b>9.4.1</b> 修改某些字体颜色</a></li>
<li class="chapter" data-level="9.4.2" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#更改全文页边距等"><i class="fa fa-check"></i><b>9.4.2</b> 更改全文页边距等</a></li>
<li class="chapter" data-level="9.4.3" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#缩进文本"><i class="fa fa-check"></i><b>9.4.3</b> 缩进文本</a></li>
<li class="chapter" data-level="9.4.4" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#分页"><i class="fa fa-check"></i><b>9.4.4</b> 分页</a></li>
<li class="chapter" data-level="9.4.5" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#控制文本输出的宽度"><i class="fa fa-check"></i><b>9.4.5</b> 控制文本输出的宽度</a></li>
<li class="chapter" data-level="9.4.6" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#控制图片输出大小"><i class="fa fa-check"></i><b>9.4.6</b> 控制图片输出大小</a></li>
<li class="chapter" data-level="9.4.7" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#图片对齐"><i class="fa fa-check"></i><b>9.4.7</b> 图片对齐</a></li>
<li class="chapter" data-level="9.4.8" data-path="rmarkdown-base.html"><a href="rmarkdown-base.html#多列输出"><i class="fa fa-check"></i><b>9.4.8</b> 多列输出</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html"><i class="fa fa-check"></i><b>10</b> 其他方式的文档沟通</a>
<ul>
<li class="chapter" data-level="10.1" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#typora"><i class="fa fa-check"></i><b>10.1</b> Typora</a></li>
<li class="chapter" data-level="10.2" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#xaringan-幻灯片"><i class="fa fa-check"></i><b>10.2</b> xaringan 幻灯片</a></li>
<li class="chapter" data-level="10.3" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#制作幻灯片"><i class="fa fa-check"></i><b>10.3</b> 制作幻灯片</a></li>
<li class="chapter" data-level="10.4" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#rbeamer"><i class="fa fa-check"></i><b>10.4</b> Rbeamer</a></li>
<li class="chapter" data-level="10.5" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#dashboards"><i class="fa fa-check"></i><b>10.5</b> Dashboards</a></li>
<li class="chapter" data-level="10.6" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#shiny"><i class="fa fa-check"></i><b>10.6</b> shiny</a></li>
<li class="chapter" data-level="10.7" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#blogdown"><i class="fa fa-check"></i><b>10.7</b> blogdown</a></li>
<li class="chapter" data-level="10.8" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#制作简历"><i class="fa fa-check"></i><b>10.8</b> 制作简历</a></li>
<li class="chapter" data-level="10.9" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#bookdown"><i class="fa fa-check"></i><b>10.9</b> bookdown</a></li>
<li class="chapter" data-level="10.10" data-path="rmarkdown-extended.html"><a href="rmarkdown-extended.html#小编有话说"><i class="fa fa-check"></i><b>10.10</b> 小编有话说</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="appendix.html"><a href="appendix.html"><i class="fa fa-check"></i><b>11</b> 附录</a>
<ul>
<li class="chapter" data-level="" data-path="appendix.html"><a href="appendix.html#安装r和rstudio"><i class="fa fa-check"></i>安装R和Rstudio</a>
<ul>
<li class="chapter" data-level="" data-path="appendix.html"><a href="appendix.html#安装-r"><i class="fa fa-check"></i>安装 R</a></li>
<li class="chapter" data-level="" data-path="appendix.html"><a href="appendix.html#安装rstudio"><i class="fa fa-check"></i>安装RStudio</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="appendix.html"><a href="appendix.html#可能的问题"><i class="fa fa-check"></i>可能的问题</a></li>
<li class="chapter" data-level="" data-path="appendix.html"><a href="appendix.html#如何获取帮助"><i class="fa fa-check"></i>如何获取帮助</a></li>
<li class="chapter" data-level="" data-path="appendix.html"><a href="appendix.html#r-语言社区"><i class="fa fa-check"></i>R 语言社区</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="reference.html"><a href="reference.html"><i class="fa fa-check"></i><b>12</b> 参考书籍</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">庄闪闪的可视化手册</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="ggplot2-plot" class="section level1 hasAnchor" number="4">
<h1><span class="header-section-number">4</span> 使用 ggplot2 包绘图<a href="ggplot2-plot.html#ggplot2-plot" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<div id="简介-1" class="section level2 hasAnchor" number="4.1">
<h2><span class="header-section-number">4.1</span> 简介<a href="ggplot2-plot.html#简介-1" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>ggplot2 包是 Harley Wickham 在 2005
年创建的,是包含了一套全面而连贯的语法的绘图系统。</p>
<div class="figure">
<img src="figure/15.png" alt="" />
<p class="caption">Harley Wickham</p>
</div>
<p>弥补了R中创建图形缺乏一致性的缺点,且不会局限于一些已经定义好的统计图形,可以根据需要创造出任何有助于解决所遇到问题的图形。</p>
<p><strong>核心理念</strong>:将绘图与数据分离,数据相关的绘图与数据无关的绘图分离,<strong>按图层作图</strong>。</p>
</div>
<div id="qplot" class="section level2 hasAnchor" number="4.2">
<h2><span class="header-section-number">4.2</span> qplot<a href="ggplot2-plot.html#qplot" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><code>ggplot2</code>包的绘图语言与常用的绘图函数的使用方法不同,为了让读者快速使用<code>ggplot2</code>包,包的作者Harley
Wickham提供了<code>qplot</code>函数(quick
plot),让人在了解<code>ggplot2</code>的语言逻辑之前,就能迅速实现数据的可视化。</p>
<p>鸢尾花数据集<code>iris</code></p>
<div class="sourceCode" id="cb51"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb51-1"><a href="ggplot2-plot.html#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(iris,<span class="dv">10</span>)</span></code></pre></div>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa</code></pre>
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb53-1"><a href="ggplot2-plot.html#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(iris)</span></code></pre></div>
<pre><code>## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...</code></pre>
<ul>
<li><strong>例子一:</strong></li>
</ul>
<p>创建一个以物种种类为分组的花萼长度的箱线图,箱线图的颜色依据不同的物种种类而变化。</p>
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb55-1"><a href="ggplot2-plot.html#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb55-2"><a href="ggplot2-plot.html#cb55-2" aria-hidden="true" tabindex="-1"></a><span class="fu">qplot</span>(Species, Sepal.Length, <span class="at">data =</span> iris, <span class="at">geom =</span> <span class="st">"boxplot"</span>,</span>
<span id="cb55-3"><a href="ggplot2-plot.html#cb55-3" aria-hidden="true" tabindex="-1"></a> <span class="at">fill =</span> Species,<span class="at">main =</span> <span class="st">"依据种类分组的花萼长度箱线图"</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-3-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="ggplot2-plot.html#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="fu">boxplot</span>(Sepal.Length<span class="sc">~</span>Species,<span class="at">data =</span>iris,<span class="at">main =</span> <span class="st">"依据种类分组的花萼长度箱线图"</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-3-2.png" width="672" style="display: block; margin: auto;" /></p>
<ul>
<li><strong>例子二:</strong></li>
</ul>
<p>利用<code>qplot</code>函数画出小提琴图,只需要将<code>geom</code>设置为<code>“violon”</code>即可,并添加扰动以减少数据重叠。</p>
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb57-1"><a href="ggplot2-plot.html#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="fu">qplot</span>(Species, Sepal.Length, <span class="at">data =</span> iris, <span class="at">geom =</span> <span class="fu">c</span>(<span class="st">"violin"</span>, <span class="st">"jitter"</span>), </span>
<span id="cb57-2"><a href="ggplot2-plot.html#cb57-2" aria-hidden="true" tabindex="-1"></a> <span class="at">fill =</span> Species,<span class="at">main =</span> <span class="st">"依据种类分组的花萼长度小提琴图"</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-4-1.png" width="672" style="display: block; margin: auto;" /></p>
<ul>
<li><strong>例子三:</strong></li>
</ul>
<p>建一个以花萼长度和花萼宽度的散点图,并利用颜色和符号形状区分物种种类。</p>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb58-1"><a href="ggplot2-plot.html#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="fu">qplot</span>(Sepal.Length, Sepal.Width, <span class="at">geom =</span> <span class="st">"point"</span>,<span class="at">data =</span> iris, <span class="at">colour =</span> Species, </span>
<span id="cb58-2"><a href="ggplot2-plot.html#cb58-2" aria-hidden="true" tabindex="-1"></a> <span class="at">shape =</span> Species,<span class="at">main =</span> <span class="st">"绘制花萼长度和花萼宽度的散点图"</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-5-1.png" width="672" style="display: block; margin: auto;" /></p>
<ul>
<li><strong>例子四:</strong></li>
</ul>
<p>利用facets参数绘制分面板散点图,并增加光滑曲线。</p>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb59-1"><a href="ggplot2-plot.html#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="fu">qplot</span>(Sepal.Length, Sepal.Width, <span class="at">data =</span> iris, <span class="at">geom =</span> <span class="fu">c</span>(<span class="st">"point"</span>, <span class="st">"smooth"</span>),</span>
<span id="cb59-2"><a href="ggplot2-plot.html#cb59-2" aria-hidden="true" tabindex="-1"></a> <span class="at">facets =</span> <span class="sc">~</span>Species,<span class="at">main =</span> <span class="st">"绘制分面板的散点图"</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-6-1.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="ggplot2包图形语法" class="section level2 hasAnchor" number="4.3">
<h2><span class="header-section-number">4.3</span> ggplot2包图形语法<a href="ggplot2-plot.html#ggplot2包图形语法" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><strong>推荐书籍</strong>:</p>
<p>ggplot2: Elegant Graphics for Data Analysis <a href="https://ggplot2-book.org/" class="uri">https://ggplot2-book.org/</a></p>
<p>Fundamentals of Data Visualization <a href="https://clauswilke.com/dataviz/" class="uri">https://clauswilke.com/dataviz/</a></p>
<div id="对比不同画图语法" class="section level3 hasAnchor" number="4.3.1">
<h3><span class="header-section-number">4.3.1</span> 对比不同画图语法<a href="ggplot2-plot.html#对比不同画图语法" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>以绘制iris数据集中Sepal.Length与Sepal.Width的散点图为例,分别采用内置的plot函数与ggplot2包的ggplot函数绘制散点图,对比理解ggplot2包的语言逻辑。</p>
<p>代码(三种类型):</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="ggplot2-plot.html#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 基础包</span></span>
<span id="cb60-2"><a href="ggplot2-plot.html#cb60-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(iris<span class="sc">$</span>Sepal.Length, iris<span class="sc">$</span>Sepal.Width)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-8-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb61-1"><a href="ggplot2-plot.html#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="co"># qplot()</span></span>
<span id="cb61-2"><a href="ggplot2-plot.html#cb61-2" aria-hidden="true" tabindex="-1"></a><span class="fu">qplot</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width,<span class="at">data =</span> iris,<span class="at">geom =</span> <span class="st">"point"</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-8-2.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="ggplot2-plot.html#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ggplot()</span></span>
<span id="cb62-2"><a href="ggplot2-plot.html#cb62-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data=</span> iris, <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width)) <span class="sc">+</span> <span class="co">#绘制底层画布</span></span>
<span id="cb62-3"><a href="ggplot2-plot.html#cb62-3" aria-hidden="true" tabindex="-1"></a><span class="fu">geom_point</span>() <span class="co">#在画布上添加点</span></span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-8-3.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="思想介绍" class="section level3 hasAnchor" number="4.3.2">
<h3><span class="header-section-number">4.3.2</span> 思想介绍<a href="ggplot2-plot.html#思想介绍" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p><strong>注:</strong>该部分主要参考<a href="https://bookdown.org/wangminjie/R4DS/intro-R.html#%E5%AE%89%E8%A3%85-rstudio">数据科学中的R语言——王敏杰</a>。</p>
<p>ggplot的绘图有以下几个特点。</p>
<ol style="list-style-type: decimal">
<li><p>有明确的起始(以ggplot函数开始)与终止(一句语句一幅图)。</p></li>
<li><p>ggplot2语句可以理解为一句语句绘制一幅图,然后进行图层叠加,而叠加是通过”+“号把绘图语句拼接实现的。</p></li>
</ol>
<p>ggplot函数包括9个部件:</p>
<ul>
<li>数据 (data) ( 数据框)</li>
<li>映射 (mapping)</li>
<li>几何对象 (geom_point() , geom_boxplot())</li>
<li>统计变换 (stats)</li>
<li>标度 (scale)</li>
<li>坐标系 (coord)</li>
<li>分面 (facet)</li>
<li>主题 (theme)</li>
<li>存储和输出 (output)</li>
</ul>
<p>其中前三个是必需的。</p>
<p>Hadley
Wickham将这套可视化语法诠释为:一张统计图形就是从<strong>数据</strong>到<strong>几何对象</strong>(geometric
object,缩写geom)的<strong>图形属性</strong>(aesthetic
attribute,缩写aes)的一个映射。</p>
<p><img src="figure/10.png" /></p>
<p>此外,图形中还可能包含数据的<strong>统计变换</strong>(statistical
transformation,缩写stats),最后绘制在某个特定的<strong>坐标系</strong>(coordinate
system,缩写coord)中,而<strong>分面</strong>(facet)则可以用来生成数据不同子集的图形。</p>
<div class="figure">
<img src="figure/11.png" alt="" />
<p class="caption">语法模板</p>
</div>
<div class="figure">
<img src="figure/22.png" alt="" />
<p class="caption">把这看懂其实差不多了</p>
</div>
<p><strong>例子(带你入门)</strong></p>
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb63-1"><a href="ggplot2-plot.html#cb63-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(Petal.Length,Petal.Width)) <span class="sc">+</span></span>
<span id="cb63-2"><a href="ggplot2-plot.html#cb63-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">size =</span> <span class="dv">2</span>,<span class="at">alpha =</span> <span class="fl">0.5</span>,<span class="at">col =</span><span class="st">"red"</span>) <span class="sc">+</span></span>
<span id="cb63-3"><a href="ggplot2-plot.html#cb63-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>(<span class="at">method =</span> <span class="st">"lm"</span>,<span class="at">se =</span> F)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-9-1.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="全局变量-vs.-局部变量" class="section level3 hasAnchor" number="4.3.3">
<h3><span class="header-section-number">4.3.3</span> 全局变量 vs. 局部变量<a href="ggplot2-plot.html#全局变量-vs.-局部变量" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb64-1"><a href="ggplot2-plot.html#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Petal.Length,<span class="at">y =</span> Petal.Width, <span class="at">col =</span> Species)) <span class="sc">+</span></span>
<span id="cb64-2"><a href="ggplot2-plot.html#cb64-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() </span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-10-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb65"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb65-1"><a href="ggplot2-plot.html#cb65-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris) <span class="sc">+</span></span>
<span id="cb65-2"><a href="ggplot2-plot.html#cb65-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Petal.Length,<span class="at">y =</span> Petal.Width, <span class="at">col =</span> Species))</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-10-2.png" width="672" style="display: block; margin: auto;" /></p>
<p>大家可以看到,以上两段代码出来的图是一样。但背后的含义却不同。</p>
<p><strong>例子(观察两者之间的区别)</strong></p>
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb66-1"><a href="ggplot2-plot.html#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="co">#版本一</span></span>
<span id="cb66-2"><a href="ggplot2-plot.html#cb66-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Petal.Length,<span class="at">y =</span> Petal.Width, <span class="at">col =</span> Species)) <span class="sc">+</span></span>
<span id="cb66-3"><a href="ggplot2-plot.html#cb66-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span> </span>
<span id="cb66-4"><a href="ggplot2-plot.html#cb66-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>() </span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-11-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb67"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb67-1"><a href="ggplot2-plot.html#cb67-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 版本二</span></span>
<span id="cb67-2"><a href="ggplot2-plot.html#cb67-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Petal.Length,<span class="at">y =</span> Petal.Width)) <span class="sc">+</span></span>
<span id="cb67-3"><a href="ggplot2-plot.html#cb67-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">col =</span> Species)) <span class="sc">+</span> </span>
<span id="cb67-4"><a href="ggplot2-plot.html#cb67-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>()</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-11-2.png" width="672" style="display: block; margin: auto;" /></p>
</div>
</div>
<div id="几何对象" class="section level2 hasAnchor" number="4.4">
<h2><span class="header-section-number">4.4</span> 几何对象<a href="ggplot2-plot.html#几何对象" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><code>geom_xxx()</code>提供了各种基本图形。 列表如下:</p>
<ul>
<li><p>基础图形:</p>
<ul>
<li><code>geom_blank()</code>不画图,可以按映射的变量设定坐标范围;</li>
<li><code>geom_point()</code>每个观测为一个散点;</li>
<li><code>geom_hline()</code>, <code>geom_vline()</code>, <code>geom_abline()</code>画线;</li>
<li><code>geom_path()</code>每个观测提供坐标,在相邻观测之间连线;</li>
<li><code>geom_ribbon()</code>需要x和ymin,
ymax维,在从小到大排序后的相邻观测之间连接阴影区域;</li>
<li><code>geom_segment()</code>需要x, y和xend, yend,为每个观测画一条线段;</li>
<li><code>geom_rect()</code>需要xmin, xmax, ymin,
ymax,为每个观测画一个长方形,可有填充色;</li>
<li><code>geom_polygon()</code>需要x,
y,将相邻观测连续并连接成一个闭合的多边形,中间填充颜色;</li>
<li><code>geom_text()</code>需要x, y和lable,每个观测画一条文字标签。</li>
</ul></li>
<li><p>单变量图层:</p>
<ul>
<li><code>geom_bar()</code>, <code>geom_col()</code>作条形图;</li>
<li><code>geom_histogram()</code>对连续变量x作直方图;</li>
<li><code>geom_density()</code>对连续变量x作一元密度估计曲线;</li>
<li><code>geom_dotplot()</code>用原点作直方图;</li>
<li><code>geom_freqpoly()</code>用折线作直方图。</li>
</ul></li>
<li><p>两变量图形:</p>
<ul>
<li><p>两个连续变量x, y:</p>
<ul>
<li><code>geom_point()</code>散点图;</li>
<li><code>geom_quantile()</code>拟合分位数回归曲线;</li>
<li><code>geom_rug()</code>在坐标轴处画数值对应的短须线;</li>
<li><code>geom_smooth()</code>画各种拟合曲线;</li>
<li><code>geom_text()</code>在指定的x, y位置画label给出的文字标签;</li>
</ul></li>
<li><p>显示二元分布:</p>
<ul>
<li><code>geom_bin2d()</code>作长方形分块的二维直方图;</li>
<li><code>geom_density2d()</code>作二元密度估计等值线图;</li>
<li><code>geom_hex()</code>作正六边形分块的二维直方图。</li>
</ul></li>
<li><p>两个变量中有分类变量时:</p>
<ul>
<li><code>geom_count()</code>:重叠点越多画点越大;</li>
<li><code>geom_jitter()</code>:
随机扰动散点位置避免重叠,数值变量有重叠时也可以用;</li>
</ul></li>
<li><p>一个连续变量和一个分类变量:</p>
<ul>
<li><code>geom_col()</code>作条形图,对分类变量的每个值画一个条形,长度与连续变量值成比例;</li>
<li><code>geom_boxplot()</code>对每个类做一个盒形图;</li>
<li><code>geom_violin()</code>对每个类做一个小提琴图。</li>
</ul></li>
<li><p>一个时间变量和一个连续变量:</p>
<ul>
<li><code>geom_area()</code>作阴影曲线图,曲线下方填充阴影色;</li>
<li><code>geom_line()</code>作折线图,在相邻两个时间之间连接线段;</li>
<li><code>geom_step()</code>作阶梯函数图,在相邻两个时间之间连接阶梯函数线。</li>
</ul></li>
<li><p>不确定性:</p>
<ul>
<li><code>geom_crossbar()</code>对每个观测输入的x, y, ymin,
ymax画中间有线的纵向条形;</li>
<li><code>geom_errbar()</code>对每个观测输入的x, ymin, ymax画纵向误差条;</li>
<li><code>geom_linerange()</code>对每个观测输入的x, ymin, ymax画一条竖线;</li>
<li><code>geom_pointrnage()</code>对每个观测输入的x, y, ymin,
ymax画一条中间有点的竖线。</li>
</ul></li>
</ul></li>
<li><p>地图:</p>
<ul>
<li><code>geom_map()</code>: 用区域边界坐标数据画边界线地图。</li>
</ul></li>
<li><p>三个变量:</p>
<ul>
<li><p><code>geom_contour()</code>: 用输入的x, y, z数据画等值线图。</p></li>
<li><p><code>geom_tile()</code>用输入的x, y位置, width,
height大小和指定的fill维画长方形色块填充图。</p></li>
<li><p><code>geom_raster()</code>是<code>geom_tile()</code>的长方形大小相同时的快速版本。</p></li>
</ul></li>
</ul>
<div id="参考书籍" class="section level3 hasAnchor" number="4.4.1">
<h3><span class="header-section-number">4.4.1</span> 参考书籍<a href="ggplot2-plot.html#参考书籍" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>由于这部分内容非常的多,短短两小时不可能讲完,这里给了一些参考资料,各个都是满满的干货。</p>
<ul>
<li><p><a href="https://bookdown.org/wangminjie/R4DS/ggplot2-geom.html#%E5%9F%BA%E6%9C%AC%E7%BB%98%E5%9B%BE">数据科学中的R-第14章ggplot之集合对象</a></p></li>
<li><p><a href="https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/ggplotvis.html">R语言教程第30节-ggplot各种图形</a></p></li>
<li><p><a href="http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html">Top 50 ggplot2
Visualizations</a></p></li>
<li><p><a href="http://r4ds.had.co.nz/data-visualisation.html">Chapter 3: Data
Visualisation</a> of <em>R
for Data Science</em></p></li>
<li><p><a href="http://r4ds.had.co.nz/graphics-for-communication.html">Chapter 28: Graphics for
communication</a>
of <em>R for Data Science</em></p></li>
<li><p><a href="https://r-graphics.org/">Graphs</a> in <em>R Graphics Cookbook</em></p></li>
</ul>
</div>
</div>
<div id="统计变换" class="section level2 hasAnchor" number="4.5">
<h2><span class="header-section-number">4.5</span> 统计变换<a href="ggplot2-plot.html#统计变换" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><strong>概念</strong>:对数据所应用的统计类型/方法。</p>
<p>ggplot2为每一种几何类型指定了一种默认的统计类型,如果仅指定geom或stat中的一个,另外一个会自动获取。其中,stat_identity则表示不做任何的统计变换。</p>
<p><strong>示例</strong>:只需指定geom或stat中的一个,具体细小细节可以参考这
<a href="https://bookdown.org/wangminjie/R4DS/ggplot2-stat-layer.html" class="uri">https://bookdown.org/wangminjie/R4DS/ggplot2-stat-layer.html</a></p>
<div class="sourceCode" id="cb68"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb68-1"><a href="ggplot2-plot.html#cb68-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(iris) <span class="sc">+</span> </span>
<span id="cb68-2"><a href="ggplot2-plot.html#cb68-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="fu">aes</span>(<span class="at">x=</span>Sepal.Length), <span class="at">stat=</span><span class="st">"bin"</span>, <span class="at">binwidth =</span> <span class="fl">0.5</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-12-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb69-1"><a href="ggplot2-plot.html#cb69-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(iris) <span class="sc">+</span> </span>
<span id="cb69-2"><a href="ggplot2-plot.html#cb69-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">stat_bin</span>(<span class="fu">aes</span>(<span class="at">x=</span>Sepal.Length), <span class="at">geom=</span><span class="st">"bar"</span>, <span class="at">binwidth =</span> <span class="fl">0.5</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-12-2.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="刻度scale" class="section level2 hasAnchor" number="4.6">
<h2><span class="header-section-number">4.6</span> 刻度scale<a href="ggplot2-plot.html#刻度scale" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>这一节我们一起学习ggplot2中的scales语法,推荐大家阅读Hadley
Wickham最新版的<a href="https://ggplot2-book.org/">《ggplot2: Elegant Graphics for Data
Analysis》</a>,但如果需要详细了解<strong>标度</strong>参数体系,还是要看<a href="https://cran.r-project.org/web/packages/ggplot2/index.html">ggplot2官方文档</a></p>
<p>在<code>ggplot()</code>的mapping参数中指定x维、y维、color维等,实际上每一维度都有一个<strong>对应的默认刻度</strong>(scale),即,将<strong>数据值映射到图形中的映射方法</strong>。</p>
<p>如果需要修改刻度对应的变换或者标度方法,可以调用相应的scale_xxx()函数。</p>
<p><strong>画图都没用到scale啊!</strong></p>
<p>能画个很漂亮的图,那是因为ggplot2默认缺省条件下,已经很美观了。(据说Hadley
Wickham很后悔使用了这么漂亮的缺省值,因为很漂亮了大家都不认真学画图了。马云好像也说后悔创立了阿里巴巴?)</p>
<div class="sourceCode" id="cb70"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb70-1"><a href="ggplot2-plot.html#cb70-1" aria-hidden="true" tabindex="-1"></a><span class="co">#解释</span></span>
<span id="cb70-2"><a href="ggplot2-plot.html#cb70-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Petal.Length,<span class="at">y =</span> Petal.Width, <span class="at">col =</span> Species)) <span class="sc">+</span></span>
<span id="cb70-3"><a href="ggplot2-plot.html#cb70-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span> </span>
<span id="cb70-4"><a href="ggplot2-plot.html#cb70-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>() </span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-13-1.png" width="672" style="display: block; margin: auto;" /></p>
<div id="丰富的刻度体系" class="section level3 hasAnchor" number="4.6.1">
<h3><span class="header-section-number">4.6.1</span> 丰富的刻度体系<a href="ggplot2-plot.html#丰富的刻度体系" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p><strong>注意:</strong>标度函数是由”_“分割的三个部分构成的 - scale - 视觉属性名
(e.g., colour, shape or x) - 标度名 (e.g., continuous, discrete,
brewer).</p>
<ul>
<li>将数据变量映射到具体的位置、颜色、填充色、大小、符号等。</li>
</ul>
<p><img src="figure/23.jpg" /></p>
<p>每个标度函数内部都有丰富的参数系统</p>
<pre><code>scale_colour_manual(
palette = function(),
limits = NULL,
name = waiver(),
labels = waiver(),
breaks = waiver(),
minor_breaks = waiver(),
values = waiver(),
...
)</code></pre>
<ul>
<li><p>参数<code>name</code>,坐标和图例的名字,如果不想要图例的名字,就可以
<code>name = NULL</code></p></li>
<li><p>参数<code>limits</code>,
坐标或图例的范围区间。连续性<code>c(n, m)</code>,离散型<code>c("a", "b", "c")</code></p></li>
<li><p>参数<code>breaks</code>, 控制显示在坐标轴或者图例上的值(元素)</p></li>
<li><p>参数<code>labels</code>, 坐标和图例的间隔标签</p>
<ul>
<li>一般情况下,内置函数会自动完成</li>
<li>也可人工指定一个字符型向量,与<code>breaks</code>提供的字符型向量一一对应</li>
<li>也可以是函数,把<code>breaks</code>提供的字符型向量当做函数的输入</li>
<li><code>NULL</code>,就是去掉标签</li>
</ul></li>
<li><p>参数<code>values</code> 指的是(颜色、形状等)视觉属性值,</p>
<ul>
<li>要么,与数值的顺序一致;</li>
<li>要么,与<code>breaks</code>提供的字符型向量长度一致</li>
<li>要么,用命名向量<code>c("数据标签" = "视觉属性")</code>提供</li>
</ul></li>
<li><p>参数<code>expand</code>, 控制参数溢出量</p></li>
<li><p>参数<code>range</code>, 设置尺寸大小范围,比如针对点的相对大小</p></li>
</ul>
<p>下面,我们通过具体的案例讲解如何使用参数,把图形变成我们想要的模样。</p>
<p><strong>例子</strong>:随机从iris数据集的150个样本中抽取100个样本,并绘制条形图反映100个样本中各个鸢尾花种类的数量情况。然后通过修改标尺参数做前后对比图,进而理解标尺在ggplot2包中的作用。</p>
<div class="sourceCode" id="cb72"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb72-1"><a href="ggplot2-plot.html#cb72-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>) <span class="co"># 设置随机种子</span></span>
<span id="cb72-2"><a href="ggplot2-plot.html#cb72-2" aria-hidden="true" tabindex="-1"></a>my_iris <span class="ot"><-</span> iris[<span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">150</span>, <span class="dv">100</span>, <span class="at">replace =</span> <span class="cn">FALSE</span>),] <span class="co"># 随机抽样</span></span>
<span id="cb72-3"><a href="ggplot2-plot.html#cb72-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb72-4"><a href="ggplot2-plot.html#cb72-4" aria-hidden="true" tabindex="-1"></a>p <span class="ot"><-</span> <span class="fu">ggplot</span>(my_iris) <span class="sc">+</span> </span>
<span id="cb72-5"><a href="ggplot2-plot.html#cb72-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="fu">aes</span>(<span class="at">x =</span> Species, <span class="at">fill =</span> Species))</span>
<span id="cb72-6"><a href="ggplot2-plot.html#cb72-6" aria-hidden="true" tabindex="-1"></a>p</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-14-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb73"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb73-1"><a href="ggplot2-plot.html#cb73-1" aria-hidden="true" tabindex="-1"></a>p <span class="sc">+</span> <span class="fu">scale_fill_manual</span>(</span>
<span id="cb73-2"><a href="ggplot2-plot.html#cb73-2" aria-hidden="true" tabindex="-1"></a> <span class="at">values =</span> <span class="fu">c</span>(<span class="st">"orange"</span>, <span class="st">"red"</span>, <span class="st">"lightyellow3"</span>), <span class="co"># 颜色设置</span></span>
<span id="cb73-3"><a href="ggplot2-plot.html#cb73-3" aria-hidden="true" tabindex="-1"></a> <span class="at">name =</span> <span class="cn">NULL</span>, <span class="co"># 图例和轴使用的名称</span></span>
<span id="cb73-4"><a href="ggplot2-plot.html#cb73-4" aria-hidden="true" tabindex="-1"></a> <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"set"</span>, <span class="st">"ver"</span>, <span class="st">"vir"</span>) <span class="co"># 图例使用的标签</span></span>
<span id="cb73-5"><a href="ggplot2-plot.html#cb73-5" aria-hidden="true" tabindex="-1"></a>) <span class="sc">+</span> </span>
<span id="cb73-6"><a href="ggplot2-plot.html#cb73-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"set"</span>, <span class="st">"ver"</span>, <span class="st">"vir"</span>),<span class="at">name =</span> <span class="st">"A"</span>) <span class="sc">+</span></span>
<span id="cb73-7"><a href="ggplot2-plot.html#cb73-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">name =</span> <span class="st">"B"</span>,<span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">20</span>,<span class="dv">40</span>)) <span class="sc">+</span></span>
<span id="cb73-8"><a href="ggplot2-plot.html#cb73-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>()</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-14-2.png" width="672" style="display: block; margin: auto;" /></p>
<p>使用<code>scale_color_manual</code>或<code>scale_color_brewer</code>函数修改图形的颜色。在对iris数据集中的Sepal.Length与Sepal.Width的散点图分别使用以上两种方法修改散点颜色</p>
<div class="sourceCode" id="cb74"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb74-1"><a href="ggplot2-plot.html#cb74-1" aria-hidden="true" tabindex="-1"></a><span class="co">#图一:使用scale_color_manual函数</span></span>
<span id="cb74-2"><a href="ggplot2-plot.html#cb74-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(iris, <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width, <span class="at">colour =</span> Species))<span class="sc">+</span></span>
<span id="cb74-3"><a href="ggplot2-plot.html#cb74-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">size =</span> <span class="dv">2</span>) <span class="sc">+</span></span>
<span id="cb74-4"><a href="ggplot2-plot.html#cb74-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"orange"</span>, <span class="st">"olivedrab"</span>, <span class="st">"navy"</span>),</span>
<span id="cb74-5"><a href="ggplot2-plot.html#cb74-5" aria-hidden="true" tabindex="-1"></a> <span class="at">name =</span> <span class="cn">NULL</span>) </span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-16-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb75"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb75-1"><a href="ggplot2-plot.html#cb75-1" aria-hidden="true" tabindex="-1"></a><span class="co">#图二:使用scale_color_brewer函数</span></span>
<span id="cb75-2"><a href="ggplot2-plot.html#cb75-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(iris,<span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width, <span class="at">colour =</span> Species))<span class="sc">+</span></span>
<span id="cb75-3"><a href="ggplot2-plot.html#cb75-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_grey</span>()<span class="sc">+</span></span>
<span id="cb75-4"><a href="ggplot2-plot.html#cb75-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">size=</span><span class="dv">2</span>) </span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-16-2.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb76"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb76-1"><a href="ggplot2-plot.html#cb76-1" aria-hidden="true" tabindex="-1"></a><span class="co"># library(RColorBrewer)</span></span>
<span id="cb76-2"><a href="ggplot2-plot.html#cb76-2" aria-hidden="true" tabindex="-1"></a><span class="co"># brewer.pal(3, "Set1")</span></span>
<span id="cb76-3"><a href="ggplot2-plot.html#cb76-3" aria-hidden="true" tabindex="-1"></a><span class="co"># display.brewer.all()</span></span></code></pre></div>
</div>
</div>
<div id="坐标系" class="section level2 hasAnchor" number="4.7">
<h2><span class="header-section-number">4.7</span> 坐标系<a href="ggplot2-plot.html#坐标系" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>ggplot2默认的坐标系是笛卡尔坐标系,可以用如下方法指定取值范围:<code>coord_cartesian(xlim = c(0,5), ylim = c(0, 3))</code>。</p>
<p><code>coord_flip</code>:x轴和y轴换位置。</p>
<p><code>coord_polar(theta = "x",direction=1)</code>是角度坐标系,theta指定角度对应的变量,start指定起点离12点钟方向的偏离值,direction若为1表示顺时针方向,若为-1表示逆时针方向。</p>
<div class="sourceCode" id="cb77"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb77-1"><a href="ggplot2-plot.html#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 饼图 = 堆叠长条图 + polar_coordinates</span></span>
<span id="cb77-2"><a href="ggplot2-plot.html#cb77-2" aria-hidden="true" tabindex="-1"></a>pie <span class="ot"><-</span> <span class="fu">ggplot</span>(my_iris, <span class="fu">aes</span>(<span class="at">x =</span> <span class="fu">factor</span>(<span class="dv">1</span>), <span class="at">fill =</span> Species)) <span class="sc">+</span></span>
<span id="cb77-3"><a href="ggplot2-plot.html#cb77-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">width =</span> <span class="dv">1</span>)</span>
<span id="cb77-4"><a href="ggplot2-plot.html#cb77-4" aria-hidden="true" tabindex="-1"></a>pie <span class="sc">+</span> <span class="fu">coord_polar</span>(<span class="at">theta =</span> <span class="st">"y"</span>,<span class="at">direction =</span> <span class="sc">-</span><span class="dv">1</span>,<span class="at">start =</span> <span class="dv">30</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-17-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb78"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb78-1"><a href="ggplot2-plot.html#cb78-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 靶心图 = 饼图 + polar_coordinates</span></span>
<span id="cb78-2"><a href="ggplot2-plot.html#cb78-2" aria-hidden="true" tabindex="-1"></a>pie <span class="sc">+</span> <span class="fu">coord_polar</span>()</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-17-2.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb79"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb79-1"><a href="ggplot2-plot.html#cb79-1" aria-hidden="true" tabindex="-1"></a><span class="co">#锯齿图 = 柱状图 + polar_coordinates</span></span>
<span id="cb79-2"><a href="ggplot2-plot.html#cb79-2" aria-hidden="true" tabindex="-1"></a>cxc <span class="ot"><-</span> <span class="fu">ggplot</span>(my_iris, <span class="fu">aes</span>(<span class="at">x =</span> Species)) <span class="sc">+</span></span>
<span id="cb79-3"><a href="ggplot2-plot.html#cb79-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">width =</span> <span class="dv">1</span>, <span class="at">colour =</span> <span class="st">"black"</span>)</span>
<span id="cb79-4"><a href="ggplot2-plot.html#cb79-4" aria-hidden="true" tabindex="-1"></a>cxc <span class="sc">+</span> <span class="fu">coord_polar</span>()</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-17-3.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="分面" class="section level2 hasAnchor" number="4.8">
<h2><span class="header-section-number">4.8</span> 分面<a href="ggplot2-plot.html#分面" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>分面,就是分组绘图,根据定义的规则,将数据分为多个子集,每个子集按照统一的规则单独制图,排布在一个页面上。</p>
<p>ggplot2提供两种分面方法:<code>facet_grid</code>函数和<code>facet_wrap</code>函数。</p>
<p><strong>1. facet_grid函数</strong></p>
<p>注意<code>facet_grid</code>函数是一个二维的矩形布局,每个子集的位置由行位置变量~列位置变量的决定</p>
<div class="sourceCode" id="cb80"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb80-1"><a href="ggplot2-plot.html#cb80-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb80-2"><a href="ggplot2-plot.html#cb80-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span>
<span id="cb80-3"><a href="ggplot2-plot.html#cb80-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb80-4"><a href="ggplot2-plot.html#cb80-4" aria-hidden="true" tabindex="-1"></a>my_iris1 <span class="ot"><-</span> iris <span class="sc">%>%</span> <span class="fu">gather</span>(feature_name, feature_value, <span class="fu">one_of</span>(<span class="fu">c</span>(<span class="st">"Sepal.Length"</span>, <span class="st">"Sepal.Width"</span>, <span class="st">"Petal.Length"</span>, <span class="st">"Petal.Width"</span>))) <span class="co"># 数据变换</span></span>
<span id="cb80-5"><a href="ggplot2-plot.html#cb80-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(my_iris1) <span class="sc">+</span> </span>
<span id="cb80-6"><a href="ggplot2-plot.html#cb80-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_violin</span>(<span class="fu">aes</span>(<span class="at">x =</span> Species, <span class="at">y =</span> feature_value)) <span class="sc">+</span> <span class="co"># 绘制小提琴图</span></span>
<span id="cb80-7"><a href="ggplot2-plot.html#cb80-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_grid</span>(feature_name <span class="sc">~</span> Species, <span class="at">scales =</span> <span class="st">"free"</span>) <span class="co"># 分面</span></span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-18-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb81"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb81-1"><a href="ggplot2-plot.html#cb81-1" aria-hidden="true" tabindex="-1"></a><span class="co"># iris例子</span></span>
<span id="cb81-2"><a href="ggplot2-plot.html#cb81-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width)) <span class="sc">+</span> <span class="co"># 底层画布</span></span>
<span id="cb81-3"><a href="ggplot2-plot.html#cb81-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb81-4"><a href="ggplot2-plot.html#cb81-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>() <span class="sc">+</span></span>
<span id="cb81-5"><a href="ggplot2-plot.html#cb81-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="sc">~</span>Species)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-19-1.png" width="672" style="display: block; margin: auto;" /></p>
<p><strong>2. facet_wrap函数</strong></p>
<p>facet_wrap函数生成一个动态调整的一维布局,根据”~位置变量1+位置变量2+…“来确定每个子集的位置,先逐行排列,放不下了移动到下一行。</p>
<div class="sourceCode" id="cb82"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb82-1"><a href="ggplot2-plot.html#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(my_iris1) <span class="sc">+</span></span>
<span id="cb82-2"><a href="ggplot2-plot.html#cb82-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_violin</span>(<span class="fu">aes</span>(<span class="at">x =</span> Species, <span class="at">y =</span> feature_value)) <span class="sc">+</span></span>
<span id="cb82-3"><a href="ggplot2-plot.html#cb82-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span> feature_name <span class="sc">+</span> Species, <span class="at">scales =</span> <span class="st">"free_y"</span>,<span class="at">nrow =</span> <span class="dv">3</span>,</span>
<span id="cb82-4"><a href="ggplot2-plot.html#cb82-4" aria-hidden="true" tabindex="-1"></a> <span class="at">strip.position =</span> <span class="st">"bottom"</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-20-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb83"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb83-1"><a href="ggplot2-plot.html#cb83-1" aria-hidden="true" tabindex="-1"></a><span class="co"># iris例子</span></span>
<span id="cb83-2"><a href="ggplot2-plot.html#cb83-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width)) <span class="sc">+</span> <span class="co"># 底层画布</span></span>
<span id="cb83-3"><a href="ggplot2-plot.html#cb83-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb83-4"><a href="ggplot2-plot.html#cb83-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>()<span class="sc">+</span></span>
<span id="cb83-5"><a href="ggplot2-plot.html#cb83-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>Species)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-21-1.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="标题标注指南拼接" class="section level2 hasAnchor" number="4.9">
<h2><span class="header-section-number">4.9</span> 标题、标注、指南、拼接<a href="ggplot2-plot.html#标题标注指南拼接" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>除了<code>ggplot()</code>指定数据与映射,<code>geom_xxx()</code>作图,还可以用许多辅助函数增强图形。</p>
<ul>
<li><code>labs()</code>可以设置适当的标题和标签。</li>
<li><code>annotate()</code>函数可以直接在坐标系内进行<em>文字、符号、线段、箭头、长方形</em>的绘制。</li>
<li><code>guides()</code>函数可以控制图例的取舍以及做法。</li>
<li><code>theme()</code>函数可以控制一些整体的选项如背景色、字体类型、图例的摆放位置等。</li>
</ul>
<div id="标题" class="section level3 hasAnchor" number="4.9.1">
<h3><span class="header-section-number">4.9.1</span> 标题<a href="ggplot2-plot.html#标题" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>函数<code>labs()</code>可以用来指定图形上方的<code>标题(title)</code>、<code>副标题(subtitle)</code>、右下方的<code>标注(caption)</code>、<code>左上方的标签</code>以及<code>坐标轴标题</code>和其它维的名称。
例如:</p>
<div class="sourceCode" id="cb84"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb84-1"><a href="ggplot2-plot.html#cb84-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(gapminder)</span>
<span id="cb84-2"><a href="ggplot2-plot.html#cb84-2" aria-hidden="true" tabindex="-1"></a>p <span class="ot"><-</span> <span class="fu">ggplot</span>(<span class="at">data =</span> gapminder,</span>
<span id="cb84-3"><a href="ggplot2-plot.html#cb84-3" aria-hidden="true" tabindex="-1"></a> <span class="at">mapping =</span> <span class="fu">aes</span>(</span>
<span id="cb84-4"><a href="ggplot2-plot.html#cb84-4" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> gdpPercap,</span>
<span id="cb84-5"><a href="ggplot2-plot.html#cb84-5" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> lifeExp))</span>
<span id="cb84-6"><a href="ggplot2-plot.html#cb84-6" aria-hidden="true" tabindex="-1"></a>p <span class="sc">+</span> <span class="fu">geom_point</span>(<span class="at">alpha =</span> <span class="fl">0.4</span>) <span class="sc">+</span> </span>
<span id="cb84-7"><a href="ggplot2-plot.html#cb84-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb84-8"><a href="ggplot2-plot.html#cb84-8" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"各国各年度人均GDP与期望寿命的关系"</span>,</span>
<span id="cb84-9"><a href="ggplot2-plot.html#cb84-9" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"1952-2007"</span>,</span>
<span id="cb84-10"><a href="ggplot2-plot.html#cb84-10" aria-hidden="true" tabindex="-1"></a> <span class="at">tag =</span> <span class="st">"散点图"</span>,</span>
<span id="cb84-11"><a href="ggplot2-plot.html#cb84-11" aria-hidden="true" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"数据来源:gapminder"</span>,</span>
<span id="cb84-12"><a href="ggplot2-plot.html#cb84-12" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"人均GDP(单位:美元)"</span>,</span>
<span id="cb84-13"><a href="ggplot2-plot.html#cb84-13" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"期望寿命"</span></span>
<span id="cb84-14"><a href="ggplot2-plot.html#cb84-14" aria-hidden="true" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-22-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb85"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb85-1"><a href="ggplot2-plot.html#cb85-1" aria-hidden="true" tabindex="-1"></a><span class="co">#iris案例</span></span>
<span id="cb85-2"><a href="ggplot2-plot.html#cb85-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width)) <span class="sc">+</span> <span class="co"># 底层画布</span></span>
<span id="cb85-3"><a href="ggplot2-plot.html#cb85-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb85-4"><a href="ggplot2-plot.html#cb85-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>() <span class="sc">+</span></span>
<span id="cb85-5"><a href="ggplot2-plot.html#cb85-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb85-6"><a href="ggplot2-plot.html#cb85-6" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"22"</span>,</span>
<span id="cb85-7"><a href="ggplot2-plot.html#cb85-7" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"22"</span>,</span>
<span id="cb85-8"><a href="ggplot2-plot.html#cb85-8" aria-hidden="true" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"22"</span></span>
<span id="cb85-9"><a href="ggplot2-plot.html#cb85-9" aria-hidden="true" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-23-1.png" width="672" style="display: block; margin: auto;" /></p>
<p><code>labs()</code>只是提供了这些标题功能,一般并不会同时使用这些功能。
在出版图书内,图形下方一般伴随有图形说明,这时一般就不再使用标题、副标题、标签、标注,而只需写在图的伴随说明文字中,当然,坐标轴标签一般还是需要的。</p>
</div>
<div id="标注功能" class="section level3 hasAnchor" number="4.9.2">
<h3><span class="header-section-number">4.9.2</span> 标注功能<a href="ggplot2-plot.html#标注功能" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>通过<code>annotate(geom = "text")</code>调用<code>geom_text()</code>的功能,
可以在一个散点图中标注多行文字,多行之间用<code>"\n"</code>分开:</p>
<p>在<code>annotate()</code>中选<code>geom="rect"</code>,给出长方形的左右和上限界限,
可以将上面图形中最右侧偏低的点用长方形填充标出。
可以在<code>annotate()</code>中选<code>geom="line"</code>画线,需要给出线的起点和终点坐标,可以<code>arrow</code>选项要求画箭头,用<code>arrow()</code>函数给出箭头的大小、角度等设置,
如:</p>
<div class="sourceCode" id="cb86"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb86-1"><a href="ggplot2-plot.html#cb86-1" aria-hidden="true" tabindex="-1"></a>p <span class="sc">+</span> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb86-2"><a href="ggplot2-plot.html#cb86-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>(<span class="at">method=</span><span class="st">"gam"</span>) <span class="sc">+</span></span>
<span id="cb86-3"><a href="ggplot2-plot.html#cb86-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_log10</span>() <span class="sc">+</span></span>
<span id="cb86-4"><a href="ggplot2-plot.html#cb86-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="at">geom =</span> <span class="st">"rect"</span>, </span>
<span id="cb86-5"><a href="ggplot2-plot.html#cb86-5" aria-hidden="true" tabindex="-1"></a> <span class="at">xmin =</span> <span class="fl">5.5E4</span>, <span class="at">xmax =</span> <span class="fl">1.2E5</span>,</span>
<span id="cb86-6"><a href="ggplot2-plot.html#cb86-6" aria-hidden="true" tabindex="-1"></a> <span class="at">ymin =</span> <span class="dv">54</span>, <span class="at">ymax =</span> <span class="dv">71</span>,<span class="at">col =</span> <span class="st">'red'</span>,<span class="at">fill =</span> <span class="st">'red'</span>,<span class="at">alpha =</span> <span class="fl">0.5</span>) <span class="sc">+</span> </span>
<span id="cb86-7"><a href="ggplot2-plot.html#cb86-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="at">geom =</span> <span class="st">"line"</span>,</span>
<span id="cb86-8"><a href="ggplot2-plot.html#cb86-8" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="fu">c</span>(<span class="fl">5.9E4</span>, <span class="fl">3.16E4</span>),</span>
<span id="cb86-9"><a href="ggplot2-plot.html#cb86-9" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="fu">c</span>(<span class="dv">53</span>, <span class="dv">40</span>),</span>
<span id="cb86-10"><a href="ggplot2-plot.html#cb86-10" aria-hidden="true" tabindex="-1"></a> <span class="at">arrow =</span> <span class="fu">arrow</span>(<span class="at">angle =</span> <span class="dv">20</span>, <span class="at">length =</span> <span class="fu">unit</span>(<span class="dv">4</span>, <span class="st">"mm"</span>))) <span class="sc">+</span></span>
<span id="cb86-11"><a href="ggplot2-plot.html#cb86-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="at">geom =</span> <span class="st">"text"</span>,</span>
<span id="cb86-12"><a href="ggplot2-plot.html#cb86-12" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="fl">3.16E4</span>, <span class="at">y =</span> <span class="dv">38</span>,</span>
<span id="cb86-13"><a href="ggplot2-plot.html#cb86-13" aria-hidden="true" tabindex="-1"></a> <span class="at">label =</span> <span class="st">"这些国家的期望寿命低于预期"</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-24-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb87"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb87-1"><a href="ggplot2-plot.html#cb87-1" aria-hidden="true" tabindex="-1"></a><span class="co">#iris例子 + annotate,hline,abline</span></span>
<span id="cb87-2"><a href="ggplot2-plot.html#cb87-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width)) <span class="sc">+</span> <span class="co"># 底层画布</span></span>
<span id="cb87-3"><a href="ggplot2-plot.html#cb87-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb87-4"><a href="ggplot2-plot.html#cb87-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>() </span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-25-1.png" width="672" style="display: block; margin: auto;" /></p>
<p>可以用<code>geom_hline()</code>、<code>geom_vline()</code>和<code>geom_abline()</code>画横线、竖线、斜线。
ggplot2的默认主题会自动画参考线,可以用<code>theme()</code>函数指定参考线画法。</p>
</div>
<div id="指南" class="section level3 hasAnchor" number="4.9.3">
<h3><span class="header-section-number">4.9.3</span> 指南<a href="ggplot2-plot.html#指南" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>对于颜色、填充色等维度,
会自动生成图例。用<code>guides(color = FALSE)</code>这样的方法可以取消指定维度的图例。</p>
<p><code>theme()</code>可以调整一些整体的设置,如背景色、字体、图例的摆放位置。</p>
<p><strong>例如:</strong>用<code>theme()</code>的<code>legend.position</code>改变图例的位置,
如<code>theme(legend.position = "top")</code>可以将图例放置在上方,
默认是放置在右侧的。可取值有<code>"none"、"left"、"right"、"bottom"、"top"</code>,如:</p>
<div class="sourceCode" id="cb88"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb88-1"><a href="ggplot2-plot.html#cb88-1" aria-hidden="true" tabindex="-1"></a><span class="co">#iris 例子</span></span>
<span id="cb88-2"><a href="ggplot2-plot.html#cb88-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> iris, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width,<span class="at">col =</span> Species)) <span class="sc">+</span> <span class="co"># 底层画布</span></span>
<span id="cb88-3"><a href="ggplot2-plot.html#cb88-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb88-4"><a href="ggplot2-plot.html#cb88-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>() <span class="sc">+</span></span>
<span id="cb88-5"><a href="ggplot2-plot.html#cb88-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">'left'</span>,<span class="at">panel.background =</span> <span class="fu">element_blank</span>()) <span class="sc">+</span><span class="fu">theme_bw</span>()</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-26-1.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="主题" class="section level3 hasAnchor" number="4.9.4">
<h3><span class="header-section-number">4.9.4</span> 主题<a href="ggplot2-plot.html#主题" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>ggplot2包作图可以实现内容与设计的分离,这里内容就是指数据、映射、统计、图形类型等方面,而设计就是指背景色、颜色表、字体、坐标轴做法、图例位置等的安排。将作图任务分解为内容与设计两个方面,可以让数据科学家不必关心设计有关的元素,而设计可以让专门的艺术设计人才来处理。这种工作分配已经在图书出版、网站、游戏开发等行业发挥了重要作用。</p>
<p>theme()函数用来指定设计元素,称为主题(theme),而且可以单独开发R扩展包来提供适当的主题。<strong>ggthemes扩展包</strong>是一个这样的包。</p>
<p><code>theme_set()</code>可以改变后续ggplot2作图的主题(配色、字体等)。如<code>theme_set(theme_bw()),theme_set(theme_dark())</code>等。
对单次绘图,可以直接用加号连接<code>theme_gray()</code>等这些主题函数。
主题包括<code>theme_gray()(默认主题)、theme_minimal()、theme_classic()</code>等。</p>
<p><code>theme()</code>函数还可以直接指定颜色、字体、大小等设置。</p>
<div class="sourceCode" id="cb89"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb89-1"><a href="ggplot2-plot.html#cb89-1" aria-hidden="true" tabindex="-1"></a><span class="co">#iris 例子</span></span></code></pre></div>
</div>
</div>
<div id="保存图片" class="section level2 hasAnchor" number="4.10">
<h2><span class="header-section-number">4.10</span> 保存图片<a href="ggplot2-plot.html#保存图片" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>ggplot2包中提供ggsave函数进行图形保存。ggsave函数的使用格式如下所示。</p>
<p><code>ggsave(filename,width,height,...)</code></p>
<p>其中,filename为保存的文件名与路径,width指图像宽度,height指图像高度。</p>
<p><strong>示例:</strong>运行下列代码将会在当前工作目录下生成一个名为mygraph的pdf图形。</p>
<div class="sourceCode" id="cb90"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb90-1"><a href="ggplot2-plot.html#cb90-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(iris, <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">y =</span> Sepal.Width, <span class="at">colour =</span> Species))<span class="sc">+</span></span>
<span id="cb90-2"><a href="ggplot2-plot.html#cb90-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">size =</span> <span class="dv">2</span>)</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-28-1.png" width="672" style="display: block; margin: auto;" /></p>
<div class="sourceCode" id="cb91"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb91-1"><a href="ggplot2-plot.html#cb91-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="at">file =</span> <span class="st">"mygraph1.png"</span>, <span class="at">width =</span> <span class="dv">6</span>, <span class="at">height =</span> <span class="dv">8</span>)</span></code></pre></div>
<p>或者可以使用Rstudio界面进行保存图片,具体教程课件(R语言可视化基础教程)</p>
</div>
<div id="例子" class="section level2 hasAnchor" number="4.11">
<h2><span class="header-section-number">4.11</span> 例子<a href="ggplot2-plot.html#例子" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>该部分来源于:公众号[小明的数据分析笔记本]。大家可以通过以下例子对今天所学的知识进行回顾。其他例子可以在第 <span class="math inline">\(\ref{#some-tips-alls}\)</span> 找到。</p>
<div id="柱状图误差项" class="section level3 hasAnchor" number="4.11.1">
<h3><span class="header-section-number">4.11.1</span> 柱状图+误差项<a href="ggplot2-plot.html#柱状图误差项" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb92"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb92-1"><a href="ggplot2-plot.html#cb92-1" aria-hidden="true" tabindex="-1"></a><span class="do">## 小明推送笔记《小明的数据分析笔记本》</span></span>
<span id="cb92-2"><a href="ggplot2-plot.html#cb92-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 跟着Nature microbiology学画图~R语言ggplot2画柱形图 </span></span>
<span id="cb92-3"><a href="ggplot2-plot.html#cb92-3" aria-hidden="true" tabindex="-1"></a><span class="co"># https://mp.weixin.qq.com/s/E-1X_VSq03AhvC_0cNEgyQ</span></span>
<span id="cb92-4"><a href="ggplot2-plot.html#cb92-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb92-5"><a href="ggplot2-plot.html#cb92-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb92-6"><a href="ggplot2-plot.html#cb92-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb92-7"><a href="ggplot2-plot.html#cb92-7" aria-hidden="true" tabindex="-1"></a><span class="do">### 柱状图+误差项</span></span>
<span id="cb92-8"><a href="ggplot2-plot.html#cb92-8" aria-hidden="true" tabindex="-1"></a>data <span class="ot">=</span> <span class="fu">data.frame</span>(<span class="st">"group"</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="st">"A"</span>,<span class="st">"B"</span>),<span class="st">"value"</span><span class="ot">=</span><span class="fu">c</span>(<span class="fl">0.8</span>,<span class="fl">0.4</span>),<span class="st">"errorbar"</span><span class="ot">=</span><span class="fu">c</span>(.<span class="dv">2</span>,.<span class="dv">1</span>))</span>
<span id="cb92-9"><a href="ggplot2-plot.html#cb92-9" aria-hidden="true" tabindex="-1"></a>data</span></code></pre></div>
<pre><code>## group value errorbar
## 1 A 0.8 0.2
## 2 B 0.4 0.1</code></pre>
<div class="sourceCode" id="cb94"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb94-1"><a href="ggplot2-plot.html#cb94-1" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot">=</span> <span class="fu">ggplot</span>(data,<span class="fu">aes</span>(<span class="at">x =</span> group, <span class="at">y =</span> value)) <span class="sc">+</span></span>
<span id="cb94-2"><a href="ggplot2-plot.html#cb94-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="fu">aes</span>(<span class="at">fill=</span>group),<span class="at">color=</span><span class="st">"black"</span>) <span class="sc">+</span> <span class="co">#柱状图</span></span>
<span id="cb94-3"><a href="ggplot2-plot.html#cb94-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_hline</span>(<span class="at">yintercept =</span> <span class="dv">1</span>,<span class="at">lty =</span> <span class="dv">2</span>) <span class="sc">+</span> <span class="co">#加横线</span></span>
<span id="cb94-4"><a href="ggplot2-plot.html#cb94-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_hline</span>(<span class="at">yintercept =</span> <span class="fl">0.5</span>,<span class="at">lty =</span> <span class="st">"dashed"</span>) <span class="sc">+</span></span>
<span id="cb94-5"><a href="ggplot2-plot.html#cb94-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span> <span class="co">#主题设置</span></span>
<span id="cb94-6"><a href="ggplot2-plot.html#cb94-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">panel.grid =</span> <span class="fu">element_blank</span>(), <span class="co">#网格为空</span></span>
<span id="cb94-7"><a href="ggplot2-plot.html#cb94-7" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"none"</span>) <span class="sc">+</span> <span class="co">#legend位置为无,就是不加</span></span>
<span id="cb94-8"><a href="ggplot2-plot.html#cb94-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>,<span class="dv">0</span>),<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">0</span>,<span class="fl">1.5</span>)) <span class="sc">+</span> <span class="co">#y为连续,设置ylim</span></span>
<span id="cb94-9"><a href="ggplot2-plot.html#cb94-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_discrete</span>(<span class="at">label =</span> <span class="fu">c</span>(<span class="st">"Ositive </span><span class="sc">\n</span><span class="st"> interactions"</span>,<span class="st">"Negative</span><span class="sc">\n</span><span class="st">interactions"</span>)) <span class="sc">+</span> <span class="co">#x为离散</span></span>
<span id="cb94-10"><a href="ggplot2-plot.html#cb94-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="st">"segment"</span>,<span class="at">x=</span><span class="dv">1</span>,<span class="at">y=</span><span class="fl">0.8</span>,<span class="at">xend=</span><span class="dv">1</span>,<span class="at">yend=</span><span class="dv">1</span>) <span class="sc">+</span> <span class="co">#加线段segment,当然这个函数可以加很多其他的包括字</span></span>
<span id="cb94-11"><a href="ggplot2-plot.html#cb94-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="st">"segment"</span>,<span class="at">x=</span><span class="dv">2</span>,<span class="at">y=</span><span class="fl">0.4</span>,<span class="at">xend=</span><span class="dv">2</span>,<span class="at">yend=</span><span class="fl">0.5</span>) <span class="sc">+</span></span>
<span id="cb94-12"><a href="ggplot2-plot.html#cb94-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="cn">NULL</span>, <span class="co">#标签,注意\n可以空行</span></span>
<span id="cb94-13"><a href="ggplot2-plot.html#cb94-13" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">'Absolute fold change</span><span class="sc">\n</span><span class="st">in growth from co-cultures</span><span class="sc">\n</span><span class="st">compared to monocultures'</span>,</span>
<span id="cb94-14"><a href="ggplot2-plot.html#cb94-14" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Average growth fold change in</span><span class="sc">\n</span><span class="st">co-cultures"</span>) <span class="sc">+</span></span>
<span id="cb94-15"><a href="ggplot2-plot.html#cb94-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="st">"segment"</span>,<span class="at">x=</span><span class="dv">1</span>,<span class="at">y=</span><span class="dv">1</span>,<span class="at">xend=</span><span class="fl">1.2</span>,<span class="at">yend=</span><span class="fl">1.05</span>) <span class="sc">+</span></span>
<span id="cb94-16"><a href="ggplot2-plot.html#cb94-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="st">"segment"</span>,<span class="at">x=</span><span class="dv">2</span>,<span class="at">y=</span><span class="dv">1</span>,<span class="at">xend=</span><span class="fl">1.8</span>,<span class="at">yend=</span><span class="fl">1.05</span>) <span class="sc">+</span> </span>
<span id="cb94-17"><a href="ggplot2-plot.html#cb94-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="st">"segment"</span>,<span class="at">x=</span><span class="fl">1.2</span>,<span class="at">y=</span><span class="fl">1.05</span>,<span class="at">xend=</span><span class="fl">1.8</span>,<span class="at">yend=</span><span class="fl">1.05</span>) <span class="sc">+</span> </span>
<span id="cb94-18"><a href="ggplot2-plot.html#cb94-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">annotate</span>(<span class="st">"text"</span>, <span class="at">x =</span> <span class="fl">1.5</span>, <span class="at">y =</span> <span class="fl">1.1</span>, <span class="at">label =</span> <span class="st">"p=0.005"</span> ) <span class="sc">+</span> </span>
<span id="cb94-19"><a href="ggplot2-plot.html#cb94-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#ff8080"</span>,<span class="st">"#90bff9"</span>)) <span class="co">#填充色使用离散颜色manual,两种颜色这里。</span></span>
<span id="cb94-20"><a href="ggplot2-plot.html#cb94-20" aria-hidden="true" tabindex="-1"></a>p1</span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-29-1.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="有正值和负值的柱形图" class="section level3 hasAnchor" number="4.11.2">
<h3><span class="header-section-number">4.11.2</span> 有正值和负值的柱形图<a href="ggplot2-plot.html#有正值和负值的柱形图" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb95"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb95-1"><a href="ggplot2-plot.html#cb95-1" aria-hidden="true" tabindex="-1"></a><span class="do">## 有正值和负值的柱形图</span></span>
<span id="cb95-2"><a href="ggplot2-plot.html#cb95-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb95-3"><a href="ggplot2-plot.html#cb95-3" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">28</span></span>
<span id="cb95-4"><a href="ggplot2-plot.html#cb95-4" aria-hidden="true" tabindex="-1"></a>y <span class="ot"><-</span> <span class="fu">sample</span>(<span class="sc">-</span><span class="dv">100</span><span class="sc">:</span><span class="dv">150</span>,<span class="dv">28</span>,<span class="at">replace =</span> F)</span>
<span id="cb95-5"><a href="ggplot2-plot.html#cb95-5" aria-hidden="true" tabindex="-1"></a>df2 <span class="ot"><-</span> <span class="fu">data.frame</span>(x,y)</span>
<span id="cb95-6"><a href="ggplot2-plot.html#cb95-6" aria-hidden="true" tabindex="-1"></a>df2<span class="sc">$</span>x <span class="ot">=</span> <span class="fu">as.factor</span>(df2<span class="sc">$</span>x)</span>
<span id="cb95-7"><a href="ggplot2-plot.html#cb95-7" aria-hidden="true" tabindex="-1"></a>df2<span class="sc">$</span>group <span class="ot"><-</span> <span class="fu">ifelse</span>(df2<span class="sc">$</span>y<span class="sc">></span><span class="dv">0</span>,<span class="st">"A"</span>,<span class="st">"B"</span>)</span>
<span id="cb95-8"><a href="ggplot2-plot.html#cb95-8" aria-hidden="true" tabindex="-1"></a>df2<span class="sc">$</span>group<span class="ot"><-</span><span class="fu">factor</span>(df2<span class="sc">$</span>group,</span>
<span id="cb95-9"><a href="ggplot2-plot.html#cb95-9" aria-hidden="true" tabindex="-1"></a> <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"Synergistic interactions"</span>,</span>
<span id="cb95-10"><a href="ggplot2-plot.html#cb95-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"Non-synergistic interactions"</span>))</span>
<span id="cb95-11"><a href="ggplot2-plot.html#cb95-11" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(df2)</span></code></pre></div>
<pre><code>## x y group
## 1 1 62 Synergistic interactions
## 2 2 -58 Non-synergistic interactions
## 3 3 -100 Non-synergistic interactions
## 4 4 -72 Non-synergistic interactions
## 5 5 -23 Non-synergistic interactions
## 6 6 49 Synergistic interactions</code></pre>
<div class="sourceCode" id="cb97"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb97-1"><a href="ggplot2-plot.html#cb97-1" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot">=</span> <span class="fu">ggplot</span>(df2,<span class="fu">aes</span>(x,y)) <span class="sc">+</span> </span>
<span id="cb97-2"><a href="ggplot2-plot.html#cb97-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="fu">aes</span>(<span class="at">fill =</span> group),<span class="at">col =</span> <span class="st">"black"</span>) <span class="sc">+</span> </span>
<span id="cb97-3"><a href="ggplot2-plot.html#cb97-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_hline</span>(<span class="at">yintercept =</span> <span class="dv">100</span>,<span class="at">lty =</span> <span class="st">"dashed"</span>) <span class="sc">+</span></span>
<span id="cb97-4"><a href="ggplot2-plot.html#cb97-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_hline</span>(<span class="at">yintercept =</span> <span class="dv">50</span>,<span class="at">lty =</span> <span class="st">"dashed"</span>) <span class="sc">+</span></span>
<span id="cb97-5"><a href="ggplot2-plot.html#cb97-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_hline</span>(<span class="at">yintercept =</span> <span class="sc">-</span><span class="dv">50</span>,<span class="at">lty =</span> <span class="st">"dashed"</span>) <span class="sc">+</span></span>
<span id="cb97-6"><a href="ggplot2-plot.html#cb97-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb97-7"><a href="ggplot2-plot.html#cb97-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb97-8"><a href="ggplot2-plot.html#cb97-8" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">90</span>,<span class="at">hjust =</span> <span class="fl">0.5</span>,</span>
<span id="cb97-9"><a href="ggplot2-plot.html#cb97-9" aria-hidden="true" tabindex="-1"></a> <span class="at">vjust =</span> <span class="fl">0.5</span>),</span>
<span id="cb97-10"><a href="ggplot2-plot.html#cb97-10" aria-hidden="true" tabindex="-1"></a> <span class="at">plot.title =</span> <span class="fu">element_text</span>(<span class="at">hjust =</span> <span class="fl">0.5</span>),</span>
<span id="cb97-11"><a href="ggplot2-plot.html#cb97-11" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb97-12"><a href="ggplot2-plot.html#cb97-12" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.title =</span> <span class="fu">element_blank</span>()) <span class="sc">+</span> <span class="co">#取消标签的名称</span></span>
<span id="cb97-13"><a href="ggplot2-plot.html#cb97-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>,<span class="dv">0</span>),</span>
<span id="cb97-14"><a href="ggplot2-plot.html#cb97-14" aria-hidden="true" tabindex="-1"></a> <span class="at">limits =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">100</span>,<span class="dv">150</span>),</span>
<span id="cb97-15"><a href="ggplot2-plot.html#cb97-15" aria-hidden="true" tabindex="-1"></a> <span class="at">breaks =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">100</span>,<span class="sc">-</span><span class="dv">50</span>,<span class="dv">0</span>,<span class="dv">50</span>,<span class="dv">100</span>,<span class="dv">150</span>)) <span class="sc">+</span></span>
<span id="cb97-16"><a href="ggplot2-plot.html#cb97-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x=</span><span class="st">"Pairwise interactions"</span>,</span>
<span id="cb97-17"><a href="ggplot2-plot.html#cb97-17" aria-hidden="true" tabindex="-1"></a> <span class="at">y=</span><span class="st">"Percentage change from</span><span class="sc">\n</span><span class="st">monoculture"</span>,</span>
<span id="cb97-18"><a href="ggplot2-plot.html#cb97-18" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Synergistic versus non-synergistic</span><span class="sc">\n</span><span class="st">interactions"</span>) <span class="sc">+</span> <span class="co">#标签说明</span></span>
<span id="cb97-19"><a href="ggplot2-plot.html#cb97-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#ff8080"</span>,<span class="st">"#90bff9"</span>))</span>
<span id="cb97-20"><a href="ggplot2-plot.html#cb97-20" aria-hidden="true" tabindex="-1"></a>p2 </span></code></pre></div>
<p><img src="2001-ggplot-ploting_files/figure-html/unnamed-chunk-30-1.png" width="672" style="display: block; margin: auto;" /></p>
</div>
<div id="合并两图" class="section level3 hasAnchor" number="4.11.3">
<h3><span class="header-section-number">4.11.3</span> 合并两图<a href="ggplot2-plot.html#合并两图" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>合并两图或者多图可以使用以下包:</p>
<ul>
<li><code>cowplot</code>包的<code>plot_grid()</code></li>
<li><code>pathwork</code>包</li>
<li><code>gridEctra</code>包的<code>grid.arrange()</code></li>
</ul>
<p>具体可以参考我公众号的这篇推文<a href="https://mp.weixin.qq.com/s/goFqnPtH85oXAwlxcacRDw">R可视乎|合并多幅图形</a></p>
<p>这里使用了cowplot包</p>
<div class="sourceCode" id="cb98"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb98-1"><a href="ggplot2-plot.html#cb98-1" aria-hidden="true" tabindex="-1"></a><span class="do">## 合并两图(使用cowplot包)</span></span>
<span id="cb98-2"><a href="ggplot2-plot.html#cb98-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(cowplot)</span>
<span id="cb98-3"><a href="ggplot2-plot.html#cb98-3" aria-hidden="true" tabindex="-1"></a><span class="fu">pdf</span>(<span class="st">"test/plot_cow.pdf"</span>,<span class="at">width =</span> <span class="dv">8</span>,<span class="at">height =</span> <span class="dv">4</span>)</span>
<span id="cb98-4"><a href="ggplot2-plot.html#cb98-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot_grid</span>(p1,p2,<span class="at">ncol =</span> <span class="dv">2</span>,<span class="at">nrow =</span> <span class="dv">1</span>,<span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"d"</span>,<span class="st">"e"</span>))</span>
<span id="cb98-5"><a href="ggplot2-plot.html#cb98-5" aria-hidden="true" tabindex="-1"></a><span class="fu">dev.off</span>()</span></code></pre></div>
<pre><code>## quartz_off_screen
## 2</code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="causal.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="main-diagram-types.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>