-
Notifications
You must be signed in to change notification settings - Fork 2
/
UsefulCode.tex
1446 lines (1199 loc) · 81.8 KB
/
UsefulCode.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
\documentclass[10,portrait]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\PassOptionsToPackage{hyphens}{url} % url is loaded by hyperref
\usepackage[unicode=true]{hyperref}
\PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref
\hypersetup{
pdftitle={Useful R code},
colorlinks=true,
linkcolor=blue,
citecolor=red,
urlcolor=blue,
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\usepackage[margin=1in]{geometry}
\usepackage[]{biblatex}
\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{\KeywordTok}[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{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\title{Useful R code}
\author{Matthew
Malishev\textsuperscript{1}*\\[2\baselineskip]\emph{\textsuperscript{1}
Department of Biology, Emory University, 1510 Clifton Road NE, Atlanta,
GA, USA, 30322}}
\date{}
\begin{document}
\maketitle
{
\hypersetup{linkcolor=black}
\setcounter{tocdepth}{4}
\tableofcontents
}
~
Date: 2019-10-25\\
\texttt{R} version: 3.5.0\\
*Corresponding author:
\href{mailto:[email protected]}{\nolinkurl{[email protected]}}\\
This document can be found at
\url{https://github.com/darwinanddavis/UsefulCode}
\newpage
\subsection{Overview}\label{overview}
This document outlines some useful \texttt{R} code for plotting, cool
functions, and other random tidbits.
\subsubsection{Install dependencies}\label{install-dependencies}
\subsubsection{Attributes}\label{attributes}
Access structural attributes of unique classes, such as raster and ggmap
(bbox).
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Normal example}
\NormalTok{df <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{X =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{), }\DataTypeTok{Y =} \KeywordTok{c}\NormalTok{(}\DecValTok{6}\OperatorTok{:}\DecValTok{10}\NormalTok{))}
\KeywordTok{str}\NormalTok{(df)}
\NormalTok{df}\OperatorTok{$}\NormalTok{X}
\CommentTok{# `attr` method}
\KeywordTok{require}\NormalTok{(ggmap)}
\NormalTok{map <-}\StringTok{ }\KeywordTok{get_map}\NormalTok{(}\StringTok{"Atlanta"}\NormalTok{, }\DataTypeTok{zoom =} \DecValTok{12}\NormalTok{, }\DataTypeTok{source =} \StringTok{"stamen"}\NormalTok{, }
\DataTypeTok{maptype =} \StringTok{"toner-lines"}\NormalTok{)}
\KeywordTok{str}\NormalTok{(map)}
\KeywordTok{attr}\NormalTok{(map, }\StringTok{"bb"}\NormalTok{)}\OperatorTok{$}\NormalTok{ll.lat}
\end{Highlighting}
\end{Shaded}
\subsubsection{Classes}\label{classes}
Convert character to factor to numeric without conversion error
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{read.table}\NormalTok{(f, }\DataTypeTok{header =}\NormalTok{ T, }\DataTypeTok{sep =} \StringTok{","}\NormalTok{, }\DataTypeTok{row.names =} \OtherTok{NULL}\NormalTok{, }
\DataTypeTok{stringsAsFactors =} \OtherTok{FALSE}\NormalTok{, }\DataTypeTok{strip.white =} \OtherTok{TRUE}\NormalTok{)}
\NormalTok{f}\OperatorTok{$}\NormalTok{V2 <-}\StringTok{ }\KeywordTok{as.numeric}\NormalTok{(f}\OperatorTok{$}\NormalTok{V2)}
\end{Highlighting}
\end{Shaded}
See call options for class
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{methods}\NormalTok{(}\DataTypeTok{class =} \StringTok{"estUDm"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Set dynamic input for variable / assign variable to char vector
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{shadedens <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(shadedens) \{}
\CommentTok{# set shade density to clumped (to match food) or}
\CommentTok{# sparse}
\ControlFlowTok{if}\NormalTok{ (shadedens }\OperatorTok{==}\StringTok{ "Random"}\NormalTok{) \{}
\KeywordTok{NLCommand}\NormalTok{(}\StringTok{"set Shade-density }\CharTok{\textbackslash{}"}\StringTok{Random}\CharTok{\textbackslash{}"}\StringTok{ "}\NormalTok{)}
\NormalTok{ \} }\ControlFlowTok{else}\NormalTok{ \{}
\KeywordTok{NLCommand}\NormalTok{(}\StringTok{"set Shade-density }\CharTok{\textbackslash{}"}\StringTok{Clumped}\CharTok{\textbackslash{}"}\StringTok{ "}\NormalTok{)}
\NormalTok{ \}}
\NormalTok{\}}
\KeywordTok{shadedens}\NormalTok{(}\StringTok{"Clumped"}\NormalTok{) }\CommentTok{# set clumped resources}
\end{Highlighting}
\end{Shaded}
\subsubsection{D3 apps}\label{d3-apps}
Interactive network plots using d3
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Load package}
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"networkD3"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(networkD3)}
\CommentTok{# Load energy projection data}
\NormalTok{URL <-}\StringTok{ "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"}
\NormalTok{Energy <-}\StringTok{ }\NormalTok{jsonlite}\OperatorTok{::}\KeywordTok{fromJSON}\NormalTok{(URL)}
\CommentTok{# Now we have 2 data frames: a 'links' data frame}
\CommentTok{# with 3 columns (from, to, value), and a 'nodes'}
\CommentTok{# data frame that gives the name of each node.}
\KeywordTok{head}\NormalTok{(Energy}\OperatorTok{$}\NormalTok{links)}
\KeywordTok{head}\NormalTok{(Energy}\OperatorTok{$}\NormalTok{nodes)}
\CommentTok{# Thus we can plot it}
\KeywordTok{sankeyNetwork}\NormalTok{(}\DataTypeTok{Links =}\NormalTok{ Energy}\OperatorTok{$}\NormalTok{links, }\DataTypeTok{Nodes =}\NormalTok{ Energy}\OperatorTok{$}\NormalTok{nodes, }
\DataTypeTok{Source =} \StringTok{"source"}\NormalTok{, }\DataTypeTok{Target =} \StringTok{"target"}\NormalTok{, }\DataTypeTok{Value =} \StringTok{"value"}\NormalTok{, }
\DataTypeTok{NodeID =} \StringTok{"name"}\NormalTok{, }\DataTypeTok{units =} \StringTok{"TWh"}\NormalTok{, }\DataTypeTok{fontSize =} \DecValTok{12}\NormalTok{, }
\DataTypeTok{nodeWidth =} \DecValTok{30}\NormalTok{)}
\StringTok{`}\DataTypeTok{?}\StringTok{`}\NormalTok{(sankeyNetwork)}
\end{Highlighting}
\end{Shaded}
\subsubsection{Dataframes}\label{dataframes}
Optimal empty data frame
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{Date =} \KeywordTok{as.Date}\NormalTok{(}\KeywordTok{character}\NormalTok{()), }\DataTypeTok{X =} \KeywordTok{numeric}\NormalTok{(), }
\DataTypeTok{Y =} \KeywordTok{integer}\NormalTok{(), }\DataTypeTok{stringsAsFactors =} \OtherTok{FALSE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Add df cols with \texttt{mutate}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(dplyr)}
\NormalTok{df <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{a =} \KeywordTok{rnorm}\NormalTok{(}\DecValTok{10}\NormalTok{), }\DataTypeTok{b =}\NormalTok{ (}\DecValTok{1}\OperatorTok{:}\DecValTok{20}\NormalTok{))}
\NormalTok{df }\OperatorTok{%>%}\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{c =} \KeywordTok{rnorm}\NormalTok{(}\DecValTok{20}\NormalTok{), }\DataTypeTok{b =}\NormalTok{ b }\OperatorTok{*}\StringTok{ }\DecValTok{67}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Change \texttt{df} column names
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{colnames}\NormalTok{(data)[}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{)] <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"TimeStamp"}\NormalTok{, }\StringTok{"Lat"}\NormalTok{, }
\StringTok{"Long"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Remove multiple columns from df
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{### Remove multiple NA columns}
\NormalTok{rm_cols <-}\StringTok{ }\KeywordTok{grep}\NormalTok{(}\StringTok{"NA"}\NormalTok{, }\KeywordTok{names}\NormalTok{(tt), }\DataTypeTok{ignore.case =}\NormalTok{ F)}
\NormalTok{df[, }\KeywordTok{colnames}\NormalTok{(df[, rm_cols])] <-}\StringTok{ }\KeywordTok{list}\NormalTok{(}\OtherTok{NULL}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Check number of characters in each column
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{sapply}\NormalTok{(meso1, }\ControlFlowTok{function}\NormalTok{(x) }\KeywordTok{sum}\NormalTok{(}\KeywordTok{nchar}\NormalTok{(x)))}
\end{Highlighting}
\end{Shaded}
\subsubsection{Generic functions}\label{generic-functions}
Generic useful functions that I can't place under any other headings
here
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# dput() for converting outputs such as copied text}
\CommentTok{# or data tables into vectors}
\NormalTok{xx <-}\StringTok{ "Some copied text or table from the internet"}
\KeywordTok{dput}\NormalTok{(xx)}
\end{Highlighting}
\end{Shaded}
Round up integers to optimal rounded value
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{nn <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\DecValTok{46}\NormalTok{, }\DecValTok{11}\NormalTok{, }\DecValTok{23}\NormalTok{)}
\KeywordTok{round_any}\NormalTok{(nn, }\DecValTok{10}\NormalTok{)}
\KeywordTok{round_any}\NormalTok{(nn, }\DecValTok{10}\NormalTok{, ceiling)}
\KeywordTok{round_any}\NormalTok{(nn, }\DecValTok{10}\NormalTok{, floor)}
\end{Highlighting}
\end{Shaded}
Get summary stats for dataset (means)
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{means =}\StringTok{ }\KeywordTok{aggregate}\NormalTok{(Cumulative_cercs }\OperatorTok{~}\StringTok{ }\NormalTok{r }\OperatorTok{*}\StringTok{ }\NormalTok{hb, }\DataTypeTok{data =}\NormalTok{ df, }
\DataTypeTok{FUN =}\NormalTok{ mean)}
\end{Highlighting}
\end{Shaded}
\subsubsection{Lists}\label{lists}
Find maximum value in entire list
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{master <-}\StringTok{ }\KeywordTok{list}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DecValTok{100}\NormalTok{, }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{12}\NormalTok{))}
\KeywordTok{do.call}\NormalTok{(max, master)}
\end{Highlighting}
\end{Shaded}
Plot all elements in a list
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{xx <-}\StringTok{ }\KeywordTok{list}\NormalTok{(}\KeywordTok{sample}\NormalTok{(}\DecValTok{5}\NormalTok{, }\DecValTok{1000}\NormalTok{, }\DataTypeTok{replace =}\NormalTok{ T), }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{1000}\NormalTok{), }
\KeywordTok{sample}\NormalTok{(}\DecValTok{50}\NormalTok{, }\DecValTok{1000}\NormalTok{, }\DataTypeTok{replace =}\NormalTok{ T))}
\KeywordTok{plot}\NormalTok{(}\KeywordTok{unlist}\NormalTok{(xx), }\DataTypeTok{type =} \StringTok{"l"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Apply each row of df or vector to individual elements of a list
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df =}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{events =}\NormalTok{ LETTERS[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{], }\DataTypeTok{outs =} \DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{)}
\KeywordTok{sapply}\NormalTok{(df}\OperatorTok{$}\NormalTok{outs, list)}
\end{Highlighting}
\end{Shaded}
Append extra element onto existing list
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{rv <-}\StringTok{ }\KeywordTok{sample}\NormalTok{(}\DecValTok{1000}\NormalTok{, }\DecValTok{15}\NormalTok{) }\CommentTok{# random vector}
\NormalTok{listvec <-}\StringTok{ }\KeywordTok{sapply}\NormalTok{(}\KeywordTok{rep}\NormalTok{(}\OtherTok{NA}\NormalTok{, }\DecValTok{7}\NormalTok{), list) }\CommentTok{# list with 7 empty elements}
\NormalTok{listvec_final <-}\StringTok{ }\KeywordTok{c}\NormalTok{(listvec, }\KeywordTok{list}\NormalTok{(rv)) }\CommentTok{# append rv}
\NormalTok{listvec_final <-}\StringTok{ }\KeywordTok{c}\NormalTok{(listvec, rv) }\CommentTok{# to append rv contents as separate elements, remove internal list }
\end{Highlighting}
\end{Shaded}
\subsubsection{Loops}\label{loops}
Save loop output in master list
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pars <-}\StringTok{ }\KeywordTok{seq}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{, }\FloatTok{0.5}\NormalTok{)}
\NormalTok{master <-}\StringTok{ }\KeywordTok{list}\NormalTok{()}
\NormalTok{t_list <-}\StringTok{ }\KeywordTok{list}\NormalTok{()}
\ControlFlowTok{for}\NormalTok{ (p }\ControlFlowTok{in} \DecValTok{1}\OperatorTok{:}\KeywordTok{length}\NormalTok{(pars)) \{}
\ControlFlowTok{for}\NormalTok{ (t }\ControlFlowTok{in} \DecValTok{5}\NormalTok{) \{}
\NormalTok{ tt <-}\StringTok{ }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{1000} \OperatorTok{*}\StringTok{ }\NormalTok{t)}
\NormalTok{ t_list[t] <-}\StringTok{ }\NormalTok{tt}
\NormalTok{ \}}
\NormalTok{ master[[}\KeywordTok{length}\NormalTok{(master) }\OperatorTok{+}\StringTok{ }\DecValTok{1}\NormalTok{]] <-}\StringTok{ }\NormalTok{t_list }\CommentTok{# store in master list}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
Optimal way to save results to data frame in loop
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(dplyr)}
\NormalTok{fun <-}\StringTok{ }\NormalTok{sum }\CommentTok{# sum # choose mean or sum }
\NormalTok{out_first <-}\StringTok{ }\KeywordTok{list}\NormalTok{() }\CommentTok{# create first empty list}
\NormalTok{out_second <-}\StringTok{ }\KeywordTok{list}\NormalTok{() }\CommentTok{# create second empty list}
\ControlFlowTok{for}\NormalTok{ (me }\ControlFlowTok{in} \DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{) \{}
\NormalTok{ global_output_fh =}\StringTok{ }\KeywordTok{paste0}\NormalTok{(}\KeywordTok{getwd}\NormalTok{(), }\StringTok{"/"}\NormalTok{, me, }\StringTok{".R"}\NormalTok{) }\CommentTok{# get file handle}
\NormalTok{ output <-}\StringTok{ }\KeywordTok{readRDS}\NormalTok{(global_output_fh) }\CommentTok{# read in file }
\NormalTok{ cercs <-}\StringTok{ }\NormalTok{output[[}\DecValTok{1}\NormalTok{]] }\CommentTok{# get data }
\CommentTok{# define function}
\NormalTok{ SEM =}\StringTok{ }\ControlFlowTok{function}\NormalTok{(x) \{}
\KeywordTok{sd}\NormalTok{(x)}\OperatorTok{/}\KeywordTok{sqrt}\NormalTok{(}\KeywordTok{length}\NormalTok{(x))}
\NormalTok{ \}}
\CommentTok{# create col name to pass to aggregate function}
\NormalTok{ cerc_outs =}\StringTok{ }\KeywordTok{list}\NormalTok{(}\DataTypeTok{Outs =}\NormalTok{ cercs)}
\NormalTok{ outs =}\StringTok{ }\KeywordTok{aggregate}\NormalTok{(Outs }\OperatorTok{~}\StringTok{ }\NormalTok{., }\DataTypeTok{data =}\NormalTok{ cerc_outs, }\DataTypeTok{FUN =}\NormalTok{ fun)}
\NormalTok{ cerc_se =}\StringTok{ }\KeywordTok{list}\NormalTok{(}\DataTypeTok{SEs =}\NormalTok{ cercs)}
\NormalTok{ se =}\StringTok{ }\KeywordTok{aggregate}\NormalTok{(SEs }\OperatorTok{~}\StringTok{ }\NormalTok{., }\DataTypeTok{data =}\NormalTok{ cerc_se, }\DataTypeTok{FUN =}\NormalTok{ SEM)}
\CommentTok{# save to df by creating new df cols}
\NormalTok{ outs}\OperatorTok{$}\NormalTok{me <-}\StringTok{ }\NormalTok{me }\CommentTok{# create new col with iteration}
\NormalTok{ out_first[[me]] <-}\StringTok{ }\NormalTok{outs }\CommentTok{# add first output to list}
\NormalTok{ out_second[[me]] <-}\StringTok{ }\NormalTok{se }\CommentTok{# add second output to list}
\NormalTok{\} }\CommentTok{# end file read }
\CommentTok{# option 1}
\NormalTok{out_final =}\StringTok{ }\KeywordTok{do.call}\NormalTok{(rbind, out_first)}
\CommentTok{# option 2}
\NormalTok{out_final <-}\StringTok{ }\KeywordTok{bind_rows}\NormalTok{(out_first) }\CommentTok{# make fresh df }
\NormalTok{out_final}\OperatorTok{$}\NormalTok{Second <-}\StringTok{ }\KeywordTok{bind_rows}\NormalTok{(out_second) }\CommentTok{# add second col}
\end{Highlighting}
\end{Shaded}
\subsubsection{Maps}\label{maps}
High res maps
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# https://hecate.hakai.org/rguide/mapping-in-r.html}
\KeywordTok{require}\NormalTok{(maptools)}
\NormalTok{d <-}\StringTok{ }\KeywordTok{map_data}\NormalTok{(}\StringTok{"worldHires"}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{"Colombia"}\NormalTok{, }\StringTok{"Ecuador"}\NormalTok{, }
\StringTok{"Peru"}\NormalTok{, }\StringTok{"Panama"}\NormalTok{))}
\CommentTok{# plot}
\KeywordTok{ggplot}\NormalTok{() }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_polygon}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ d, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ long, }\DataTypeTok{y =}\NormalTok{ lat, }
\DataTypeTok{group =}\NormalTok{ group), }\DataTypeTok{fill =} \StringTok{"black"}\NormalTok{, }\DataTypeTok{col =} \StringTok{"pink"}\NormalTok{) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\CommentTok{# theme_tufte(ticks=F) +}
\KeywordTok{theme_nothing}\NormalTok{() }\OperatorTok{+}\StringTok{ }\KeywordTok{coord_map}\NormalTok{(}\StringTok{"mercator"}\NormalTok{, }\DataTypeTok{xlim =} \KeywordTok{c}\NormalTok{(}\OperatorTok{-}\DecValTok{75}\NormalTok{, }
\OperatorTok{-}\DecValTok{81}\NormalTok{), }\DataTypeTok{ylim =} \KeywordTok{c}\NormalTok{(}\OperatorTok{-}\DecValTok{2}\NormalTok{, }\DecValTok{8}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
Read in KMZ/KML data (Google Maps data)
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(sf)}
\NormalTok{zp <-}\StringTok{ }\NormalTok{sf}\OperatorTok{::}\KeywordTok{st_read}\NormalTok{(}\StringTok{"ziggy_test.kml"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\subsubsection{Messages}\label{messages}
Display status message of progress
\begin{Shaded}
\begin{Highlighting}[]
\ControlFlowTok{for}\NormalTok{ (i }\ControlFlowTok{in} \DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{) \{}
\KeywordTok{Sys.sleep}\NormalTok{(}\FloatTok{0.2}\NormalTok{)}
\CommentTok{# Dirk says using cat() like this is naughty ;-)}
\CommentTok{# cat(i,'\textbackslash{}r') So you can use message() like this,}
\CommentTok{# thanks to Sharpie's comment to use}
\CommentTok{# appendLF=FALSE.}
\KeywordTok{message}\NormalTok{(i, }\StringTok{"}\CharTok{\textbackslash{}r}\StringTok{"}\NormalTok{, }\DataTypeTok{appendLF =} \OtherTok{FALSE}\NormalTok{) }\CommentTok{# appendLF = new line }
\KeywordTok{flush.console}\NormalTok{()}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
Display popup progress bar
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(tcltk)}
\NormalTok{pb <-}\StringTok{ }\KeywordTok{tkProgressBar}\NormalTok{(}\StringTok{"test progress bar"}\NormalTok{, }\StringTok{"Some information in %"}\NormalTok{, }
\DecValTok{0}\NormalTok{, }\DecValTok{100}\NormalTok{, }\DecValTok{50}\NormalTok{)}
\KeywordTok{Sys.sleep}\NormalTok{(}\FloatTok{0.5}\NormalTok{)}
\NormalTok{u <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\KeywordTok{sort}\NormalTok{(}\KeywordTok{runif}\NormalTok{(}\DecValTok{20}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{100}\NormalTok{)), }\DecValTok{100}\NormalTok{)}
\ControlFlowTok{for}\NormalTok{ (i }\ControlFlowTok{in}\NormalTok{ u) \{}
\KeywordTok{Sys.sleep}\NormalTok{(}\FloatTok{0.1}\NormalTok{)}
\NormalTok{ info <-}\StringTok{ }\KeywordTok{sprintf}\NormalTok{(}\StringTok{"%d%% done"}\NormalTok{, }\KeywordTok{round}\NormalTok{(i))}
\KeywordTok{setTkProgressBar}\NormalTok{(pb, i, }\KeywordTok{sprintf}\NormalTok{(}\StringTok{"test (%s)"}\NormalTok{, info), }
\NormalTok{ info)}
\NormalTok{\}}
\KeywordTok{Sys.sleep}\NormalTok{(}\DecValTok{5}\NormalTok{)}
\KeywordTok{close}\NormalTok{(pb)}
\end{Highlighting}
\end{Shaded}
\subsubsection{\texorpdfstring{\texttt{NAs} and
\texttt{NaNs}}{NAs and NaNs}}\label{nas-and-nans}
Replace NAs and NaNs with 0's
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df[}\KeywordTok{is.na}\NormalTok{(df)] <-}\StringTok{ }\DecValTok{0}
\NormalTok{df[}\KeywordTok{is.nan}\NormalTok{(df)] <-}\StringTok{ }\DecValTok{0} \CommentTok{# good for matrices }
\end{Highlighting}
\end{Shaded}
Replace X values less than given value (V) with 0
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df}\OperatorTok{$}\NormalTok{X[df}\OperatorTok{$}\NormalTok{X }\OperatorTok{<}\StringTok{ }\NormalTok{V] <-}\StringTok{ }\DecValTok{0}
\end{Highlighting}
\end{Shaded}
Check for \texttt{NAs}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{sapply}\NormalTok{(df, }\ControlFlowTok{function}\NormalTok{(x) }\KeywordTok{sum}\NormalTok{(}\KeywordTok{is.na}\NormalTok{(x)))}
\end{Highlighting}
\end{Shaded}
snail.update{[}is.nan(snail.update){]} \textless{}- 0 Replace
\texttt{NaN} and \texttt{Inf} values with \texttt{NA}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df}\OperatorTok{$}\NormalTok{col1[}\KeywordTok{which}\NormalTok{(}\OperatorTok{!}\KeywordTok{is.finite}\NormalTok{(df}\OperatorTok{$}\NormalTok{col1))] <-}\StringTok{ }\OtherTok{NA}
\end{Highlighting}
\end{Shaded}
Fill in missing data values in sequence with \texttt{NA}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# /Users/malishev/Documents/Manuscripts/Chapter4/Sims/Chapter4_figs.R}
\KeywordTok{library}\NormalTok{(zoo)}
\NormalTok{data <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{index =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{4}\NormalTok{, }\DecValTok{6}\OperatorTok{:}\DecValTok{10}\NormalTok{), }\DataTypeTok{data =} \KeywordTok{c}\NormalTok{(}\FloatTok{1.5}\NormalTok{, }
\FloatTok{4.3}\NormalTok{, }\FloatTok{5.6}\NormalTok{, }\FloatTok{6.7}\NormalTok{, }\FloatTok{7.1}\NormalTok{, }\FloatTok{12.5}\NormalTok{, }\FloatTok{14.5}\NormalTok{, }\FloatTok{16.8}\NormalTok{, }\FloatTok{3.4}\NormalTok{))}
\CommentTok{# you can create a series}
\NormalTok{z <-}\StringTok{ }\KeywordTok{zoo}\NormalTok{(data}\OperatorTok{$}\NormalTok{data, data}\OperatorTok{$}\NormalTok{index)}
\CommentTok{# end extend it to the grid 1:10}
\NormalTok{z <-}\StringTok{ }\KeywordTok{merge}\NormalTok{(}\KeywordTok{zoo}\NormalTok{(, }\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{), z)}
\CommentTok{# worked example fill in missing Tb values}
\NormalTok{minTb.d <-}\StringTok{ }\KeywordTok{zoo}\NormalTok{(minTb}\OperatorTok{$}\NormalTok{Tick, minTb}\OperatorTok{$}\NormalTok{Days)}
\NormalTok{minTb.d <-}\StringTok{ }\KeywordTok{merge}\NormalTok{(}\KeywordTok{zoo}\NormalTok{(}\OtherTok{NULL}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\NormalTok{days), minTb.d) }\CommentTok{# make the minTb series match the temp series (117 days)}
\NormalTok{minTb.d <-}\StringTok{ }\KeywordTok{as.numeric}\NormalTok{(minTb.d) }\CommentTok{# = time individuals reached VTMIN in ticks}
\NormalTok{minTb <-}\StringTok{ }\NormalTok{minTb.d }\OperatorTok{-}\StringTok{ }\NormalTok{temp}\OperatorTok{$}\NormalTok{Tick }\CommentTok{# get diff between starting time and time to reach VTMIN}
\NormalTok{minTb <-}\StringTok{ }\NormalTok{minTb}\OperatorTok{/}\DecValTok{2} \CommentTok{# convert ticks to minutes}
\NormalTok{minTb <-}\StringTok{ }\NormalTok{minTb}\OperatorTok{/}\DecValTok{60} \CommentTok{#convert to hours}
\NormalTok{minTb <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{Days =} \DecValTok{1}\OperatorTok{:}\NormalTok{days, }\DataTypeTok{Time =}\NormalTok{ minTb)}
\CommentTok{# then fill in missing values}
\KeywordTok{approx}\NormalTok{(minTb}\OperatorTok{$}\NormalTok{Time, }\DataTypeTok{method =} \StringTok{"linear"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Remove rows with NA
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{data <-}\StringTok{ }\NormalTok{data[}\OperatorTok{!}\KeywordTok{is.na}\NormalTok{(data}\OperatorTok{$}\NormalTok{X), ]}
\end{Highlighting}
\end{Shaded}
Turn NULLs in list into NAs to get numeric values (fix for `cannot
coerce double' error)
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{hl_list <-}\StringTok{ }\KeywordTok{lapply}\NormalTok{(hl_list, }\ControlFlowTok{function}\NormalTok{(x) }\KeywordTok{ifelse}\NormalTok{(x }\OperatorTok{==}\StringTok{ }
\StringTok{ "NULL"}\NormalTok{, }\OtherTok{NA}\NormalTok{, x))}
\end{Highlighting}
\end{Shaded}
Turn NaN or NAs in list into 0s
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# NaN}
\NormalTok{global_output <-}\StringTok{ }\KeywordTok{rapply}\NormalTok{(global_output, }\DataTypeTok{f =} \ControlFlowTok{function}\NormalTok{(x) }\KeywordTok{ifelse}\NormalTok{(}\KeywordTok{is.nan}\NormalTok{(x), }
\DecValTok{0}\NormalTok{, x), }\DataTypeTok{how =} \StringTok{"replace"}\NormalTok{)}
\CommentTok{# NA}
\NormalTok{global_output <-}\StringTok{ }\KeywordTok{rapply}\NormalTok{(global_output, }\DataTypeTok{f =} \ControlFlowTok{function}\NormalTok{(x) }\KeywordTok{ifelse}\NormalTok{(}\KeywordTok{is.na}\NormalTok{(x), }
\DecValTok{0}\NormalTok{, x), }\DataTypeTok{how =} \StringTok{"replace"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\subsubsection{Packages}\label{packages}
\href{https://blog.socialcops.com/technology/data-science/announcing-rlandsat-landsat-8-data/}{\texttt{rLandsat}}\\
Sourcing, requesting, and downloading NASA Landsat 8 satellite data.
\href{https://blog.rstudio.com/2018/09/19/radix-for-r-markdown/}{Radix}\\
Improved \texttt{RMarkdown} output and interaction.
\href{http://www.stats.gla.ac.uk/~adrian/rpanel/}{\texttt{rpanel}}\\
\href{https://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf}{Reference
guide}\\
Create interactive GUI control toggles from \texttt{R}. Like an early
Shiny.
\subsubsection{Plotting}\label{plotting}
Plot one plot window above and two below
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{layout}\NormalTok{(}\KeywordTok{matrix}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{), }\DecValTok{2}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DataTypeTok{byrow =} \OtherTok{TRUE}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
Bookend axis ticks for plot E.g. at 0 and 100 when data is 1:99
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{axis}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{at =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\KeywordTok{length}\NormalTok{(loco}\OperatorTok{$}\NormalTok{X)), }\DataTypeTok{labels =} \KeywordTok{c}\NormalTok{(}\StringTok{""}\NormalTok{, }\StringTok{""}\NormalTok{)) }\CommentTok{# bookending axis tick marks}
\end{Highlighting}
\end{Shaded}
Optimal legend formatting for base
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{legend}\NormalTok{(}\StringTok{"right"}\NormalTok{, }\DataTypeTok{legend =} \KeywordTok{c}\NormalTok{(}\StringTok{"Small"}\NormalTok{, }\StringTok{"Intermediate"}\NormalTok{, }
\StringTok{"Large"}\NormalTok{), }\DataTypeTok{col =} \KeywordTok{c}\NormalTok{(colfunc[colvec[}\DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{]]), }\DataTypeTok{bty =} \StringTok{"n"}\NormalTok{, }
\DataTypeTok{pch =} \DecValTok{20}\NormalTok{, }\DataTypeTok{pt.cex =} \FloatTok{1.5}\NormalTok{, }\DataTypeTok{cex =} \FloatTok{0.7}\NormalTok{, }\DataTypeTok{y.intersp =} \FloatTok{0.5}\NormalTok{, }
\DataTypeTok{xjust =} \FloatTok{0.5}\NormalTok{, }\DataTypeTok{title =} \StringTok{"Size class"}\NormalTok{, }\DataTypeTok{title.adj =} \FloatTok{0.3}\NormalTok{, }
\DataTypeTok{text.font =} \DecValTok{2}\NormalTok{, }\DataTypeTok{trace =}\NormalTok{ T, }\DataTypeTok{inset =} \FloatTok{0.1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Plot inset plot in current plot
(\url{https://stackoverflow.com/questions/17041246/how-to-add-an-inset-subplot-to-topright-of-an-r-plot})
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# calculate position of inset}
\NormalTok{plotdim <-}\StringTok{ }\KeywordTok{par}\NormalTok{(}\StringTok{"plt"}\NormalTok{) }\CommentTok{# get plot window dims as fraction of current plot dims }
\NormalTok{xleft =}\StringTok{ }\NormalTok{plotdim[}\DecValTok{2}\NormalTok{] }\OperatorTok{-}\StringTok{ }\NormalTok{(plotdim[}\DecValTok{2}\NormalTok{] }\OperatorTok{-}\StringTok{ }\NormalTok{plotdim[}\DecValTok{1}\NormalTok{]) }\OperatorTok{*}\StringTok{ }\FloatTok{0.5}
\NormalTok{xright =}\StringTok{ }\NormalTok{plotdim[}\DecValTok{2}\NormalTok{] }\CommentTok{#}
\NormalTok{ybottom =}\StringTok{ }\NormalTok{plotdim[}\DecValTok{4}\NormalTok{] }\OperatorTok{-}\StringTok{ }\NormalTok{(plotdim[}\DecValTok{4}\NormalTok{] }\OperatorTok{-}\StringTok{ }\NormalTok{plotdim[}\DecValTok{3}\NormalTok{]) }\OperatorTok{*}\StringTok{ }
\StringTok{ }\FloatTok{0.5} \CommentTok{#}
\NormalTok{ytop =}\StringTok{ }\NormalTok{plotdim[}\DecValTok{4}\NormalTok{] }\CommentTok{#}
\CommentTok{# set position for plot inset}
\KeywordTok{par}\NormalTok{(}\DataTypeTok{fig =} \KeywordTok{c}\NormalTok{(xleft, xright, ybottom, ytop), }\DataTypeTok{mar =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }
\DecValTok{0}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{0}\NormalTok{), }\DataTypeTok{new =} \OtherTok{TRUE}\NormalTok{)}
\KeywordTok{boxplot}\NormalTok{(Eggs }\OperatorTok{~}\StringTok{ }\NormalTok{Size, }\DataTypeTok{data =}\NormalTok{ meso2, }\DataTypeTok{col =} \KeywordTok{adjustcolor}\NormalTok{(colfunc[colvec[}\DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{]], }
\DataTypeTok{alpha =} \FloatTok{0.5}\NormalTok{), }\DataTypeTok{notch =}\NormalTok{ T, }\DataTypeTok{xlab =} \StringTok{"Week"}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{"Diameter (mm)"}\NormalTok{, }
\DataTypeTok{xaxs =} \StringTok{"i"}\NormalTok{, }\DataTypeTok{yaxs =} \StringTok{"i"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Interactive plots with rCharts (javascript and d3 viz)\\
\url{http://ramnathv.github.io/rCharts/}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(devtools)}
\KeywordTok{install_github}\NormalTok{(}\StringTok{"rCharts"}\NormalTok{, }\StringTok{"ramnathv"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Cluster plot\\
\url{https://rpubs.com/dgrtwo/technology-clusters}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(readr)}
\KeywordTok{library}\NormalTok{(dplyr)}
\KeywordTok{library}\NormalTok{(igraph)}
\KeywordTok{library}\NormalTok{(ggraph)}
\KeywordTok{library}\NormalTok{(ggforce)}
\CommentTok{# This shared file contains the number of question}
\CommentTok{# that have each pair of tags This counts only}
\CommentTok{# questions that are not deleted and have a}
\CommentTok{# positive score}
\NormalTok{tag_pair_data <-}\StringTok{ }\KeywordTok{read_csv}\NormalTok{(}\StringTok{"http://varianceexplained.org/files/tag_pairs.csv.gz"}\NormalTok{)}
\NormalTok{relationships <-}\StringTok{ }\NormalTok{tag_pair_data }\OperatorTok{%>%}\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{Fraction =}\NormalTok{ Cooccur}\OperatorTok{/}\NormalTok{Tag1Total) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{filter}\NormalTok{(Fraction }\OperatorTok{>=}\StringTok{ }\FloatTok{0.35}\NormalTok{) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{distinct}\NormalTok{(Tag1)}
\NormalTok{v <-}\StringTok{ }\NormalTok{tag_pair_data }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(Tag1, Tag1Total) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{distinct}\NormalTok{(Tag1) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{filter}\NormalTok{(Tag1 }\OperatorTok{%in%}\StringTok{ }\NormalTok{relationships}\OperatorTok{$}\NormalTok{Tag1 }\OperatorTok{|}\StringTok{ }
\StringTok{ }\NormalTok{Tag1 }\OperatorTok{%in%}\StringTok{ }\NormalTok{relationships}\OperatorTok{$}\NormalTok{Tag2) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{arrange}\NormalTok{(}\KeywordTok{desc}\NormalTok{(Tag1Total))}
\NormalTok{a <-}\StringTok{ }\NormalTok{grid}\OperatorTok{::}\KeywordTok{arrow}\NormalTok{(}\DataTypeTok{length =}\NormalTok{ grid}\OperatorTok{::}\KeywordTok{unit}\NormalTok{(}\FloatTok{0.08}\NormalTok{, }\StringTok{"inches"}\NormalTok{), }
\DataTypeTok{ends =} \StringTok{"first"}\NormalTok{, }\DataTypeTok{type =} \StringTok{"closed"}\NormalTok{)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{2016}\NormalTok{)}
\NormalTok{relationships }\OperatorTok{%>%}\StringTok{ }\KeywordTok{graph_from_data_frame}\NormalTok{(}\DataTypeTok{vertices =}\NormalTok{ v) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{ggraph}\NormalTok{(}\DataTypeTok{layout =} \StringTok{"fr"}\NormalTok{) }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_edge_link}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{alpha =}\NormalTok{ Fraction), }
\DataTypeTok{arrow =}\NormalTok{ a) }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_node_point}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{size =}\NormalTok{ Tag1Total), }
\DataTypeTok{color =} \StringTok{"lightblue"}\NormalTok{) }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_node_text}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{size =}\NormalTok{ Tag1Total, }
\DataTypeTok{label =}\NormalTok{ name), }\DataTypeTok{check_overlap =} \OtherTok{TRUE}\NormalTok{) }\OperatorTok{+}\StringTok{ }\KeywordTok{scale_size_continuous}\NormalTok{(}\DataTypeTok{range =} \KeywordTok{c}\NormalTok{(}\DecValTok{2}\NormalTok{, }
\DecValTok{9}\NormalTok{)) }\OperatorTok{+}\StringTok{ }\NormalTok{ggforce}\OperatorTok{::}\KeywordTok{theme_no_axes}\NormalTok{() }\OperatorTok{+}\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position =} \StringTok{"none"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Define global plotting graphics function.
The \texttt{plot\_it.R} function is updated on the
\href{https://raw.githubusercontent.com/darwinanddavis/plot_it/master/plot_it.R}{plot\_it
Github page}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(ggplot2)}
\KeywordTok{require}\NormalTok{(ggthemes)}
\NormalTok{### set plotting params plotting function (plot for}
\NormalTok{### MS or not, set bg color, set color palette from}
\NormalTok{### RColorBrewer, set alpha value for transperancy)}
\NormalTok{plot_it <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(manuscript, bg, cp1, cp2, alpha, }
\NormalTok{ family) \{}
\KeywordTok{graphics.off}\NormalTok{()}
\ControlFlowTok{if}\NormalTok{ (manuscript }\OperatorTok{==}\StringTok{ }\DecValTok{0}\NormalTok{) \{}
\ControlFlowTok{if}\NormalTok{ (bg }\OperatorTok{==}\StringTok{ "black"}\NormalTok{) \{}
\NormalTok{ colvec <<-}\StringTok{ }\KeywordTok{magma}\NormalTok{(}\DecValTok{200}\NormalTok{, }\DecValTok{1}\NormalTok{) }\CommentTok{# plot window bg # USES <<- OPERATOR}
\KeywordTok{par}\NormalTok{(}\DataTypeTok{bg =}\NormalTok{ colvec[}\DecValTok{1}\NormalTok{], }\DataTypeTok{col.axis =} \StringTok{"white"}\NormalTok{, }
\DataTypeTok{col.lab =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{col.main =} \StringTok{"white"}\NormalTok{, }
\DataTypeTok{fg =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{bty =} \StringTok{"n"}\NormalTok{, }\DataTypeTok{las =} \DecValTok{1}\NormalTok{, }\DataTypeTok{mar =} \KeywordTok{c}\NormalTok{(}\DecValTok{5}\NormalTok{, }
\DecValTok{6}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{2}\NormalTok{), }\DataTypeTok{family =}\NormalTok{ family) }\CommentTok{#mono}
\NormalTok{ border =}\StringTok{ }\KeywordTok{adjustcolor}\NormalTok{(}\StringTok{"purple"}\NormalTok{, }\DataTypeTok{alpha =} \FloatTok{0.5}\NormalTok{)}
\NormalTok{ \} }\ControlFlowTok{else}\NormalTok{ \{}
\NormalTok{ colvec <<-}\StringTok{ }\KeywordTok{bpy.colors}\NormalTok{(}\DecValTok{200}\NormalTok{) }\CommentTok{# plot window bg # USES <<- OPERATOR}
\KeywordTok{par}\NormalTok{(}\DataTypeTok{bg =}\NormalTok{ colvec[}\DecValTok{1}\NormalTok{], }\DataTypeTok{col.axis =} \StringTok{"white"}\NormalTok{, }
\DataTypeTok{col.lab =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{col.main =} \StringTok{"white"}\NormalTok{, }
\DataTypeTok{fg =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{bty =} \StringTok{"n"}\NormalTok{, }\DataTypeTok{las =} \DecValTok{1}\NormalTok{, }\DataTypeTok{mar =} \KeywordTok{c}\NormalTok{(}\DecValTok{5}\NormalTok{, }
\DecValTok{6}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{2}\NormalTok{), }\DataTypeTok{family =}\NormalTok{ family)}
\NormalTok{ border =}\StringTok{ }\KeywordTok{adjustcolor}\NormalTok{(}\StringTok{"blue"}\NormalTok{, }\DataTypeTok{alpha =} \FloatTok{0.5}\NormalTok{)}
\NormalTok{ \}}
\NormalTok{ \} }\ControlFlowTok{else}\NormalTok{ \{}
\CommentTok{# graphics.off()}
\KeywordTok{par}\NormalTok{(}\DataTypeTok{bty =} \StringTok{"n"}\NormalTok{, }\DataTypeTok{las =} \DecValTok{1}\NormalTok{, }\DataTypeTok{family =}\NormalTok{ family)}
\NormalTok{ colv <-}\StringTok{ "white"}
\NormalTok{ \}}
\CommentTok{# color palettes}
\CommentTok{# ifelse(manuscript==1,colvec<-adjustcolor(brewer.pal(9,cp1)[9],}
\CommentTok{# alpha = alpha),colvec <-}
\CommentTok{# adjustcolor(brewer.pal(9,cp1)[5], alpha = alpha))}
\CommentTok{# # fine tune plotting colors for plotting bg}
\CommentTok{# colfunc <<-}
\CommentTok{# colorRampPalette(brewer.pal(9,cp1),alpha=alpha)}
\NormalTok{ cp1_info <-}\StringTok{ }\NormalTok{brewer.pal.info[cp1, ]}\OperatorTok{$}\NormalTok{maxcolors}
\NormalTok{ cp2_info <-}\StringTok{ }\NormalTok{brewer.pal.info[cp2, ]}\OperatorTok{$}\NormalTok{maxcolors}
\NormalTok{ colv <<-}\StringTok{ }\KeywordTok{brewer.pal}\NormalTok{(cp1_info, cp1) }\CommentTok{# USES <<- OPERATOR}
\NormalTok{ colv2 <<-}\StringTok{ }\KeywordTok{brewer.pal}\NormalTok{(cp2_info, cp2) }\CommentTok{# USES <<- OPERATOR}
\NormalTok{\}}
\CommentTok{# Setting ggplot theme graphics bg = colour to plot}
\CommentTok{# bg, family = font family}
\NormalTok{plot_it_gg <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(bg) \{}
\ControlFlowTok{if}\NormalTok{ (bg }\OperatorTok{==}\StringTok{ "white"}\NormalTok{) \{}
\NormalTok{ bg <-}\StringTok{ "white"}
\NormalTok{ fg <-}\StringTok{ "black"}
\KeywordTok{theme_tufte}\NormalTok{(}\DataTypeTok{base_family =} \StringTok{"HersheySans"}\NormalTok{) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{panel.border =} \KeywordTok{element_blank}\NormalTok{(), }\DataTypeTok{panel.grid.major =} \KeywordTok{element_blank}\NormalTok{(), }
\DataTypeTok{panel.grid.minor =} \KeywordTok{element_blank}\NormalTok{(), }
\DataTypeTok{panel.background =} \KeywordTok{element_rect}\NormalTok{(}\DataTypeTok{fill =}\NormalTok{ bg, }
\DataTypeTok{colour =}\NormalTok{ bg), }\DataTypeTok{plot.background =} \KeywordTok{element_rect}\NormalTok{(}\DataTypeTok{fill =}\NormalTok{ bg)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.line =} \KeywordTok{element_line}\NormalTok{(}\DataTypeTok{color =}\NormalTok{ fg)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.ticks =} \KeywordTok{element_line}\NormalTok{(}\DataTypeTok{color =}\NormalTok{ fg)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{plot.title =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour =}\NormalTok{ fg)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.title.x =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour =}\NormalTok{ fg), }
\DataTypeTok{axis.title.y =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour =}\NormalTok{ fg)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.text.x =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{color =}\NormalTok{ fg), }
\DataTypeTok{axis.text.y =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{color =}\NormalTok{ fg)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.key =} \KeywordTok{element_rect}\NormalTok{(}\DataTypeTok{fill =}\NormalTok{ bg)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.title =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour =}\NormalTok{ fg)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.text =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour =}\NormalTok{ fg))}
\NormalTok{ \}}
\NormalTok{\} }\CommentTok{# end gg}
\NormalTok{### Set plotting function}
\end{Highlighting}
\end{Shaded}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(}\StringTok{"RCurl"}\NormalTok{)}
\NormalTok{script <-}\StringTok{ }\KeywordTok{getURL}\NormalTok{(}\StringTok{"https://raw.githubusercontent.com/darwinanddavis/plot_it/master/plot_it.R"}\NormalTok{, }
\DataTypeTok{ssl.verifypeer =} \OtherTok{FALSE}\NormalTok{)}
\KeywordTok{eval}\NormalTok{(}\KeywordTok{parse}\NormalTok{(}\DataTypeTok{text =}\NormalTok{ script))}
\KeywordTok{cat}\NormalTok{(}\StringTok{"plot_it( }\CharTok{\textbackslash{}n}\StringTok{0 for presentation, 1 for manuscript, }\CharTok{\textbackslash{}n}\StringTok{set colour for background, }\CharTok{\textbackslash{}n}\StringTok{set colour palette. use 'display.brewer.all()', }\CharTok{\textbackslash{}n}\StringTok{set alpha for colour transperancy, }\CharTok{\textbackslash{}n}\StringTok{set font style }\CharTok{\textbackslash{}n}\StringTok{)"}\NormalTok{)}
\KeywordTok{plot_it}\NormalTok{(}\DecValTok{0}\NormalTok{, }\StringTok{"blue"}\NormalTok{, }\StringTok{"Spectral"}\NormalTok{, }\StringTok{"Greens"}\NormalTok{, }\DecValTok{1}\NormalTok{, }\StringTok{"mono"}\NormalTok{) }\CommentTok{# set col function params}
\KeywordTok{plot_it_gg}\NormalTok{(}\StringTok{"white"}\NormalTok{) }\CommentTok{# same as above }
\end{Highlighting}
\end{Shaded}
Make plot cycle on one page
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(m_abundance}\OperatorTok{$}\NormalTok{gam, }\DataTypeTok{pages =} \DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Get plot summaries and values from plot
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot.gam}\NormalTok{(m_abundance}\OperatorTok{$}\NormalTok{gam, }\DataTypeTok{shade =}\NormalTok{ T, }\DataTypeTok{pages =} \DecValTok{1}\NormalTok{, }\DataTypeTok{seWithMean =}\NormalTok{ T)[}\DecValTok{1}\NormalTok{] }\CommentTok{# everything}
\KeywordTok{plot.gam}\NormalTok{(m_abundance}\OperatorTok{$}\NormalTok{gam, }\DataTypeTok{shade =}\NormalTok{ T, }\DataTypeTok{pages =} \DecValTok{1}\NormalTok{, }\DataTypeTok{seWithMean =}\NormalTok{ T)[}\DecValTok{1}\NormalTok{][[}\DecValTok{1}\NormalTok{]]}\OperatorTok{$}\NormalTok{x }\CommentTok{#subset x}
\KeywordTok{plot.gam}\NormalTok{(m_abundance}\OperatorTok{$}\NormalTok{gam, }\DataTypeTok{shade =}\NormalTok{ T, }\DataTypeTok{pages =} \DecValTok{1}\NormalTok{, }\DataTypeTok{seWithMean =}\NormalTok{ T)[}\DecValTok{1}\NormalTok{][[}\DecValTok{1}\NormalTok{]]}\OperatorTok{$}\NormalTok{fit }\CommentTok{#get values to produce fit curve }
\end{Highlighting}
\end{Shaded}
Package for stock world maps
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# worldmap}
\KeywordTok{library}\NormalTok{(choroplethrMaps)}
\end{Highlighting}
\end{Shaded}
Circle packing, tree, dendogram, network plots
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# dendogram tree nested bubble circle packing}
\CommentTok{# network}
\CommentTok{# https://www.r-graph-gallery.com/313-basic-circle-packing-with-several-levels/}
\CommentTok{# circle packing plot Libraries}
\NormalTok{p <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"ggraph"}\NormalTok{, }\StringTok{"igraph"}\NormalTok{, }\StringTok{"tidyverse"}\NormalTok{, }\StringTok{"DeducerSpatial"}\NormalTok{, }
\StringTok{"Rcpp"}\NormalTok{, }\StringTok{"car"}\NormalTok{)}
\KeywordTok{install.packages}\NormalTok{(p, }\DataTypeTok{dependencies =}\NormalTok{ T)}
\KeywordTok{lapply}\NormalTok{(p, library, }\DataTypeTok{character.only =}\NormalTok{ T)}
\CommentTok{# We need a data frame giving a hierarchical}
\CommentTok{# structure. Let's consider the flare dataset:}
\NormalTok{edges =}\StringTok{ }\NormalTok{flare}\OperatorTok{$}\NormalTok{edges}
\CommentTok{# edges cols = character}
\CommentTok{# Usually we associate another dataset that give}
\CommentTok{# information about each node of the dataset:}
\NormalTok{vertices =}\StringTok{ }\NormalTok{flare}\OperatorTok{$}\NormalTok{vertices}
\CommentTok{# vertices cols = character, numeric, character}
\CommentTok{# Create a subset of the dataset (I remove 1 level)}
\NormalTok{edges =}\StringTok{ }\NormalTok{flare}\OperatorTok{$}\NormalTok{edges }\OperatorTok{%>%}\StringTok{ }\KeywordTok{filter}\NormalTok{(to }\OperatorTok{%in%}\StringTok{ }\NormalTok{from) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{droplevels}\NormalTok{()}
\NormalTok{vertices =}\StringTok{ }\NormalTok{flare}\OperatorTok{$}\NormalTok{vertices }\OperatorTok{%>%}\StringTok{ }\KeywordTok{filter}\NormalTok{(name }\OperatorTok{%in%}\StringTok{ }\KeywordTok{c}\NormalTok{(edges}\OperatorTok{$}\NormalTok{from, }
\NormalTok{ edges}\OperatorTok{$}\NormalTok{to)) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{droplevels}\NormalTok{()}
\NormalTok{vertices}\OperatorTok{$}\NormalTok{size =}\StringTok{ }\KeywordTok{runif}\NormalTok{(}\KeywordTok{nrow}\NormalTok{(vertices))}
\CommentTok{# Then we have to make a 'graph' object using the}
\CommentTok{# igraph library:}
\NormalTok{mygraph <-}\StringTok{ }\KeywordTok{graph_from_data_frame}\NormalTok{(edges, }\DataTypeTok{vertices =}\NormalTok{ vertices)}
\CommentTok{# circle packing}
\KeywordTok{ggraph}\NormalTok{(mygraph, }\DataTypeTok{layout =} \StringTok{"circlepack"}\NormalTok{, }\DataTypeTok{weight =} \StringTok{"size"}\NormalTok{, }
\DataTypeTok{sort.by =} \OtherTok{NULL}\NormalTok{, }\DataTypeTok{direction =} \StringTok{"out"}\NormalTok{) }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_node_circle}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{fill =}\NormalTok{ depth)) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{geom_node_text}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{label =}\NormalTok{ shortName, }\DataTypeTok{filter =}\NormalTok{ leaf, }
\DataTypeTok{fill =}\NormalTok{ depth, }\DataTypeTok{size =}\NormalTok{ size)) }\OperatorTok{+}\StringTok{ }\KeywordTok{theme_void}\NormalTok{() }\OperatorTok{+}\StringTok{ }
\StringTok{ }\CommentTok{# theme(legend.position='F') + #show legend}
\KeywordTok{scale_fill_viridis}\NormalTok{(}\DataTypeTok{alpha =} \FloatTok{0.5}\NormalTok{, }\DataTypeTok{direction =} \OperatorTok{-}\DecValTok{1}\NormalTok{, }\DataTypeTok{option =} \StringTok{"magma"}\NormalTok{)}
\CommentTok{# scale_fill_distiller(palette = 'Blues')}
\CommentTok{# geom_node_label(aes(label=shortName, filter=leaf,}
\CommentTok{# size=size)) + # add text boxes}
\CommentTok{# circular dendo}
\KeywordTok{str}\NormalTok{(mygraph)}
\KeywordTok{ggraph}\NormalTok{(mygraph, }\DataTypeTok{layout =} \StringTok{"dendrogram"}\NormalTok{, }\DataTypeTok{circular =}\NormalTok{ T) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{geom_edge_diagonal}\NormalTok{(}\DataTypeTok{flipped =}\NormalTok{ F, }\DataTypeTok{label_colour =} \StringTok{"black"}\NormalTok{, }
\DataTypeTok{label_alpha =} \DecValTok{1}\NormalTok{, }\DataTypeTok{angle_calc =} \StringTok{"rot"}\NormalTok{, }\DataTypeTok{force_flip =} \OtherTok{TRUE}\NormalTok{, }
\DataTypeTok{label_dodge =} \OtherTok{NULL}\NormalTok{, }\DataTypeTok{label_push =} \OtherTok{NULL}\NormalTok{, }\DataTypeTok{show.legend =} \OtherTok{NA}\NormalTok{) }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme_void}\NormalTok{() }\OperatorTok{+}\StringTok{ }\CommentTok{# theme(legend.position='none') +}
\KeywordTok{scale_fill_distiller}\NormalTok{(}\DataTypeTok{palette =} \StringTok{"Blues"}\NormalTok{)}
\CommentTok{# tree map}
\KeywordTok{ggraph}\NormalTok{(mygraph, }\StringTok{"treemap"}\NormalTok{, }\DataTypeTok{weight =} \StringTok{"size"}\NormalTok{) }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_node_tile}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{fill =}\NormalTok{ depth), }
\DataTypeTok{size =} \FloatTok{0.25}\NormalTok{) }\OperatorTok{+}\StringTok{ }\KeywordTok{theme_void}\NormalTok{() }\OperatorTok{+}\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position =} \StringTok{"none"}\NormalTok{)}
\CommentTok{# circular partition}
\KeywordTok{ggraph}\NormalTok{(mygraph, }\StringTok{"partition"}\NormalTok{, }\DataTypeTok{circular =} \OtherTok{TRUE}\NormalTok{) }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_node_arc_bar}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{fill =}\NormalTok{ depth), }
\DataTypeTok{size =} \FloatTok{0.25}\NormalTok{) }\OperatorTok{+}\StringTok{ }\KeywordTok{theme_void}\NormalTok{() }\OperatorTok{+}\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position =} \StringTok{"none"}\NormalTok{)}
\CommentTok{# node}
\KeywordTok{ggraph}\NormalTok{(mygraph) }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_edge_link}\NormalTok{() }\OperatorTok{+}\StringTok{ }\KeywordTok{geom_node_point}\NormalTok{() }\OperatorTok{+}\StringTok{ }
\StringTok{ }\KeywordTok{theme_void}\NormalTok{() }\OperatorTok{+}\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position =} \StringTok{"none"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Insert an animal silhouette into a plot
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 1. Get image from http://www.phylopic.org}
\KeywordTok{library}\NormalTok{(png)}
\NormalTok{ima <-}\StringTok{ }\KeywordTok{readPNG}\NormalTok{(}\StringTok{"thething.png"}\NormalTok{)}
\KeywordTok{plot}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{)}
\KeywordTok{rasterImage}\NormalTok{(}\DataTypeTok{image =}\NormalTok{ ima, }\DataTypeTok{xleft =} \DecValTok{2}\NormalTok{, }\DataTypeTok{ybottom =} \FloatTok{1.8}\NormalTok{, }
\DataTypeTok{xright =} \FloatTok{2.7}\NormalTok{, }\DataTypeTok{ytop =} \FloatTok{2.7}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Create an empty plot window
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# 1}
\KeywordTok{plot}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DataTypeTok{type =} \StringTok{"n"}\NormalTok{, }\DataTypeTok{axes =} \OtherTok{FALSE}\NormalTok{, }\DataTypeTok{ann =} \OtherTok{FALSE}\NormalTok{)}
\CommentTok{# 2}
\KeywordTok{plot}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{type =} \StringTok{"n"}\NormalTok{, }\DataTypeTok{xlab =} \StringTok{""}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{""}\NormalTok{, }\DataTypeTok{xlim =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }
\DecValTok{10}\NormalTok{), }\DataTypeTok{ylim =} \KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{10}\NormalTok{))}
\CommentTok{# 3}
\KeywordTok{plot.new}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
Set color gradient, palette for smoothing data points
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(RColorBrewer)}
\NormalTok{alpha <-}\StringTok{ }\FloatTok{0.8} \CommentTok{# transparency (0 to 1 value)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{5000}\NormalTok{)}
\NormalTok{rr <-}\StringTok{ }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{5000}\NormalTok{)}
\CommentTok{# user defined gradient}
\NormalTok{col <-}\StringTok{ }\KeywordTok{colorRampPalette}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"steelblue"}\NormalTok{, }\StringTok{"lightblue"}\NormalTok{, }
\StringTok{"orange"}\NormalTok{, }\StringTok{"red"}\NormalTok{)) }\CommentTok{# set your own col gradient with as many colours as you want}
\NormalTok{colfunc <-}\StringTok{ }\KeywordTok{col}\NormalTok{(}\KeywordTok{length}\NormalTok{(rr))[}\KeywordTok{as.numeric}\NormalTok{(}\KeywordTok{cut}\NormalTok{(rr, }\DataTypeTok{breaks =} \KeywordTok{length}\NormalTok{(rr)))] }\CommentTok{# define breaks in col gradient}
\KeywordTok{plot}\NormalTok{(rr, }\DataTypeTok{col =}\NormalTok{ colfunc, }\DataTypeTok{pch =} \DecValTok{20}\NormalTok{)}
\CommentTok{# gradient from palette}
\KeywordTok{display.brewer.all}\NormalTok{()}
\NormalTok{col <-}\StringTok{ "Greens"}
\NormalTok{col <-}\StringTok{ }\KeywordTok{colorRampPalette}\NormalTok{(}\KeywordTok{brewer.pal}\NormalTok{(brewer.pal.info[col, }
\NormalTok{ ]}\OperatorTok{$}\NormalTok{maxcolors, col)) }\CommentTok{# col gradient}
\NormalTok{colfunc <-}\StringTok{ }\KeywordTok{col}\NormalTok{(}\KeywordTok{length}\NormalTok{(rr))[}\KeywordTok{as.numeric}\NormalTok{(}\KeywordTok{cut}\NormalTok{(rr, }\DataTypeTok{breaks =} \KeywordTok{length}\NormalTok{(rr)))] }\CommentTok{# define breaks in col gradient}
\KeywordTok{plot}\NormalTok{(rr, }\DataTypeTok{col =}\NormalTok{ colfunc, }\DataTypeTok{pch =} \DecValTok{20}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
Add plot point every nth element
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{n <-}\StringTok{ }\DecValTok{3}
\KeywordTok{plot}\NormalTok{(}\KeywordTok{runif}\NormalTok{(}\DecValTok{10}\NormalTok{, }\DecValTok{0}\NormalTok{, }\DecValTok{1}\NormalTok{), }\DataTypeTok{type =} \StringTok{"o"}\NormalTok{, }\DataTypeTok{pch =} \KeywordTok{c}\NormalTok{(}\DecValTok{20}\NormalTok{, }\KeywordTok{rep}\NormalTok{(}\OtherTok{NA}\NormalTok{, }
\NormalTok{ n)))}
\end{Highlighting}
\end{Shaded}
Create function to make line as default type in plot
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{lplot <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(...) }\KeywordTok{plot}\NormalTok{(..., }\DataTypeTok{type =} \StringTok{"l"}\NormalTok{)}
\KeywordTok{lplot}\NormalTok{(}\KeywordTok{runif}\NormalTok{(}\DecValTok{200}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
Stack dataframe columns automatically in plot
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{head}\NormalTok{(outplot)}
\CommentTok{# time N P S I 1 0.00 200.000000 200.0000 20.00000}
\CommentTok{# 2.000000 2 0.01 78.245140 177.1952 20.58217}
\CommentTok{# 2.067159 3 0.02 34.785145 168.9650 21.12174}
\CommentTok{# 2.136073}
\NormalTok{dats <-}\StringTok{ }\KeywordTok{zoo}\NormalTok{(outplot)}
\KeywordTok{plot}\NormalTok{(dats)}
\end{Highlighting}
\end{Shaded}
Make 3D scatterplot
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(scatterplot3d)}
\NormalTok{xx <-}\StringTok{ }\KeywordTok{rnorm}\NormalTok{(}\DecValTok{1000}\NormalTok{)}
\NormalTok{yy <-}\StringTok{ }\KeywordTok{runif}\NormalTok{(}\DecValTok{1000}\NormalTok{)}
\NormalTok{dens <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\KeywordTok{rep}\NormalTok{(}\FloatTok{1e-04}\NormalTok{, }\DecValTok{500}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{500}\NormalTok{))}
\NormalTok{controls <-}\StringTok{ }\KeywordTok{runif}\NormalTok{(}\DecValTok{3}\NormalTok{)}
\NormalTok{add.control <-}\StringTok{ }\DecValTok{1}
\NormalTok{dens_val <-}\StringTok{ }\DecValTok{1} \OperatorTok{*}\StringTok{ }\DecValTok{10}\OperatorTok{^-}\DecValTok{10} \CommentTok{# 0 or 1*10^-10. value to knock out blanket of colour on plot surface }
\CommentTok{# linear model of r/ship between coords}
\NormalTok{dens_lm <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(dens }\OperatorTok{~}\StringTok{ }\NormalTok{xx }\OperatorTok{+}\StringTok{ }\NormalTok{yy)}
\NormalTok{xlim <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\KeywordTok{min}\NormalTok{(xx), }\KeywordTok{max}\NormalTok{(xx))}
\NormalTok{ylim <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\KeywordTok{min}\NormalTok{(yy), }\KeywordTok{max}\NormalTok{(yy))}
\NormalTok{zlim =}\StringTok{ }\KeywordTok{c}\NormalTok{(}\KeywordTok{min}\NormalTok{(dens), }\KeywordTok{max}\NormalTok{(dens)) }\CommentTok{# set lims}
\NormalTok{colv <-}\StringTok{ "Blues"}
\NormalTok{colvv <-}\StringTok{ }\KeywordTok{colorRampPalette}\NormalTok{(}\KeywordTok{brewer.pal}\NormalTok{(brewer.pal.info[colv, }
\NormalTok{ ]}\OperatorTok{$}\NormalTok{maxcolors, colv)) }\CommentTok{# col gradient}
\NormalTok{colvv <-}\StringTok{ }\KeywordTok{colorRampPalette}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"steelblue"}\NormalTok{, }\StringTok{"lightblue"}\NormalTok{, }
\StringTok{"orange"}\NormalTok{, }\StringTok{"red"}\NormalTok{)) }\CommentTok{# set your own col gradient with as many colours as you want}
\CommentTok{# colvv<-colorRampPalette(magma(length(dens))) #}
\CommentTok{# set your own col gradient with as many colours as}
\CommentTok{# you want}
\CommentTok{# set col palette}
\NormalTok{colfunc <-}\StringTok{ }\KeywordTok{colvv}\NormalTok{(}\KeywordTok{length}\NormalTok{(dens))[}\KeywordTok{as.numeric}\NormalTok{(}\KeywordTok{cut}\NormalTok{(dens, }
\DataTypeTok{breaks =} \KeywordTok{length}\NormalTok{(dens)))] }\CommentTok{# define breaks in col gradient}
\NormalTok{bg <-}\StringTok{ }\KeywordTok{bpy.colors}\NormalTok{(}\DecValTok{1}\NormalTok{)}
\NormalTok{alpha <-}\StringTok{ }\FloatTok{0.8}
\CommentTok{# pdf(paste0(plot.dir,strat,'_',density,'_',stage,'_kudspdf.pdf'),width=8.27,height=11.69,paper='a4r')}
\CommentTok{# color=ifelse(col_heat==1, adjustcolor(colfunc,}
\CommentTok{# alpha=1),adjustcolor('lightgreen',alpha=0.2)),}
\KeywordTok{scatterplot3d}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ xx, }\DataTypeTok{y =}\NormalTok{ yy, }\DataTypeTok{z =}\NormalTok{ dens, }\DataTypeTok{color =} \KeywordTok{ifelse}\NormalTok{(dens }\OperatorTok{<=}\StringTok{ }
\StringTok{ }\NormalTok{dens_val, }\KeywordTok{adjustcolor}\NormalTok{(}\KeywordTok{ifelse}\NormalTok{(bg }\OperatorTok{==}\StringTok{ }\KeywordTok{bpy.colors}\NormalTok{(}\DecValTok{1}\NormalTok{), }
\KeywordTok{bpy.colors}\NormalTok{(}\DecValTok{1}\NormalTok{), }\StringTok{"white"}\NormalTok{), }\DataTypeTok{alpha =} \FloatTok{0.1}\NormalTok{), }\KeywordTok{adjustcolor}\NormalTok{(colfunc, }
\DataTypeTok{alpha =}\NormalTok{ alpha)), }\DataTypeTok{las =} \DecValTok{1}\NormalTok{, }\DataTypeTok{pch =} \DecValTok{15}\NormalTok{, }\DataTypeTok{type =} \StringTok{"p"}\NormalTok{, }
\DataTypeTok{lty.hplot =} \DecValTok{1}\NormalTok{, }\DataTypeTok{xlim =}\NormalTok{ xlim, }\DataTypeTok{ylim =}\NormalTok{ ylim, }\DataTypeTok{zlim =}\NormalTok{ zlim, }
\DataTypeTok{xlab =} \StringTok{"X"}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{"Y"}\NormalTok{, }\DataTypeTok{zlab =} \StringTok{"Density"}\NormalTok{, }\DataTypeTok{main =} \StringTok{"Main"}\NormalTok{, }
\DataTypeTok{box =}\NormalTok{ F, }\DataTypeTok{lty.axis =} \KeywordTok{par}\NormalTok{(}\DecValTok{1}\NormalTok{), }\DataTypeTok{grid =}\NormalTok{ F, }\DataTypeTok{col.grid =} \KeywordTok{adjustcolor}\NormalTok{(}\StringTok{"gray"}\NormalTok{, }
\DecValTok{1}\NormalTok{), }\DataTypeTok{lty.grid =} \KeywordTok{par}\NormalTok{(}\DecValTok{3}\NormalTok{), }\DataTypeTok{axis =}\NormalTok{ T)}