-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCBook.tex
5512 lines (4445 loc) · 290 KB
/
CBook.tex
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
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{book}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
pdftitle={庄闪闪的可视化手册},
pdfauthor={庄闪闪},
hidelinks,
pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
\usepackage{ctex}
%\usepackage{xltxtra} % XeLaTeX的一些额外符号
% 设置中文字体
%\setCJKmainfont[BoldFont={黑体},ItalicFont={楷体}]{新宋体}
% 设置边距
\usepackage{geometry}
\geometry{%
left=2.0cm, right=2.0cm, top=3.5cm, bottom=2.5cm}
\usepackage{amsthm,mathrsfs}
\usepackage{booktabs}
\usepackage{longtable}
\makeatletter
\def\thm@space@setup{%
\thm@preskip=8pt plus 2pt minus 4pt
\thm@postskip=\thm@preskip
}
\makeatother
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\usepackage[style=apa,]{biblatex}
\addbibresource{mybib.bib}
\title{庄闪闪的可视化手册}
\author{庄闪闪}
\date{22/7/2022}
\begin{document}
\maketitle
{
\setcounter{tocdepth}{1}
\tableofcontents
}
\hypertarget{ux7b80ux4ecb}{%
\chapter*{简介}\label{ux7b80ux4ecb}}
\addcontentsline{toc}{chapter}{简介}
这本电子书是《庄闪闪的可视化笔记》。主要内容来自一次市场调查大赛公益课程的教学内容。
这个文稿主要是在一次公益课上整理出来的。经过一年的时候,小编在实践中添加了一些新的内容,希望能为各位科研人提供一些参考。对应的上课视频如下:\href{https://www.bilibili.com/video/BV1MA411p7VR?spm_id_from=333.999.0.0}{R语言可视化教程}。
笔记还会持续更新,读者可以通过我的公众号\href{https://zll-blog.netlify.app/images/wechat.png}{《庄闪闪的 R 语言手册》}了解最新的学习笔记。
我是庄闪闪,这是我的\href{https://zll-blog.netlify.app/}{个人博客},
如果有什么问题,可以通过以下方式联系我:
\href{https://github.com/liangliangzhuang}{GitHub}、\href{https://space.bilibili.com/226576305}{B 站}、\href{https://www.zhihu.com/people/zhuangshanshan}{知乎}、\href{https://blog.csdn.net/qq_37379316?spm=1011.2124.3001.5343}{CSDN}、\href{https://www.xiaohongshu.com/user/profile/597fbfc15e87e755ab15dc26?xhsshare=WeixinSession\&appuid=597fbfc15e87e755ab15dc26\&apptime=1627016124}{小红书}、\href{https://juejin.cn/user/1689330843128008}{掘金}
\hypertarget{part-r-ux8bedux8a00ux57faux7840}{%
\part{R 语言基础}\label{part-r-ux8bedux8a00ux57faux7840}}
\hypertarget{base-tutorial}{%
\chapter{Base 系列}\label{base-tutorial}}
\hypertarget{tidyverse-tutorial}{%
\chapter{Tidyverse 系列}\label{tidyverse-tutorial}}
\hypertarget{part-r-ux8bedux8a00ux53efux89c6ux5316}{%
\part{R 语言可视化}\label{part-r-ux8bedux8a00ux53efux89c6ux5316}}
\hypertarget{causal}{%
\chapter{使用基础包绘图}\label{causal}}
\hypertarget{ux7ed8ux5236ux57faux672cux56feux5f62}{%
\section{绘制基本图形}\label{ux7ed8ux5236ux57faux672cux56feux5f62}}
\hypertarget{ux7ed8ux5236ux5206ux5e03ux5173ux7cfb}{%
\subsection{绘制分布关系}\label{ux7ed8ux5236ux5206ux5e03ux5173ux7cfb}}
数据的数字特征刻画了数据的主要特征,而对数据总体情况做全面描述时,研究人员需要研究数据的分布情况。
\textbf{主要方法}:绘制相应的图形,如直方图,条形图、饼图、箱线图等。
\hypertarget{ux76f4ux65b9ux56fe}{%
\subsubsection{直方图}\label{ux76f4ux65b9ux56fe}}
\textbf{概念介绍}:直方图(Histogram)由一系列高度不等的纵向条纹或者线段表示数据分布的情况。
\textbf{注意}:一般用横轴表示数据所属类别,纵轴表示数量或者占比。
\textbf{适用}:连续数据。
\textbf{例子}:我们使用模拟数据进行讲解,通过正态分布产生30个数据。
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\#数据模拟产生}
\NormalTok{x }\OtherTok{\textless{}{-}} \FunctionTok{rnorm}\NormalTok{(}\DecValTok{30}\NormalTok{, }\AttributeTok{mean=}\DecValTok{10}\NormalTok{, }\AttributeTok{sd=}\DecValTok{1}\NormalTok{)}
\FunctionTok{print}\NormalTok{(}\FunctionTok{round}\NormalTok{(x,}\DecValTok{2}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] 10.70 10.61 9.75 9.63 9.25 9.06 10.87 9.46 10.67 9.07 10.80 11.44
## [13] 9.71 12.32 9.75 11.49 7.78 8.94 9.60 10.38 9.37 9.94 10.26 9.39
## [25] 7.78 10.72 10.82 8.19 9.19 10.03
\end{verbatim}
\texttt{hist()}中的\texttt{breaks()}可以分段区间,取值可以是一个向量(各区间端点)或者一个数字(拆分为多少段),或者一个字符串(计算划分区间的算法名称),或者一个函数(划分区间个数的方法)。这里给出例子
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{hist}\NormalTok{(x,}\AttributeTok{breaks =} \DecValTok{3}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-3-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{hist}\NormalTok{(x, }\AttributeTok{col=}\FunctionTok{rainbow}\NormalTok{(}\DecValTok{15}\NormalTok{),}\AttributeTok{breaks =} \DecValTok{3}\NormalTok{,}
\AttributeTok{main=}\StringTok{\textquotesingle{}正态随机数\textquotesingle{}}\NormalTok{, }\AttributeTok{xlab=}\StringTok{\textquotesingle{}\textquotesingle{}}\NormalTok{, }\AttributeTok{ylab=}\StringTok{\textquotesingle{}频数\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-3-2} \end{center}
\texttt{breaks\ =\ 3}表示x轴分为3个节点。其他设置可参考帮助文档,即\texttt{?hist}。这里加入其他参数\texttt{col,main,xlab,ylab},分别表示颜色,主题名称,x轴名称,y轴名称设置。细节将会在下面一章进行详细解释。
函数\texttt{density()}估计核密度。\texttt{freq=FALSE}绘制频率图。下面的程序作直方图,并使用\texttt{lines()}函数添加核密度曲线:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{tmp.dens }\OtherTok{\textless{}{-}} \FunctionTok{density}\NormalTok{(x)}
\FunctionTok{hist}\NormalTok{(x, }\AttributeTok{freq=}\ConstantTok{FALSE}\NormalTok{,}
\AttributeTok{ylim=}\FunctionTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\FunctionTok{max}\NormalTok{(tmp.dens}\SpecialCharTok{$}\NormalTok{y)}\SpecialCharTok{+}\FloatTok{0.1}\NormalTok{),}
\AttributeTok{col=}\FunctionTok{rainbow}\NormalTok{(}\DecValTok{15}\NormalTok{),}
\AttributeTok{main=}\StringTok{\textquotesingle{}正态随机数\textquotesingle{}}\NormalTok{,}
\AttributeTok{xlab=}\StringTok{\textquotesingle{}\textquotesingle{}}\NormalTok{, }\AttributeTok{ylab=}\StringTok{\textquotesingle{}频率\textquotesingle{}}\NormalTok{)}
\FunctionTok{lines}\NormalTok{(tmp.dens, }\AttributeTok{lwd=}\DecValTok{2}\NormalTok{, }\AttributeTok{col=}\StringTok{\textquotesingle{}blue\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-4-1} \end{center}
\hypertarget{ux6761ux5f62ux56fe}{%
\subsubsection{条形图}\label{ux6761ux5f62ux56fe}}
\textbf{概念介绍}:数量的多少画成长短不同的直条,然后把这些直条按一定的顺序排列起来。
\textbf{注意}:条形图的x轴是数据类别(\textbf{离散型}),y轴是相应类别的频数。
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# 复现课件中的条形图}
\NormalTok{gender }\OtherTok{=} \FunctionTok{table}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\FunctionTok{rep}\NormalTok{(}\StringTok{"F"}\NormalTok{,}\DecValTok{12}\NormalTok{),}\FunctionTok{rep}\NormalTok{(}\StringTok{"M"}\NormalTok{,}\DecValTok{20}\NormalTok{)))}
\FunctionTok{barplot}\NormalTok{(gender,}\AttributeTok{col =} \FunctionTok{c}\NormalTok{(}\StringTok{"red"}\NormalTok{,}\StringTok{"green"}\NormalTok{),}\AttributeTok{main =} \StringTok{"性别分布"}\NormalTok{,}\AttributeTok{horiz =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-5-1} \end{center}
\textbf{数据介绍}:\texttt{VADeaths}数据集记录的是1940年Viginia(弗吉尼亚洲)不同人群(\texttt{Rural\ Male、Rural\ Female\ 、Urban\ Male、Urban\ Female})中每一千人的死亡情况。
\textbf{例子}:数据前6行展示如下:
\begin{tabular}{l|r|r|r|r}
\hline
& Rural Male & Rural Female & Urban Male & Urban Female\\
\hline
50-54 & 11.7 & 8.7 & 15.4 & 8.4\\
\hline
55-59 & 18.1 & 11.7 & 24.3 & 13.6\\
\hline
60-64 & 26.9 & 20.3 & 37.0 & 19.3\\
\hline
65-69 & 41.0 & 30.9 & 54.6 & 35.1\\
\hline
70-74 & 66.0 & 54.3 & 71.1 & 50.0\\
\hline
\end{tabular}
这里绘制该数据的条形图。\texttt{beside}默认值为FALSE,每一列都将给出堆砌的``子条''高度,若 \texttt{beside=TRUE},则每一列都表示一个分组并列
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{barplot}\NormalTok{(VADeaths) }
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-7-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{barplot}\NormalTok{(VADeaths, }\AttributeTok{beside =} \ConstantTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-7-2} \end{center}
\textbf{结论:}随着年龄的增长,\texttt{Viginia}人群的死亡率逐渐增加,并且在4类人群中, \texttt{Urban\ Male}的死亡率比同年龄段的其他群体的死亡率高。同时,在同一环境下,相同年龄段的男性的死亡率要比女性高。
\hypertarget{ux997cux56fe}{%
\subsubsection{饼图}\label{ux997cux56fe}}
\textbf{概念介绍}:将各项的大小与各项总和的比例。反映部分与部分、部分与整体之间的比例关系。
\textbf{例子}:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{percent }\OtherTok{\textless{}{-}} \FunctionTok{colSums}\NormalTok{(VADeaths)}\SpecialCharTok{*}\DecValTok{100}\SpecialCharTok{/}\FunctionTok{sum}\NormalTok{(VADeaths)}
\FunctionTok{pie}\NormalTok{(percent,}\AttributeTok{labels=}\FunctionTok{paste0}\NormalTok{(}\FunctionTok{colnames}\NormalTok{(VADeaths),}\StringTok{\textquotesingle{}}\SpecialCharTok{\textbackslash{}n}\StringTok{\textquotesingle{}}\NormalTok{,}\FunctionTok{round}\NormalTok{(percent,}\DecValTok{2}\NormalTok{),}\StringTok{\textquotesingle{}\%\textquotesingle{}}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-8-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{pie}\NormalTok{(percent,}\AttributeTok{radius=}\FloatTok{0.8}\NormalTok{) }\CommentTok{\#init.angle}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-8-2} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# ?pie}
\end{Highlighting}
\end{Shaded}
\textbf{结论:}Virginia人群中死亡最高的是Urban Male,而且男性的死亡率比女性死亡率要高。
\hypertarget{ux7bb1ux7ebfux56fe}{%
\subsubsection{箱线图}\label{ux7bb1ux7ebfux56fe}}
\textbf{概念介绍}:绘制须使用常用的统计量(最小值、下四分位数、中位数、上四分位数和最大值),并提供有关数据位置和分散情况的关键信息,尤其在比较不同特征时,更可表现其分散程度差异。
\textbf{数据介绍}:iris数据集(鸢尾花数据集),是常用的分类实验数据集合,由Fisher在1936年收集整理。数据集包含了150个子数据集,分为3类(分别为setosa、versicolor、virginica),每类50个数据,每个数据包含4个属性,即花萼长度Sepal.Length 、花萼宽度Sepal.Width、花瓣长度Petal.Length、花瓣宽度Petal.Width。
前6行数据如下:
\begin{tabular}{r|r|r|r|l}
\hline
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
\hline
5.1 & 3.5 & 1.4 & 0.2 & setosa\\
\hline
4.9 & 3.0 & 1.4 & 0.2 & setosa\\
\hline
4.7 & 3.2 & 1.3 & 0.2 & setosa\\
\hline
4.6 & 3.1 & 1.5 & 0.2 & setosa\\
\hline
5.0 & 3.6 & 1.4 & 0.2 & setosa\\
\hline
5.4 & 3.9 & 1.7 & 0.4 & setosa\\
\hline
\end{tabular}
\textbf{例子}:
使用箱线图进行分析,使用两种方法:单独分析四个变量内部的数据分布情况;组间比较(Sepal.Length \textasciitilde{} Species)注意这里的x应该是因子型。这里没有对其他参数进行添加,大家根据自己需求添加即可。
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(iris)}
\FunctionTok{boxplot}\NormalTok{(iris[}\DecValTok{1}\SpecialCharTok{:}\DecValTok{4}\NormalTok{], }\AttributeTok{main =} \StringTok{\textquotesingle{}单独的箱线图\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-10-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{boxplot}\NormalTok{(Sepal.Length }\SpecialCharTok{\textasciitilde{}}\NormalTok{ Species, }\AttributeTok{data =}\NormalTok{ iris, }\AttributeTok{main =} \StringTok{\textquotesingle{}组间比较的箱线图\textquotesingle{}}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-10-2} \end{center}
\textbf{结论:}第一个图:Sepal.Width列含有四个异常值。
第二个图:Sepal.Length列中,类别属于virginica的数据含有一个异常值。同时,从第一个图可以看到,Petal.Length列前半部分相对分散,而后半部分相对密集。
\hypertarget{ux7ed8ux5236ux6570ux636eux95f4ux5173ux7cfb}{%
\subsection{绘制数据间关系}\label{ux7ed8ux5236ux6570ux636eux95f4ux5173ux7cfb}}
\textbf{概念介绍}:在分析数据间关系时,常用散点图和多变量相关矩阵图查看数据间的相关关系。
\hypertarget{ux6563ux70b9ux56fe}{%
\subsubsection{散点图}\label{ux6563ux70b9ux56fe}}
\textbf{特点:}
\begin{enumerate}
\def\labelenumi{(\arabic{enumi})}
\item
特征之间是否存在关联趋势,关联趋势是线性的还是非线性的。
\item
一目了然的看出离群值。从而可以进一步分析这些离群值是否可能在建模分析中产生很大的影响。
\end{enumerate}
\textbf{例子}:使用cars数据进行分析速度(speed)和刹车距离(dist)之间的关系
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(cars[, }\DecValTok{1}\NormalTok{], cars[, }\DecValTok{2}\NormalTok{],}\AttributeTok{xlab =} \StringTok{"speed"}\NormalTok{,}\AttributeTok{ylab =} \StringTok{"dist"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-11-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# plot(cars) \# 效果同上}
\end{Highlighting}
\end{Shaded}
\textbf{结论:}随着汽车行驶速度的增加,刹车距离也在不断增加。
\hypertarget{ux6563ux70b9ux77e9ux9635ux56fe}{%
\subsubsection{散点矩阵图}\label{ux6563ux70b9ux77e9ux9635ux56fe}}
\textbf{概念介绍}:散点矩阵图将多个散点图组合起来,以便可以同时浏览多个二元变量关系,一定程度上克服了在平面上展示高维数据分布情况的困难。可以使用\texttt{plot()}或者\texttt{pairs()}进行绘制。
\textbf{适用}:高维数据
\begin{itemize}
\tightlist
\item
\textbf{plot}
\end{itemize}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(iris[,}\DecValTok{1}\SpecialCharTok{:}\DecValTok{4}\NormalTok{]) }
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-12-1} \end{center}
\textbf{结论:}花瓣长度(Petal.length)与花瓣宽度(Petal.Width)有明显的线性关系,其余属性之间的关系不是很明显。
\begin{itemize}
\tightlist
\item
\textbf{pairs}
\end{itemize}
此外,R中还提供了另一个绘制散点矩阵图的函数------pairs函数,绘图对象有\textbf{数据框}和\textbf{公式}两种:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{pairs}\NormalTok{(iris[,}\DecValTok{1}\SpecialCharTok{:}\DecValTok{4}\NormalTok{])}
\FunctionTok{pairs}\NormalTok{(}\SpecialCharTok{\textasciitilde{}}\NormalTok{Sepal.Length }\SpecialCharTok{+}\NormalTok{ Sepal.Width }\SpecialCharTok{+}\NormalTok{ Petal.Length }\SpecialCharTok{+}\NormalTok{ Petal.Width, }\AttributeTok{data =}\NormalTok{ iris) }\CommentTok{\# 效果同上}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-13-1} \end{center}
\hypertarget{ux591aux53d8ux91cfux76f8ux5173ux77e9ux9635ux56fe}{%
\subsubsection{多变量相关矩阵图}\label{ux591aux53d8ux91cfux76f8ux5173ux77e9ux9635ux56fe}}
\textbf{概念介绍}:多变量相关矩阵图是\textbf{相关系数矩阵}(correlation matrix)的可视化结果,显示了两两变量间的相关关系,对数据维度相对较大的数据有较好的展示效果。在R的\texttt{corrgram}包中的\texttt{corrgram}函数可绘制多变量相关矩阵图。
\textbf{数据介绍}:Mtcar数据集是1974年Motor Trend US杂志公布的32辆车的11个数据,包括燃料消耗和10个关于汽车设计与性能的数据。
\textbf{例子}:下面、根据这个数据集,绘制适用中不同元素描述相关性大小的图。
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(corrgram) }
\FunctionTok{corrgram}\NormalTok{(mtcars)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-14-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{corrgram}\NormalTok{(mtcars, }\AttributeTok{order=}\ConstantTok{TRUE}\NormalTok{, }\AttributeTok{upper.panel=}\NormalTok{panel.ellipse, }
\AttributeTok{main=}\StringTok{"Correlogram of mtcars intercorrelations"}\NormalTok{) }
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-14-2} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# 相关图,主对角线上方绘制置信椭圆和平滑拟合曲线,主对角线下方绘制阴影}
\end{Highlighting}
\end{Shaded}
\textbf{结论}:\texttt{disp}与\texttt{cyl}呈正相关关系,且相关程度较高。此外,\texttt{mpg}与\texttt{wt}呈高度负相关,且\texttt{am}与\texttt{carb}基本没有关系。
这里可以对\texttt{upper.panel}和\texttt{lower.panel}进行设置,展示不同图形。具体可以通过帮助获得相信参数设置信息(\texttt{?corrgram})
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# 相关图,主对角线上方绘制散点图,主对角线下方绘制饼图}
\FunctionTok{corrgram}\NormalTok{(mtcars, }\AttributeTok{order=}\ConstantTok{TRUE}\NormalTok{, }\AttributeTok{upper.panel=}\NormalTok{panel.pts, }\AttributeTok{lower.panel=}\NormalTok{panel.pie, }
\AttributeTok{main=}\StringTok{"Correlogram of mtcars intercorrelations"}\NormalTok{) }
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-15-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# 相关图,主对角线上方绘制置信区间,主对角线下方绘制相关系数}
\FunctionTok{corrgram}\NormalTok{(mtcars, }\AttributeTok{order=}\ConstantTok{TRUE}\NormalTok{, }\AttributeTok{upper.panel=}\NormalTok{panel.conf, }\AttributeTok{lower.panel=}\NormalTok{panel.cor,}
\AttributeTok{main=}\StringTok{"Correlogram of mtcars intercorrelations"}\NormalTok{) }
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-16-1} \end{center}
\hypertarget{ux7ed8ux5236ux5176ux4ed6ux56feux5f62}{%
\subsection{绘制其他图形}\label{ux7ed8ux5236ux5176ux4ed6ux56feux5f62}}
\hypertarget{ux6838ux5bc6ux5ea6ux56fe}{%
\subsubsection{核密度图}\label{ux6838ux5bc6ux5ea6ux56fe}}
\textbf{概念介绍}:sm包中\texttt{sm.density.compare}函数用于绘制核密度图,核密度图如果想用一条密度曲线而不是通过柱状来展示连续型变量的分布。
\textbf{特点}:相比直方图,密度图的一个优势是可以堆放,可用于比较组间差异。\texttt{sm.density.compare}函数可以直接堆放多条密度曲线。使用格式如下。
\texttt{sm.density.compare(x\ ,group,….)}
其中x是数值向量,group是分组向量,是因子型数据。
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(sm) }\CommentTok{\# 加载sm包}
\FunctionTok{sm.density.compare}\NormalTok{(mtcars}\SpecialCharTok{$}\NormalTok{wt, }\FunctionTok{factor}\NormalTok{(mtcars}\SpecialCharTok{$}\NormalTok{cyl)) }\CommentTok{\# 绘制核密度图}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-17-1} \end{center}
\hypertarget{ux5c0fux63d0ux7434ux56fe}{%
\subsubsection{小提琴图}\label{ux5c0fux63d0ux7434ux56fe}}
\textbf{概念介绍}:vioplot包中的vioplot函数用于绘制小提琴图,小提琴图是核密度图与箱线图的结合,本质是利用密度值生成的多边形,但该多边形同时还沿着一条直线作了另一半对称的``镜像'',这样两个左右或上下对称的多边形拼起来就形成了小提琴图的主体部分,最后一个箱线图也会被添加在小提琴的中轴线上。
使用格式如下。
\texttt{vioplot(\ x\ ,\ ...,\ \ range=1.5,\ \ h,\ \ ylim,\ names,\ \ horizontal=FALSE\ ,\ …)}
其中,x为数据源,可以是向量;range默认等于1.5;col是为每幅小提琴图指定颜色的向量。
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(vioplot) }\CommentTok{\# 加载vioplot包}
\FunctionTok{attach}\NormalTok{(mtcars)}
\FunctionTok{vioplot}\NormalTok{(wt[cyl}\SpecialCharTok{==}\DecValTok{4}\NormalTok{], wt[cyl}\SpecialCharTok{==}\DecValTok{6}\NormalTok{], wt[cyl}\SpecialCharTok{==}\DecValTok{8}\NormalTok{], }\AttributeTok{border=}\StringTok{"black"}\NormalTok{, }
\AttributeTok{col =} \StringTok{"gray60"}\NormalTok{, }\AttributeTok{rectCol =} \StringTok{"blue"}\NormalTok{, }\AttributeTok{horizontal =} \ConstantTok{TRUE}\NormalTok{,}
\AttributeTok{main =} \StringTok{\textquotesingle{}小提琴图\textquotesingle{}}\NormalTok{) }\CommentTok{\# 绘制小提琴图}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-18-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{boxplot}\NormalTok{(wt}\SpecialCharTok{\textasciitilde{}}\NormalTok{cyl, }\AttributeTok{main =} \StringTok{\textquotesingle{}箱线图\textquotesingle{}}\NormalTok{, }\AttributeTok{horizontal=}\ConstantTok{TRUE}\NormalTok{,}
\AttributeTok{pars=}\FunctionTok{list}\NormalTok{(}\AttributeTok{boxwex=}\FloatTok{0.1}\NormalTok{), }\AttributeTok{border=}\StringTok{"blue"}\NormalTok{) }\CommentTok{\# 绘制箱线图}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-18-2} \end{center}
\hypertarget{qqux56fe}{%
\subsubsection{QQ图}\label{qqux56fe}}
\textbf{概念介绍}:查看经验分布和理论分布是否一致。将排序后的数据和理论分布的分位数进行比较后大致相等,说明了经验分布和理论分布相似。\texttt{qqplot()}函数用于绘制QQ图,QQ图检查数据是否服从某种分布。
使用格式如下:
\texttt{qqplot(x,\ y,,...);qqnorm(y,…)\ ;\ qqline(y)}
其中,x与y均为数据源,可以是向量。
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{qqnorm}\NormalTok{(wt) }\CommentTok{\#正态分布QQ图}
\FunctionTok{qqline}\NormalTok{(wt) }\CommentTok{\#QQ线}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-19-1} \end{center}
\hypertarget{ux7b49ux9ad8ux56fe}{%
\subsubsection{等高图}\label{ux7b49ux9ad8ux56fe}}
\textbf{概念介绍}:数据形式:两个数值向量x、y和一个相应的矩阵z。x、y交叉组合之后形成的是一个``网格'',z是这个网格上的高度数值,将平面上对应的z值(高度)相等的点连接起来形成的线就是等高线。对x、y进行核密度估计,得到一个密度值矩阵,然后用x、y以及这个密度值矩阵作等高图。由于密度值反映的是某个位置上数据的密集程度,等高图就展示了一个聚类现象。
使用格式如下
\texttt{contour(x=,y=,z,nlevels=,levels=,labels=\ ,method=,...)}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(KernSmooth) }\CommentTok{\# 计算二维核密度的包}
\NormalTok{mtcars1 }\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(wt, mpg)}
\NormalTok{est }\OtherTok{=} \FunctionTok{bkde2D}\NormalTok{(mtcars1, }\FunctionTok{apply}\NormalTok{(mtcars1, }\DecValTok{2}\NormalTok{, dpik)) }\CommentTok{\# 计算二维核密度}
\FunctionTok{contour}\NormalTok{(est}\SpecialCharTok{$}\NormalTok{x1, est}\SpecialCharTok{$}\NormalTok{x2, est}\SpecialCharTok{$}\NormalTok{fhat, }\AttributeTok{nlevels =} \DecValTok{15}\NormalTok{, }
\AttributeTok{col =} \StringTok{"darkgreen"}\NormalTok{, }\AttributeTok{xlab =} \StringTok{"wt"}\NormalTok{, }\AttributeTok{ylab =} \StringTok{"mpg"}\NormalTok{) }\CommentTok{\# 画等高图}
\FunctionTok{points}\NormalTok{(mtcars1) }\CommentTok{\# 添加散点}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-20-1} \end{center}
\hypertarget{ux4feeux6539ux56feux5f62ux53c2ux6570}{%
\section{修改图形参数}\label{ux4feeux6539ux56feux5f62ux53c2ux6570}}
R是一个功能强大的图形构建平台,可以通过逐条输入语句构建图形元素(\textbf{颜色、点、线、文本以及图例}等),逐渐完善图形特征,直至得到想要的效果。图形元素的显示可以用图形函数和绘图参数来改良,也可以用绘制图形元素的基础函数来控制。
\hypertarget{ux4feeux6539ux989cux8272}{%
\subsection{修改颜色}\label{ux4feeux6539ux989cux8272}}
R语言通过设置绘图参数col来改变\textbf{图像、坐标轴、文字、点、线}等的颜色。关于颜色的函数大致可以分为下面三类:
\hypertarget{ux56faux5b9aux989cux8272ux9009ux62e9ux51fdux6570}{%
\subsubsection{固定颜色选择函数}\label{ux56faux5b9aux989cux8272ux9009ux62e9ux51fdux6570}}
R语言提供了自带的固定种类的颜色,主要涉及的是colors函数,该函数可以生成657中颜色名称,代表657种颜色,可以通过以下代码查看R自带颜色的前20中颜色的名称。
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{colors}\NormalTok{()[}\DecValTok{1}\SpecialCharTok{:}\DecValTok{20}\NormalTok{] }
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] "white" "aliceblue" "antiquewhite" "antiquewhite1"
## [5] "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine"
## [9] "aquamarine1" "aquamarine2" "aquamarine3" "aquamarine4"
## [13] "azure" "azure1" "azure2" "azure3"
## [17] "azure4" "beige" "bisque" "bisque1"
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# colors()}
\FunctionTok{plot}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{,}\AttributeTok{col =} \FunctionTok{cm.colors}\NormalTok{(}\DecValTok{1}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-21-1} \end{center}
通过palette函数固定调色板,只要设定好了调色板,它的取值就不会再改变(直到下一次重新设定调色板)。
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{palette}\NormalTok{() }\CommentTok{\#返回当前的调色板设置,此时为默认值}
\DocumentationTok{\#\# [1] "black" "\#DF536B" "\#61D04F" "\#2297E6" "\#28E2E5" "\#CD0BBC" "\#F5C710"}
\DocumentationTok{\#\# [8] "gray62"}
\FunctionTok{palette}\NormalTok{(}\FunctionTok{colors}\NormalTok{()[}\DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{]) }\CommentTok{\#重新设置调色板为colors的前10种颜色}
\FunctionTok{palette}\NormalTok{() }\CommentTok{\#返回当前的调色板设置,此时为colors()的前10种颜色}
\DocumentationTok{\#\# [1] "white" "aliceblue" "antiquewhite" "antiquewhite1"}
\DocumentationTok{\#\# [5] "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine" }
\DocumentationTok{\#\# [9] "aquamarine" "aquamarine2"}
\FunctionTok{palette}\NormalTok{(}\StringTok{\textquotesingle{}default\textquotesingle{}}\NormalTok{) }\CommentTok{\#恢复默认的调色板设置}
\end{Highlighting}
\end{Shaded}
\textbf{例子}:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(iris}\SpecialCharTok{$}\NormalTok{Sepal.Length, iris}\SpecialCharTok{$}\NormalTok{Sepal.Width, }\AttributeTok{col =}\NormalTok{ iris}\SpecialCharTok{$}\NormalTok{Species) }
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-23-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Species为因子型数据,setosa versicolor virginica分别对应数字1,2,3,}
\CommentTok{\# 即等价于col = rep(1:3, each = 50)}
\end{Highlighting}
\end{Shaded}
\hypertarget{ux6e10ux53d8ux8272ux751fux6210ux51fdux6570}{%
\subsubsection{渐变色生成函数}\label{ux6e10ux53d8ux8272ux751fux6210ux51fdux6570}}
除了固定颜色选择函数外,R还提供了一系列渐变颜色生成函数,这些函数用来控制颜色值\textbf{逐步变化}。
\includegraphics{figure/14.png}
rgb函数把RGB颜色转化为十六进制数值,使用格式前四个参数都取值于区间{[}0, max{]},names参数用来指定生成颜色向量的名称。red,green,blue参数的值越大就说明该颜色的成分越高。alpha指的是颜色的透明度,取0表示完全透明,取最大值表示完全不透明(默认完全不透明)。
\texttt{rainbow}函数、\texttt{heat.coclor}函数、\texttt{terrain.colors}函数、\texttt{topo.colors}函数、\texttt{cm.colors}函数是主题配色函数,使用格式中n设定产生颜色的数目,start和end设定彩虹颜色的一个子集,生成的颜色将从这个子集中选取。
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-24-1} \end{center}
\hypertarget{rcolorbrewerux5305}{%
\subsubsection{RColorBrewer包}\label{rcolorbrewerux5305}}
RColorBrewer包提供了3套配色方案,分别为\textbf{连续型,极端型以及离散型}。
\begin{itemize}
\item
\textbf{连续型}(Sequential)指生成一系列连续渐变的颜色,通常用来标记连续型数值的大小。共18组颜色,每组分为9个渐变颜色展示。
\item
\textbf{极端型}(Diverging)指生成用深色强调两端、浅色标示中部的系列颜色、可用来标记数据中的离群点。共9组颜色,每组分为11个渐变颜色展示。
\item
\textbf{离散型}(Qualitative)指生成一系列彼此差异比较明显的颜色,通常用来标记分类数据。共8组颜色,每组渐变颜色数不同。
\end{itemize}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow =} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{3}\NormalTok{))}
\FunctionTok{library}\NormalTok{(RColorBrewer)}
\FunctionTok{par}\NormalTok{(}\AttributeTok{mar=}\FunctionTok{c}\NormalTok{(}\FloatTok{0.1}\NormalTok{,}\DecValTok{3}\NormalTok{,}\FloatTok{0.1}\NormalTok{,}\FloatTok{0.1}\NormalTok{))}
\FunctionTok{display.brewer.all}\NormalTok{(}\AttributeTok{type=}\StringTok{"seq"}\NormalTok{)}
\FunctionTok{display.brewer.all}\NormalTok{(}\AttributeTok{type=}\StringTok{"div"}\NormalTok{)}
\FunctionTok{display.brewer.all}\NormalTok{(}\AttributeTok{type=}\StringTok{"qual"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-25-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(RColorBrewer)}
\NormalTok{my\_col }\OtherTok{\textless{}{-}} \FunctionTok{brewer.pal}\NormalTok{(}\DecValTok{3}\NormalTok{, }\StringTok{\textquotesingle{}RdYlGn\textquotesingle{}}\NormalTok{) }
\CommentTok{\# brewer.pal(n,name),其中n为颜色的数量,name表示颜色组的名称}
\FunctionTok{plot}\NormalTok{(iris}\SpecialCharTok{$}\NormalTok{Sepal.Length, iris}\SpecialCharTok{$}\NormalTok{Sepal.Width, }\AttributeTok{col =} \FunctionTok{rep}\NormalTok{(my\_col, }\AttributeTok{each =}\DecValTok{50}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-26-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(iris}\SpecialCharTok{$}\NormalTok{Sepal.Length, iris}\SpecialCharTok{$}\NormalTok{Sepal.Width, }\AttributeTok{col =} \FunctionTok{rep}\NormalTok{(}\FunctionTok{rainbow}\NormalTok{(}\DecValTok{3}\NormalTok{), }\AttributeTok{each =} \DecValTok{50}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-26-2} \end{center}
\hypertarget{ux4feeux6539ux70b9ux7b26ux53f7ux4e0eux7ebfux6761}{%
\subsection{修改点符号与线条}\label{ux4feeux6539ux70b9ux7b26ux53f7ux4e0eux7ebfux6761}}
\hypertarget{ux70b9ux6837ux5f0f}{%
\subsubsection{点样式}\label{ux70b9ux6837ux5f0f}}
\includegraphics{figure/5.jpg}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-27-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(iris}\SpecialCharTok{$}\NormalTok{Sepal.Length, iris}\SpecialCharTok{$}\NormalTok{Sepal.Width, }\AttributeTok{pch =} \FunctionTok{rep}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{3}\NormalTok{, }\AttributeTok{each =} \DecValTok{50}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-28-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# plot(1:10,pch=21,cex=1.5,col=\textquotesingle{}red\textquotesingle{},bg = "blue",lwd=5)}
\end{Highlighting}
\end{Shaded}
\hypertarget{ux7ebfux6761ux6837ux5f0f}{%
\subsubsection{线条样式}\label{ux7ebfux6761ux6837ux5f0f}}
R语言提供了绘制不同类别的线条的多种函数,主要有
\begin{itemize}
\tightlist
\item
lines:绘制曲线
\item
abline:绘制直线
\item
segments:绘制线段
\item
arrows:在线段加上箭头
\item
grid:绘制网格线
\end{itemize}
\includegraphics{figure/6.jpg}
以mtcars数据集为例来展示实际绘图过程中线条的应用。
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(mtcars)}
\NormalTok{smpg}\OtherTok{=}\NormalTok{(mpg}\SpecialCharTok{{-}}\FunctionTok{min}\NormalTok{(mpg))}\SpecialCharTok{/}\NormalTok{(}\FunctionTok{max}\NormalTok{(mpg)}\SpecialCharTok{{-}}\FunctionTok{min}\NormalTok{(mpg))}
\FunctionTok{plot}\NormalTok{(wt,smpg,}\AttributeTok{ylab=}\StringTok{"standardized mpg"}\NormalTok{)}
\CommentTok{\#添加核密度曲线图}
\FunctionTok{lines}\NormalTok{(}\FunctionTok{density}\NormalTok{(wt),}\AttributeTok{col=}\StringTok{"red"}\NormalTok{)}
\CommentTok{\#指向密度曲线的箭头}
\FunctionTok{arrows}\NormalTok{(}\FloatTok{1.8}\NormalTok{,}\FloatTok{0.05}\NormalTok{,}\FloatTok{1.5}\NormalTok{,}\FloatTok{0.1}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{2}\NormalTok{,}\FloatTok{0.05}\NormalTok{,}\StringTok{"density curve"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{0.6}\NormalTok{)}
\CommentTok{\#添加回归线}
\FunctionTok{abline}\NormalTok{(}\FunctionTok{lm}\NormalTok{(smpg}\SpecialCharTok{\textasciitilde{}}\NormalTok{wt),}\AttributeTok{lty=}\DecValTok{2}\NormalTok{,}\AttributeTok{col=}\StringTok{"green"}\NormalTok{)}
\CommentTok{\#指向回归直线的箭头}
\FunctionTok{arrows}\NormalTok{(}\DecValTok{2}\NormalTok{,}\FloatTok{0.5}\NormalTok{,}\DecValTok{2}\NormalTok{,}\FloatTok{0.7}\NormalTok{,}\AttributeTok{angle=}\DecValTok{10}\NormalTok{,}\AttributeTok{cex=}\FloatTok{0.5}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{2}\NormalTok{,}\FloatTok{0.45}\NormalTok{,}\StringTok{"regression line"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{0.6}\NormalTok{)}
\CommentTok{\#wt与mpg反向线性相关,添加最大最小值线段表现这种关系}
\FunctionTok{segments}\NormalTok{(}\FunctionTok{min}\NormalTok{(wt),}\FunctionTok{max}\NormalTok{(smpg),}\FunctionTok{max}\NormalTok{(wt),}\FunctionTok{min}\NormalTok{(smpg),}\AttributeTok{lty=}\DecValTok{3}\NormalTok{,}\AttributeTok{col=}\StringTok{"blue"}\NormalTok{)}
\CommentTok{\#指向最大最小值线段的箭头}
\FunctionTok{arrows}\NormalTok{(}\DecValTok{3}\NormalTok{,}\FloatTok{0.8}\NormalTok{,}\FloatTok{2.5}\NormalTok{,}\FloatTok{0.76}\NormalTok{,}\AttributeTok{angle=}\DecValTok{10}\NormalTok{,}\AttributeTok{cex=}\FloatTok{0.5}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\FloatTok{3.3}\NormalTok{,}\FloatTok{0.8}\NormalTok{,}\StringTok{"line segments"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{0.6}\NormalTok{)}
\CommentTok{\#添加网格线作为背景}
\FunctionTok{grid}\NormalTok{(}\AttributeTok{nx=}\DecValTok{4}\NormalTok{,}\AttributeTok{ny=}\DecValTok{5}\NormalTok{,}\AttributeTok{lty=}\DecValTok{2}\NormalTok{,}\AttributeTok{col=}\StringTok{"grey"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-29-1} \end{center}
\hypertarget{ux4feeux6539ux6587ux672cux53c2ux6570}{%
\subsubsection{修改文本参数}\label{ux4feeux6539ux6587ux672cux53c2ux6570}}
title、text和mtext函数可以在打开的画布上添加文字元素。
\begin{itemize}
\tightlist
\item
title可以添加标题元素;
\item
text可以任意位置添加文本;
\item
mtext函数则是在四条边上添加文本。
\end{itemize}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow =} \FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DecValTok{2}\NormalTok{))}
\CommentTok{\# 图一:图形添加标题}
\FunctionTok{plot}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{0}\SpecialCharTok{:}\DecValTok{5}\NormalTok{),}\AttributeTok{col=}\StringTok{"red"}\NormalTok{,}\AttributeTok{xlab=}\StringTok{""}\NormalTok{,}\AttributeTok{ylab=}\StringTok{""}\NormalTok{)}
\FunctionTok{title}\NormalTok{(}\AttributeTok{main=}\FunctionTok{list}\NormalTok{(}\StringTok{"主标题"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{1.5}\NormalTok{),}\AttributeTok{sub=}\FunctionTok{list}\NormalTok{(}\StringTok{"副标题"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{1.2}\NormalTok{), }
\AttributeTok{xlab=}\StringTok{"x轴标题"}\NormalTok{,}\AttributeTok{ylab=}\StringTok{"y轴标题"}\NormalTok{)}
\CommentTok{\# 图二:图形周边添加文本}
\FunctionTok{plot}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{0}\SpecialCharTok{:}\DecValTok{5}\NormalTok{),}\AttributeTok{col=}\StringTok{"white"}\NormalTok{)}
\FunctionTok{mtext}\NormalTok{(}\StringTok{\textquotesingle{}side=1:下边\textquotesingle{}}\NormalTok{,}\AttributeTok{side=}\DecValTok{1}\NormalTok{,}\AttributeTok{line=}\DecValTok{2}\NormalTok{)}
\FunctionTok{mtext}\NormalTok{(}\StringTok{\textquotesingle{}side=2:左边\textquotesingle{}}\NormalTok{ ,}\AttributeTok{side=}\DecValTok{2}\NormalTok{,}\AttributeTok{line=}\DecValTok{2}\NormalTok{)}
\FunctionTok{mtext}\NormalTok{(}\StringTok{\textquotesingle{}side=3:上边\textquotesingle{}}\NormalTok{ ,}\AttributeTok{side=}\DecValTok{3}\NormalTok{)}
\FunctionTok{mtext}\NormalTok{(}\StringTok{\textquotesingle{}side=4:右边\textquotesingle{}}\NormalTok{ ,}\AttributeTok{side=}\DecValTok{4}\NormalTok{)}
\CommentTok{\# 图三:字体展示}
\FunctionTok{plot}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{0}\SpecialCharTok{:}\DecValTok{5}\NormalTok{),}\AttributeTok{col=}\StringTok{"white"}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{4}\NormalTok{,}\AttributeTok{labels=}\StringTok{"font=1:正常字体(默认)"}\NormalTok{,}\AttributeTok{font=}\DecValTok{1}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{3}\NormalTok{,}\DecValTok{3}\NormalTok{,}\AttributeTok{labels=}\StringTok{"font=2:粗体字体"}\NormalTok{,}\AttributeTok{font=}\DecValTok{2}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{4}\NormalTok{,}\DecValTok{2}\NormalTok{,}\AttributeTok{labels=}\StringTok{"font=3:斜体字体"}\NormalTok{,}\AttributeTok{font=}\DecValTok{3}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{5}\NormalTok{,}\DecValTok{1}\NormalTok{,}\AttributeTok{labels=}\StringTok{"font=4:粗斜体字体"}\NormalTok{,}\AttributeTok{font=}\DecValTok{4}\NormalTok{)}
\CommentTok{\# 图四:字体大小展示}
\FunctionTok{plot}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{0}\SpecialCharTok{:}\DecValTok{6}\NormalTok{),}\AttributeTok{col=}\StringTok{"white"}\NormalTok{,}\AttributeTok{xlim=}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{8}\NormalTok{))}
\FunctionTok{text}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{5}\NormalTok{,}\AttributeTok{labels=}\StringTok{"cex=0.5:放大0.5倍"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{0.5}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{3}\NormalTok{,}\DecValTok{4}\NormalTok{,}\AttributeTok{labels=}\StringTok{"cex=0.8:放大0.8倍"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{0.8}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{4}\NormalTok{,}\DecValTok{3}\NormalTok{,}\AttributeTok{labels=}\StringTok{"cex=1(默认):正常大小"}\NormalTok{,}\AttributeTok{cex=}\DecValTok{1}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{5}\NormalTok{,}\DecValTok{2}\NormalTok{,}\AttributeTok{labels=}\StringTok{"cex=1.2:放大1.2倍"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{1.2}\NormalTok{)}
\FunctionTok{text}\NormalTok{(}\DecValTok{6}\NormalTok{,}\DecValTok{1}\NormalTok{,}\AttributeTok{labels=}\StringTok{"cex=1.5:放大1.5倍"}\NormalTok{,}\AttributeTok{cex=}\FloatTok{1.5}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-30-1} \end{center}
\textbf{例子}:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{attach}\NormalTok{(mtcars)}
\FunctionTok{plot}\NormalTok{(wt, mpg, }\AttributeTok{xlab =} \StringTok{"Weight (1000 lbs)"}\NormalTok{, }
\AttributeTok{ylab =} \StringTok{"Miles/(US) gallon"}\NormalTok{) }\CommentTok{\#绘图,并修改x,y轴的标题}
\FunctionTok{title}\NormalTok{(}\AttributeTok{main=}\FunctionTok{list}\NormalTok{(}\StringTok{"mtcars wt V.S. mpg"}\NormalTok{, }\AttributeTok{cex=}\FloatTok{1.5}\NormalTok{)) }\CommentTok{\# 添加标题}
\FunctionTok{text}\NormalTok{(}\FloatTok{4.5}\NormalTok{, }\DecValTok{34}\NormalTok{, }\AttributeTok{labels =} \StringTok{\textquotesingle{}extracted from the 1974\textquotesingle{}}\NormalTok{, }\AttributeTok{cex =} \FloatTok{1.5}\NormalTok{) }\CommentTok{\# 说明数据来源}
\FunctionTok{text}\NormalTok{(}\FloatTok{4.5}\NormalTok{, }\DecValTok{32}\NormalTok{, }\AttributeTok{labels =} \StringTok{\textquotesingle{}Motor Trend US\textquotesingle{}}\NormalTok{, }\AttributeTok{font =} \DecValTok{3}\NormalTok{) }\CommentTok{\# 杂志名称}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-31-1} \end{center}
\hypertarget{ux8bbeux7f6eux5750ux6807ux8f74}{%
\subsubsection{设置坐标轴}\label{ux8bbeux7f6eux5750ux6807ux8f74}}
使用\texttt{axis()}进行设置坐标轴。
\includegraphics{figure/7.jpg}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{12}\NormalTok{), }\AttributeTok{col=}\StringTok{"white"}\NormalTok{, }\AttributeTok{xaxt=}\StringTok{"n"}\NormalTok{, }\AttributeTok{yaxt=}\StringTok{"n"}\NormalTok{, }\AttributeTok{ann =} \ConstantTok{FALSE}\NormalTok{)}
\FunctionTok{axis}\NormalTok{(}\DecValTok{1}\NormalTok{, }\AttributeTok{at=}\DecValTok{1}\SpecialCharTok{:}\DecValTok{12}\NormalTok{, }\AttributeTok{col.axis=}\StringTok{"red"}\NormalTok{, }\AttributeTok{labels=}\NormalTok{month.abb)}
\FunctionTok{axis}\NormalTok{(}\DecValTok{2}\NormalTok{, }\AttributeTok{at=}\FunctionTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{12}\NormalTok{,}\AttributeTok{length=}\DecValTok{10}\NormalTok{), }\AttributeTok{col.axis=}\StringTok{"red"}\NormalTok{, }\AttributeTok{labels=}\DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{, }\AttributeTok{las=}\DecValTok{2}\NormalTok{)}
\FunctionTok{axis}\NormalTok{(}\DecValTok{3}\NormalTok{, }\AttributeTok{at=}\FunctionTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{12}\NormalTok{,}\AttributeTok{length=}\DecValTok{7}\NormalTok{), }\AttributeTok{col.axis=}\StringTok{"blue"}\NormalTok{, }\AttributeTok{cex.axis=}\FloatTok{0.7}\NormalTok{, }
\AttributeTok{tck=}\SpecialCharTok{{-}}\FloatTok{0.01}\NormalTok{, }\AttributeTok{labels =} \FunctionTok{c}\NormalTok{(}\StringTok{"Mon"}\NormalTok{, }\StringTok{"Tues"}\NormalTok{, }\StringTok{"Wed"}\NormalTok{, }\StringTok{"Thu"}\NormalTok{, }\StringTok{"Fri"}\NormalTok{, }\StringTok{"Sat"}\NormalTok{, }\StringTok{"Sun"}\NormalTok{)) }
\FunctionTok{axis}\NormalTok{(}\DecValTok{4}\NormalTok{, }\AttributeTok{at=}\FunctionTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{12}\NormalTok{,}\AttributeTok{length=}\DecValTok{11}\NormalTok{), }\AttributeTok{col.axis=}\StringTok{"blue"}\NormalTok{, }\AttributeTok{cex.axis=}\FloatTok{0.7}\NormalTok{, }
\AttributeTok{tck=}\SpecialCharTok{{-}}\FloatTok{0.01}\NormalTok{, }\AttributeTok{labels=}\FunctionTok{seq}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\FloatTok{0.1}\NormalTok{), }\AttributeTok{las=}\DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-32-1} \end{center}
\hypertarget{ux6dfbux52a0ux56feux4f8b}{%
\subsubsection{添加图例}\label{ux6dfbux52a0ux56feux4f8b}}
legend函数的绘制图例的位置效果
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{local}\OtherTok{=}\FunctionTok{c}\NormalTok{(}\StringTok{"bottomright"}\NormalTok{, }\StringTok{"bottom"}\NormalTok{, }\StringTok{"bottomleft"}\NormalTok{, }\StringTok{"left"}\NormalTok{, }\StringTok{"topleft"}\NormalTok{,}
\StringTok{"top"}\NormalTok{, }\StringTok{"topright"}\NormalTok{, }\StringTok{"right"}\NormalTok{, }\StringTok{"center"}\NormalTok{)}
\FunctionTok{par}\NormalTok{(}\AttributeTok{mar =} \FunctionTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{4}\NormalTok{,}\DecValTok{2}\NormalTok{), }\AttributeTok{pty=}\StringTok{\textquotesingle{}m\textquotesingle{}}\NormalTok{)}
\FunctionTok{plot}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{0}\SpecialCharTok{:}\DecValTok{10}\NormalTok{), }\AttributeTok{col =} \StringTok{"white"}\NormalTok{)}
\FunctionTok{legend}\NormalTok{(}\DecValTok{3}\NormalTok{, }\DecValTok{8}\NormalTok{, }\StringTok{"图例在(3,8)"}\NormalTok{)}
\ControlFlowTok{for}\NormalTok{(i }\ControlFlowTok{in} \DecValTok{1}\SpecialCharTok{:}\DecValTok{9}\NormalTok{)\{}
\FunctionTok{legend}\NormalTok{(local[i], }\FunctionTok{paste}\NormalTok{(}\StringTok{"图例在"}\NormalTok{, local[i]))}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-33-1} \end{center}
\textbf{综合测试}:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(iris}\SpecialCharTok{$}\NormalTok{Sepal.Length, iris}\SpecialCharTok{$}\NormalTok{Sepal.Width, }\AttributeTok{col =}\NormalTok{ iris}\SpecialCharTok{$}\NormalTok{Species, }
\AttributeTok{main =} \FunctionTok{list}\NormalTok{(}\StringTok{\textquotesingle{}鸢尾花的花萼长与宽的散点图\textquotesingle{}}\NormalTok{, }\AttributeTok{cex =} \FloatTok{1.5}\NormalTok{), }
\AttributeTok{xlab=}\StringTok{"花萼长度"}\NormalTok{, }\AttributeTok{ylab=}\StringTok{"花萼宽度"}\NormalTok{,}\AttributeTok{pch=}\DecValTok{19}\NormalTok{)}
\FunctionTok{grid}\NormalTok{(}\AttributeTok{nx=}\DecValTok{5}\NormalTok{, }\AttributeTok{ny=}\DecValTok{5}\NormalTok{, }\AttributeTok{lty=}\DecValTok{2}\NormalTok{, }\AttributeTok{col=}\StringTok{"grey"}\NormalTok{) }\CommentTok{\# 添加网格线}
\FunctionTok{legend}\NormalTok{(}\DecValTok{7}\NormalTok{,}\FloatTok{4.5}\NormalTok{, }\FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}setosa\textquotesingle{}}\NormalTok{, }\StringTok{\textquotesingle{}versicolor\textquotesingle{}}\NormalTok{, }\StringTok{\textquotesingle{}virginica\textquotesingle{}}\NormalTok{),}
\AttributeTok{pch=}\DecValTok{19}\NormalTok{, }\AttributeTok{col =} \DecValTok{1}\SpecialCharTok{:}\DecValTok{3}\NormalTok{) }\CommentTok{\# 添加图例}
\FunctionTok{lines}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\FloatTok{4.3}\NormalTok{, }\FloatTok{6.5}\NormalTok{), }\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }\FloatTok{4.5}\NormalTok{), }\AttributeTok{col =}\StringTok{\textquotesingle{}blue\textquotesingle{}}\NormalTok{) }\CommentTok{\# 添加直线}
\FunctionTok{arrows}\NormalTok{(}\DecValTok{6}\NormalTok{, }\DecValTok{4}\NormalTok{ , }\FloatTok{6.5}\NormalTok{ ,}\DecValTok{4}\NormalTok{, }\AttributeTok{angle=}\DecValTok{10}\NormalTok{, }\AttributeTok{cex=}\FloatTok{0.5}\NormalTok{) }\CommentTok{\# 添加箭头}
\FunctionTok{text}\NormalTok{(}\FloatTok{6.9}\NormalTok{, }\DecValTok{4}\NormalTok{, }\StringTok{"左上角全是setosa"}\NormalTok{, }\AttributeTok{cex=}\FloatTok{0.8}\NormalTok{) }\CommentTok{\# 添加文字说明}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-34-1} \end{center}
\hypertarget{ux7ed8ux5236ux7ec4ux5408ux56feux5f62}{%
\section{绘制组合图形}\label{ux7ed8ux5236ux7ec4ux5408ux56feux5f62}}
\hypertarget{par}{%
\subsection{par()}\label{par}}
一页多图用mfrow参数或mfcol参数规定。
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mfrow1}\OtherTok{=}\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{)) }\CommentTok{\#mar=c(2,2,2,2)}
\ControlFlowTok{for}\NormalTok{(i }\ControlFlowTok{in} \DecValTok{1}\SpecialCharTok{:}\DecValTok{6}\NormalTok{)\{}
\FunctionTok{plot}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\NormalTok{i),}\AttributeTok{main=}\FunctionTok{paste}\NormalTok{(}\StringTok{"I\textquotesingle{}m image:"}\NormalTok{,i))}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-35-1} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(mfrow1)}
\NormalTok{op }\OtherTok{=} \FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{,}\DecValTok{2}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{,}\AttributeTok{pch=}\DecValTok{12}\NormalTok{)}
\FunctionTok{hist}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{)}
\FunctionTok{boxplot}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{)}
\FunctionTok{pie}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\DecValTok{10}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-35-2} \end{center}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(op)}
\end{Highlighting}
\end{Shaded}
\hypertarget{layout}{%
\subsection{layout}\label{layout}}
与par函数均分画布不同,layout函数可以不均等的分隔页面
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mat}\OtherTok{\textless{}{-}}\FunctionTok{matrix}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{1}\NormalTok{,}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{,}\DecValTok{3}\NormalTok{,}\DecValTok{4}\NormalTok{,}\DecValTok{4}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{6}\NormalTok{), }\AttributeTok{nrow =} \DecValTok{2}\NormalTok{, }\AttributeTok{byrow =} \ConstantTok{TRUE}\NormalTok{)}
\FunctionTok{layout}\NormalTok{(mat)}
\ControlFlowTok{for}\NormalTok{(i }\ControlFlowTok{in} \DecValTok{1}\SpecialCharTok{:}\DecValTok{6}\NormalTok{)\{}
\FunctionTok{plot}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\SpecialCharTok{:}\NormalTok{i),}\AttributeTok{main=}\FunctionTok{paste}\NormalTok{(}\StringTok{"I\textquotesingle{}m image:"}\NormalTok{,i))}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{1001-base-ploting_files/figure-latex/unnamed-chunk-36-1} \end{center}
\hypertarget{ux4fddux5b58ux56feux5f62}{%