-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
1291 lines (1087 loc) · 39.7 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
---
title: Useful R code
author: |
| Matthew Malishev
| @darwinanddavis
#bibliography:/Users/malishev/Documents/Melbourne Uni/Thesis_2016/library.bib
fontsize: 10
geometry: margin=1in
documentclass: article
linkcolor: pink
urlcolor: blue
citecolor: red
always_allow_html: yes
output:
md_document:
variant: markdown_github
pdf_document:
includes:
in_header: # add .tex file with header content
highlight: tango
template: null
toc: yes
toc_depth: 4
number_sections: false
fig_width: 4
fig_height: 5
fig_caption: true
df_print: tibble
citation_package: biblatex # natbib
latex_engine: xelatex #pdflatex # lualatex
keep_tex: true # keep .tex file in dir
html_document:
highlight: tango
code_folding: hide
toc: yes
toc_depth: 4
number_sections: no
toc_float: yes
word_document:
highlight: tango
keep_md: yes
pandoc_args: --smart
#reference: mystyles.docx
toc: yes
toc_depth: 4
inludes:
before_body: before_body.tex
subtitle:
tags:
- nothing
- nothingness
params:
dir: "/Users/malishev/Documents/Melbourne Uni/Programs/R code/UsefulCode"
date: !r Sys.Date()
version: !r getRversion()
email: "matthew.malishev [at] gmail.com"
doi: https://github.com/darwinanddavis/UsefulCode
classoption: portrait
# ^['https://github.com/darwinanddavis/UsefulCode'] # footnote
vignette: >
%\VignetteIndexEntry{Useful R code}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
---
<!-- This file is edited in README.Rmd -->
# Useful `R` Code
## Links to HTML versions
### [Useful Code](https://darwinanddavis.github.io/UsefulCode/UsefulCode.html)
\
### [Useful Code 2](https://darwinanddavis.github.io/UsefulCode/UsefulCode2.html)
\
### [Useful Code in `ggplot`](https://darwinanddavis.github.io/UsefulCode/UsefulCode_ggplot.html)
\
### [Useful Code - sf and geos](https://darwinanddavis.github.io/UsefulCode/UsefulCode_sf.html)
\
<!-- <script type="text/x-mathjax-config"> -->
<!-- MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "all"} } }); -->
<!-- </script> -->
```{r echo = FALSE, eval=F, warning=F}
library(rmarkdown)
# setwd("")
# f <- list.files()[1]
# render(f, output_format='pdf_document')
# render(f, output_format='pdf_document')
```
```{r, set-options, echo = FALSE, cache = FALSE}
options(width=100)
knitr::opts_chunk$set(
eval = F, # run all code
# echo = FALSE, # show code chunks in output
comment = "",
tidy.opts=list(width.cutoff=100), # set width of code chunks in output
tidy=TRUE, # make output as tidy
message = FALSE, # mask all messages
warning = FALSE, # mask all warnings
size="small" # set code chunk size
)
# https://github.com/ucb-stat133/stat133-fall-2016/blob/master/hws/hw02-tables-ggplot.Rmd
knitr::opts_knit$set(root.dir=paste0(params$dir,"/")) # set working dir
setwd(paste0(params$dir,"/")) # for running just in R not knitr
```
Date: `r params$date`
`R` version: `r params$version`
*Corresponding author: `r params$email`
This document can be found at `r params$doi`
\newpage
## Overview
This document outlines some useful `R` code for plotting, cool functions, and other random tidbits.
### Install dependencies
```{r, load packages, include=F, cache=F, message=F, warning=F, results='hide', echo=T}
packages <- c("rgdal","dplyr","zoo","RColorBrewer","viridis","plyr","digitize","jpeg","devtools","imager","dplyr","ggplot2","ggridges","ggjoy","ggthemes","ggforce","svDialogs","data.table","tibble","extrafont","sp","readr","igraph","ggraph","jsonlite","deSolve")
if (require(packages)) {
install.packages(packages,dependencies = T)
require(packages)
}
lapply(packages,require,character.only=T)
ppp <- lapply(packages,require,character.only=T)
if(any(ppp==F)){print("Check packages are loaded")}
sessionInfo()
```
### Attributes
Access structural attributes of unique classes, such as raster and ggmap (bbox).
``` {r, attr1, results='hide',eval=F}
# Normal example
df <- data.frame("X"=c(1:5),"Y"=c(6:10))
str(df)
df$X
# `attr` method
require(ggmap)
map <- get_map("Atlanta",zoom=12,source="stamen",maptype="toner-lines")
str(map)
attr(map,"bb")$ll.lat
```
### Classes
Convert character to factor to numeric without conversion error
```{r, classes1, results='hide',eval=F}
read.table(f,header=T,sep=",",row.names=NULL,stringsAsFactors=FALSE, strip.white=TRUE)
f$V2<-as.numeric(f$V2)
```
See call options for class
```{r, classes2,eval=F}
methods(class="estUDm")
```
Set dynamic input for variable / assign variable to char vector
```{r, classes3, results='hide',eval=F}
shadedens<-function(shadedens){ # set shade density to clumped (to match food) or sparse
if (shadedens == "Random"){
NLCommand("set Shade-density \"Random\" ")
}else{
NLCommand("set Shade-density \"Clumped\" ")
}
}
shadedens("Clumped") # set clumped resources
```
### D3 apps
Interactive network plots using d3
```{r, d31, results='hide',eval=F}
# Load package
install.packages("networkD3")
library(networkD3)
# Load energy projection data
URL <- "https://cdn.rawgit.com/christophergandrud/networkD3/master/JSONdata/energy.json"
Energy <- jsonlite::fromJSON(URL)
# Now we have 2 data frames: a 'links' data frame with 3 columns (from, to, value), and a 'nodes' data frame that gives the name of each node.
head(Energy$links)
head(Energy$nodes)
# Thus we can plot it
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "TWh", fontSize = 12, nodeWidth = 30)
?sankeyNetwork
```
### Dataframes
Optimal empty data frame
```{r, df1, results='hide',eval=F}
df <- data.frame(Date=as.Date(character()),
X=numeric(),
Y=integer(),
stringsAsFactors=FALSE)
```
Add df cols with `mutate`
```{r, df2, results='hide',eval=F}
require(dplyr)
df <- data.frame("a"=rnorm(10),"b"=(1:20))
df %>%
mutate(
"c"=rnorm(20),
b = b *67
)
```
Change `df` column names
```{r, df3, results='hide',eval=F}
colnames(data)[c(1,2,3)] <- c("TimeStamp","Lat","Long")
```
Remove multiple columns from df
```{r, df4, results='hide',eval=F}
### Remove multiple NA columns
rm_cols <- grep("NA", names(tt), ignore.case = F)
df[,colnames(df[,rm_cols])] <- list(NULL)
```
Check number of characters in each column
```{r, df5, results="hide",eval=F}
sapply(meso1, function(x) sum(nchar(x)))
```
### Generic functions
Generic useful functions that I can't place under any other headings here
```{r, gen1, results='hide',eval=F}
# dput() for converting outputs such as copied text or data tables into vectors
xx <- "Some copied text or table from the internet"
dput(xx)
```
Round up integers to optimal rounded value
```{r, gen2, results='hide',eval=F}
nn <- c(46,11,23)
round_any(nn,10)
round_any(nn,10,ceiling)
round_any(nn,10,floor)
```
Get summary stats for dataset (means)
```{r, gen3, results='hide',eval=F}
means = aggregate(Cumulative_cercs ~ r*hb, data=df, FUN=mean)
```
### `ggplot` functions
Remove annoying stock gridlines from plot window
```{r, ggplot1, results='hide',eval=F}
plot + theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
# alternative (after loading ggridges library)
theme_ridges(grid=F,center_axis_labels = T)
```
Setting global graphics theme for ggplot
```{r, ggplot2, results='hide',eval=F}
plot_it_gg <- function(bg,family){ # bg = colour to plot bg, family = font family
theme_tufte(base_family = family) +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bg,
colour = bg),
plot.background = element_rect(fill=bg)
) +
theme(axis.line = element_line(color = "white")) +
theme(axis.ticks = element_line(color = "white")) +
theme(plot.title = element_text(colour = "white")) +
theme(axis.title.x = element_text(colour = "white"),
axis.title.y = element_text(colour = "white")) +
theme(axis.text.x = element_text(color = "white"),
axis.text.y = element_text(color = "white")) +
theme(legend.key = element_rect(fill = bg)) +
theme(legend.title = element_text(colour="white")) +
theme(legend.text = element_text(colour="white"))
}
```
Put plot in function to take dynamic data inputs
Ref: http://jcborras.net/carpet/visualizing-political-divergences-2012-local-elections-in-helsinki.html
```{r, ggplot3, results='hide',eval=F}
hr.mass.plot <- function(d) {
p <- ggplot(d, aes(HR, Mass, color = colfunc)) +
geom_density_2d(data=d, aes(x = HR, y = Mass),
stat = "density2d",position="identity",
color=adjustcolor("orange",alpha=0.8), size=1.5, contour = T, lineend="square",linejoin="round")
p <- p + geom_point(data=d, aes(x = HR, y = Mass),
color=colfunc,
fill=colfunc) +
scale_color_manual(values = magma(8))
p <- p + scale_y_continuous(limits=c(-200,200), name="Mass lost (g)")
p <- p + scale_x_continuous(limits=c(0,0.35),name=expression("Home range area (km^2)"))
p <- p + theme_classic()
print(p)
}
hr.mass.plot(d)
```
Using `ggplot` when looping through `for` loop and saving to dir
```{r, ggplot4,eval=F}
pdf("mypdf.pdf",onefile = T)
for(i in 1:3){
par(bty="n", las = 1)
grid.arrange(
ggplot(data, aes(x = X, y = Y, fill=..x..)) +
geom_density_ridges_gradient(scale = 5, size=0.2,color="black", rel_min_height = 0.01,panel_scaling=T,alpha=0.2) +
geom_density_ridges(scale = 5, size=0.2,color="black", rel_min_height = 0.01,fill="white",alpha=0.2) +
scale_fill_viridis(name = "Diameter", alpha=0.1, option = "magma",direction=-1) +
xlim(c(0,25)) +
labs(title = paste0("Title_",i)) +
xlab("X") +
ylab("Y")
)
}
# end loop
dev.off()
# geom_density_ridges() # scale = overlap
# geom_density_ridges(scale = 5, size=0.2,color="white", rel_min_height = 0.01,fill=col,alpha=0.5) +
# scale_fill_viridis option = "magma", "inferno","plasma", "viridis", "cividis"
```
Converting lists and dataframes to usable format for `ggplot` (`melt` package)
```{r, ggplot5, results='hide',eval=F}
# ------------------------- plot individual outputs -------------------------
mm_ = readRDS(paste0(model.path,fh,".R"))
cat("order = cerc, food, juv, adult, infected, infected shedding, host length, parasite mass")
# plot master
mm <- mm_[[2]]
y_m <- melt(mm);y_m
ggplot() +
geom_point(data = y_m, aes(x = rep.int(1:n.ticks,max(L1)) , y = value, group = L1, colour=factor(L1)), ) +
geom_line(data = y_m, aes(x = rep.int(1:n.ticks,max(L1)) , y = value, group = L1, colour=factor(L1)), ) +
#linetype=y_m$L1) +
theme_tufte()
# + geom_text(x=,y=,label = max(value),check_overlap = TUE)
```
Insert math expression in legend title
```{r, ggplot6, results='hide',eval=F}
ggplot() +
labs(title = bquote("Hello" ~ r[xy] ~ "and" ~ B^2))
```
Create double line break with expression in legend title (and labels)
```{r, ggplot7, results='hide',eval=F}
ggplot() +
scale_color_manual(
expression(atop("text",
atop(textstyle(epsilon))))
)
```
### Lists
Find maximum value in entire list
```{r, list1, results='hide',eval=F}
master <- list(1:10,100,rnorm(12))
do.call(max,master)
```
Plot all elements in a list
```{r, list2, results='hide',eval=F}
xx <- list(sample(5,1000,replace=T),rnorm(1000),sample(50,1000,replace=T))
plot(unlist(xx),type="l")
```
Apply each row of df or vector to individual elements of a list
```{r, list3, results='hide',eval=F}
df = data.frame("events" = LETTERS[1:10], "outs" = 1:10)
sapply(df$outs,list)
```
Append extra element onto existing list
```{r, list4, results='hide',eval=F}
rv <- sample(1000,15) # random vector
listvec <- sapply(rep(NA,7),list) # list with 7 empty elements
listvec_final <- c(listvec,list(rv)) # append rv
listvec_final <- c(listvec,rv) # to append rv contents as separate elements, remove internal list
```
### Loops
Save loop output in master list
```{r, loop1, results='hide',eval=F}
pars <- seq(0,1,0.5)
master <- list()
t_list <- list()
for (p in 1:length(pars)){
for(t in 5){
tt <- rnorm(1000*t)
t_list[t] <-tt
}
master[[length(master)+1]] <- t_list # store in master list
}
```
Optimal way to save results to data frame in loop
```{r, loop2, results='hide',eval=F}
require(dplyr)
fun <- sum # sum # choose mean or sum
out_first <- list() # create first empty list
out_second <- list() # create second empty list
for(me in 1:10){
global_output_fh = paste0(getwd(),"/",me,".R") # get file handle
output <- readRDS(global_output_fh) # read in file
cercs <- output[[1]] # get data
# define function
SEM = function(x){
sd(x)/sqrt(length(x))
}
# create col name to pass to aggregate function
cerc_outs = list("Outs" = cercs)
outs = aggregate(Outs ~ ., data=cerc_outs, FUN=fun)
cerc_se = list("SEs" = cercs)
se = aggregate(SEs ~ ., data=cerc_se, FUN=SEM)
# save to df by creating new df cols
outs$me <- me # create new col with iteration
out_first[[me]] <- outs # add first output to list
out_second[[me]] <- se # add second output to list
} # end file read
# option 1
out_final = do.call(rbind, out_first)
# option 2
out_final <- bind_rows(out_first) # make fresh df
out_final$Second <- bind_rows(out_second) # add second col
```
### Maps
High res maps
```{r, map1, results='hide',eval=F}
# https://hecate.hakai.org/rguide/mapping-in-r.html
require(maptools)
d <- map_data("worldHires", c("Colombia","Ecuador","Peru","Panama"))
# plot
ggplot() +
geom_polygon(data=d,aes(x=long, y=lat, group = group), fill = "black", col="pink") +
# theme_tufte(ticks=F) +
theme_nothing() +
coord_map("mercator",xlim=c(-75,-81),ylim=c(-2,8))
```
Read in KMZ/KML data (Google Maps data)
```{r, map2, results='hide',eval=F}
require(sf)
zp <- sf::st_read("ziggy_test.kml")
```
### Messages
Display status message of progress
```{r, message1, results='hide',eval=F}
for(i in 1:10) {
Sys.sleep(0.2)
# Dirk says using cat() like this is naughty ;-)
#cat(i,"\r")
# So you can use message() like this, thanks to Sharpie's
# comment to use appendLF=FALSE.
message(i,"\r",appendLF=FALSE) # appendLF = new line
flush.console()
}
```
Display popup progress bar
```{r, message2, results='hide',eval=F}
require(tcltk)
pb <- tkProgressBar("test progress bar", "Some information in %",
0, 100, 50)
Sys.sleep(0.5)
u <- c(0, sort(runif(20, 0 ,100)), 100)
for(i in u) {
Sys.sleep(0.1)
info <- sprintf("%d%% done", round(i))
setTkProgressBar(pb, i, sprintf("test (%s)", info), info)
}
Sys.sleep(5)
close(pb)
```
### `NAs` and `NaNs `
Replace NAs and NaNs with 0's
```{r, NA1, results='hide',eval=F}
df[is.na(df)] <- 0
df[is.nan(df)] <- 0 # good for matrices
```
Replace X values less than given value (V) with 0
```{r, NA2, results='hide',eval=F}
df$X[df$X<V] <- 0
```
Check for `NAs`
```{r, NA3, results='hide',eval=F}
sapply(df, function(x) sum(is.na(x)))
```
snail.update[is.nan(snail.update)] <- 0 Replace `NaN` and `Inf` values with `NA`
```{r, NA4, results='hide',eval=F}
df$col1[which(!is.finite(df$col1))] <- NA
```
Fill in missing data values in sequence with `NA`
```{r, NA5, results='hide',eval=F}
# /Users/malishev/Documents/Manuscripts/Chapter4/Sims/Chapter4_figs.R
library(zoo)
data <- data.frame(index = c(1:4, 6:10),
data = c(1.5,4.3,5.6,6.7,7.1,12.5,14.5,16.8,3.4))
#you can create a series
z <- zoo(data$data, data$index)
#end extend it to the grid 1:10
z <- merge(zoo(,1:10), z)
#worked example
# fill in missing Tb values
minTb.d <- zoo(minTb$Tick,minTb$Days)
minTb.d <- merge(zoo(NULL,1:days), minTb.d) # make the minTb series match the temp series (117 days)
minTb.d <- as.numeric(minTb.d) # = time individuals reached VTMIN in ticks
minTb <- minTb.d - temp$Tick # get diff between starting time and time to reach VTMIN
minTb <- minTb/2 # convert ticks to minutes
minTb <- minTb/60 #convert to hours
minTb <- data.frame("Days"=1:days,"Time"=minTb)
# then fill in missing values
approx(minTb$Time,method = "linear")
```
Remove rows with NA
```{r, NA6, results='hide',eval=F}
data <- data[!is.na(data$X),]
```
Turn NULLs in list into NAs to get numeric values (fix for 'cannot coerce double' error)
```{r, NA7, results='hide',eval=F}
hl_list <- lapply(hl_list, function(x) ifelse(x == "NULL", NA, x))
```
Turn NaN or NAs in list into 0s
```{r, NA8, results='hide',eval=F}
# NaN
global_output <- rapply(global_output, f=function(x) ifelse(is.nan(x),0,x), how="replace" )
# NA
global_output <- rapply(global_output, f=function(x) ifelse(is.na(x),0,x), how="replace" )
```
### Packages
[`rLandsat`](https://blog.socialcops.com/technology/data-science/announcing-rlandsat-landsat-8-data/)
Sourcing, requesting, and downloading NASA Landsat 8 satellite data.
[Radix](https://blog.rstudio.com/2018/09/19/radix-for-r-markdown/)
Improved `RMarkdown` output and interaction.
[`rpanel`](http://www.stats.gla.ac.uk/~adrian/rpanel/)
[Reference guide](https://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf)
Create interactive GUI control toggles from `R`. Like an early Shiny.
### Plotting
Plot one plot window above and two below
```{r, plot1}
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
```
Bookend axis ticks for plot
E.g. at 0 and 100 when data is 1:99
```{r, plot2, results='hide',eval=F}
axis(1,at=c(0,length(loco$X)),labels=c("",""))# bookending axis tick marks
```
Optimal legend formatting for base
```{r, plot3, results='hide',eval=F}
legend("right",legend=c("Small","Intermediate","Large"),col=c(colfunc[colvec[1:3]]),
bty="n",pch=20,pt.cex=1.5,cex=0.7,y.intersp = 0.5, xjust = 0.5,
title="Size class",title.adj = 0.3,text.font=2,
trace=T,inset=0.1)
```
Plot inset plot in current plot (https://stackoverflow.com/questions/17041246/how-to-add-an-inset-subplot-to-topright-of-an-r-plot)
```{r, plot4, results='hide',eval=F}
# calculate position of inset
plotdim <- par("plt")# get plot window dims as fraction of current plot dims
xleft = plotdim[2] - (plotdim[2] - plotdim[1]) * 0.5
xright = plotdim[2] #
ybottom = plotdim[4] - (plotdim[4] - plotdim[3]) * 0.5 #
ytop = plotdim[4] #
# set position for plot inset
par(fig = c(xleft, xright, ybottom, ytop),mar=c(0,0,0,0),new=TRUE)
boxplot(Eggs~Size,data=meso2,
col=adjustcolor(colfunc[colvec[1:3]],alpha=0.5),
notch = T,xlab="Week",ylab="Diameter (mm)",
xaxs = "i", yaxs = "i"
)
```
Interactive plots with rCharts (javascript and d3 viz)
http://ramnathv.github.io/rCharts/
```{r, plot5, results='hide',eval=F}
require(devtools)
install_github('rCharts', 'ramnathv')
```
Cluster plot
https://rpubs.com/dgrtwo/technology-clusters
```{r, plot6, results='hide',eval=F}
library(readr)
library(dplyr)
library(igraph)
library(ggraph)
library(ggforce)
# This shared file contains the number of question that have each pair of tags
# This counts only questions that are not deleted and have a positive score
tag_pair_data <- read_csv("http://varianceexplained.org/files/tag_pairs.csv.gz")
relationships <- tag_pair_data %>%
mutate(Fraction = Cooccur / Tag1Total) %>%
filter(Fraction >= .35) %>%
distinct(Tag1)
v <- tag_pair_data %>%
select(Tag1, Tag1Total) %>%
distinct(Tag1) %>%
filter(Tag1 %in% relationships$Tag1 |
Tag1 %in% relationships$Tag2) %>%
arrange(desc(Tag1Total))
a <- grid::arrow(length = grid::unit(.08, "inches"), ends = "first", type = "closed")
set.seed(2016)
relationships %>%
graph_from_data_frame(vertices = v) %>%
ggraph(layout = "fr") +
geom_edge_link(aes(alpha = Fraction), arrow = a) +
geom_node_point(aes(size = Tag1Total), color = "lightblue") +
geom_node_text(aes(size = Tag1Total, label = name), check_overlap = TRUE) +
scale_size_continuous(range = c(2, 9)) +
ggforce::theme_no_axes() +
theme(legend.position = "none")
```
Define global plotting graphics function.
The `plot_it.R` function is updated on the [plot_it Github page](https://raw.githubusercontent.com/darwinanddavis/plot_it/master/plot_it.R).
```{r, plot7, eval=F, cache = F, tidy = TRUE, lazy = TRUE, results="hide"}
require(ggplot2)
require(ggthemes)
### set plotting params
plot_it <- function(manuscript,bg,cp1,cp2,alpha,family){ # plotting function (plot for MS or not, set bg color, set color palette from RColorBrewer, set alpha value for transperancy)
graphics.off()
if(manuscript==0){
if(bg=="black"){
colvec <<- magma(200,1) # plot window bg # USES <<- OPERATOR
par(bg = colvec[1],col.axis="white",col.lab="white",col.main="white",fg="white",bty="n",las=1,mar=c(5,6,4,2),family=family) #mono
border=adjustcolor("purple",alpha=0.5)
}else{
colvec <<- bpy.colors(200) # plot window bg # USES <<- OPERATOR
par(bg = colvec[1],col.axis="white",col.lab="white",col.main="white",fg="white",bty="n",las=1,mar=c(5,6,4,2),family=family)
border=adjustcolor("blue",alpha=0.5)
}
}else{
# graphics.off()
par(bty="n",las=1,family=family)
colv<-"white"
}
# color palettes
# ifelse(manuscript==1,colvec<-adjustcolor(brewer.pal(9,cp1)[9], alpha = alpha),colvec <- adjustcolor(brewer.pal(9,cp1)[5], alpha = alpha)) # fine tune plotting colors for plotting bg
# colfunc <<- colorRampPalette(brewer.pal(9,cp1),alpha=alpha)
cp1_info <- brewer.pal.info[cp1,]$maxcolors
cp2_info <- brewer.pal.info[cp2,]$maxcolors
colv <<- brewer.pal(cp1_info,cp1) # USES <<- OPERATOR
colv2 <<- brewer.pal(cp2_info,cp2) # USES <<- OPERATOR
}
# Setting ggplot theme graphics
plot_it_gg <- function(bg){ # bg = colour to plot bg, family = font family
if(bg=="white"){
bg <- "white"
fg <- "black"
theme_tufte(base_family = "HersheySans") +
theme(panel.border = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_rect(fill = bg,colour = bg),plot.background = element_rect(fill=bg)) +
theme(axis.line = element_line(color = fg)) +theme(axis.ticks = element_line(color = fg)) + theme(plot.title = element_text(colour = fg)) +theme(axis.title.x = element_text(colour = fg), axis.title.y = element_text(colour = fg)) + theme(axis.text.x = element_text(color = fg),axis.text.y = element_text(color = fg)) + theme(legend.key = element_rect(fill = bg)) + theme(legend.title = element_text(colour=fg)) + theme(legend.text = element_text(colour=fg))
}
}# end gg
### Set plotting function
```{r eval=F, results="hide"}
require("RCurl")
script <- getURL("https://raw.githubusercontent.com/darwinanddavis/plot_it/master/plot_it.R", ssl.verifypeer = FALSE)
eval(parse(text = script))
cat("plot_it( \n0 for presentation, 1 for manuscript, \nset colour for background, \nset colour palette. use 'display.brewer.all()', \nset alpha for colour transperancy, \nset font style \n)")
plot_it(0,"blue","Spectral","Greens",1,"mono") # set col function params
plot_it_gg("white") # same as above
```
Make plot cycle on one page
```{r plot8, results="hide",eval=F}
plot(m_abundance$gam,pages=1)
```
Get plot summaries and values from plot
```{r plot9, results="hide",eval=F}
plot.gam(m_abundance$gam,shade=T,pages=1,seWithMean = T)[1] # everything
plot.gam(m_abundance$gam,shade=T,pages=1,seWithMean = T)[1][[1]]$x #subset x
plot.gam(m_abundance$gam,shade=T,pages=1,seWithMean = T)[1][[1]]$fit #get values to produce fit curve
```
Package for stock world maps
```{r plot10, results="hide",eval=F}
#worldmap
library(choroplethrMaps)
```
Circle packing, tree, dendogram, network plots
```{r plot11, results="hide",eval=F}
# dendogram tree nested bubble circle packing network
# https://www.r-graph-gallery.com/313-basic-circle-packing-with-several-levels/
# circle packing plot
# Libraries
p <- c("ggraph","igraph","tidyverse","DeducerSpatial","Rcpp","car")
install.packages(p,dependencies = T)
lapply(p,library,character.only=T)
# We need a data frame giving a hierarchical structure. Let's consider the flare dataset:
edges=flare$edges
# edges cols = character
# Usually we associate another dataset that give information about each node of the dataset:
vertices = flare$vertices
# vertices cols = character, numeric, character
# Create a subset of the dataset (I remove 1 level)
edges = flare$edges %>% filter(to %in% from) %>% droplevels()
vertices = flare$vertices %>% filter(name %in% c(edges$from, edges$to)) %>% droplevels()
vertices$size=runif(nrow(vertices))
# Then we have to make a 'graph' object using the igraph library:
mygraph <- graph_from_data_frame( edges, vertices=vertices )
# circle packing
ggraph(mygraph, layout = 'circlepack', weight="size",sort.by=NULL,direction="out") +
geom_node_circle(aes(fill=depth)) +
geom_node_text(aes(label=shortName, filter=leaf, fill=depth, size=size)) +
theme_void() +
# theme(legend.position="F") + #show legend
scale_fill_viridis(alpha=0.5,direction=-1,option="magma")
# scale_fill_distiller(palette = "Blues")
# geom_node_label(aes(label=shortName, filter=leaf, size=size)) + # add text boxes
#circular dendo
str(mygraph)
ggraph(mygraph, layout='dendrogram', circular=T) +
geom_edge_diagonal(flipped=F,
label_colour = "black",
label_alpha = 1,
angle_calc = "rot",
force_flip = TRUE,label_dodge = NULL, label_push = NULL,
show.legend = NA) +
theme_void() +
# theme(legend.position="none") +
scale_fill_distiller(palette = "Blues")
# tree map
ggraph(mygraph, 'treemap', weight = 'size') +
geom_node_tile(aes(fill = depth), size = 0.25) +
theme_void() +
theme(legend.position="none")
# circular partition
ggraph(mygraph, 'partition', circular = TRUE) +
geom_node_arc_bar(aes(fill = depth), size = 0.25) +
theme_void() +
theme(legend.position="none")
# node
ggraph(mygraph) +
geom_edge_link() +
geom_node_point() +
theme_void() +
theme(legend.position="none")
```
Insert an animal silhouette into a plot
```{r, plot12, results='hide',eval=F}
#1. Get image from http://www.phylopic.org
library(png)
ima <- readPNG("thething.png")
plot(1:3,1:3)
rasterImage(image=ima, xleft=2,ybottom=1.8,
xright=2.7,ytop=2.7)
```
Create an empty plot window
```{r, plot13, results='hide',eval=F}
# 1
plot(0,type='n',axes=FALSE,ann=FALSE)
# 2
plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 10))
# 3
plot.new()
```
Set color gradient, palette for smoothing data points
```{r, plot14, results='hide',eval=F}
require(RColorBrewer)
alpha <- 0.8 # transparency (0 to 1 value)
set.seed(5000)
rr <- rnorm(5000)
# user defined gradient
col<-colorRampPalette(c("steelblue","lightblue","orange","red")) # set your own col gradient with as many colours as you want
colfunc <- col(length(rr))[as.numeric(cut(rr,breaks = length(rr)))] # define breaks in col gradient
plot(rr,col=colfunc,pch=20)
# gradient from palette
display.brewer.all()
col <- "Greens"
col<-colorRampPalette(brewer.pal(brewer.pal.info[col,]$maxcolors,col)) # col gradient
colfunc <- col(length(rr))[as.numeric(cut(rr,breaks = length(rr)))] # define breaks in col gradient
plot(rr,col=colfunc,pch=20)
```
Add plot point every nth element
```{r, plot15, results='hide',eval=F}
n <- 3
plot(runif(10, 0, 1), type = "o", pch = c(20, rep(NA, n)))
```
Create function to make line as default type in plot
```{r, plot16, results='hide',eval=F}
lplot <- function(...) plot(..., type="l")
lplot(runif(200))
```
Stack dataframe columns automatically in plot
```{r, plot17, results='hide',eval=F}
head(outplot)
# time N P S I
# 1 0.00 200.000000 200.0000 20.00000 2.000000
# 2 0.01 78.245140 177.1952 20.58217 2.067159
# 3 0.02 34.785145 168.9650 21.12174 2.136073
dats <- zoo(outplot)
plot(dats)
```
Make 3D scatterplot
```{r, plot18, results='hide',eval=F}
require(scatterplot3d)
xx <- rnorm(1000)
yy <- runif(1000)
dens <- c(rep(0.0001,500),rep(1,500))
controls <- runif(3)
add.control <- 1
dens_val <- 1*10^-10 # 0 or 1*10^-10. value to knock out blanket of colour on plot surface
#linear model of r/ship between coords
dens_lm <- lm(dens ~ xx + yy)
xlim <- c(min(xx),max(xx)); ylim <- c(min(yy),max(yy)); zlim=c(min(dens),max(dens)) # set lims
colv <- "Blues"
colvv<-colorRampPalette(brewer.pal(brewer.pal.info[colv,]$maxcolors,colv)) # col gradient
colvv<-colorRampPalette(c("steelblue","lightblue","orange","red")) # set your own col gradient with as many colours as you want
# colvv<-colorRampPalette(magma(length(dens))) # set your own col gradient with as many colours as you want
# set col palette
colfunc <- colvv(length(dens))[as.numeric(cut(dens,breaks = length(dens)))] # define breaks in col gradient
bg <- bpy.colors(1)
alpha <- 0.8
# pdf(paste0(plot.dir,strat,"_",density,"_",stage,"_kudspdf.pdf"),width=8.27,height=11.69,paper="a4r")
# color=ifelse(col_heat==1, adjustcolor(colfunc, alpha=1),adjustcolor("lightgreen",alpha=0.2)),
scatterplot3d(x=xx,y=yy,z=dens,
color=ifelse(dens<=dens_val,adjustcolor(ifelse(bg==bpy.colors(1),bpy.colors(1),"white"),alpha=0.1),adjustcolor(colfunc,alpha=alpha)),
las=1,
pch=15,
type="p",
lty.hplot = 1,
xlim=xlim,
ylim=ylim,
zlim=zlim,
xlab="X",
ylab="Y",
zlab="Density",
main="Main",
box=F,
lty.axis=par(1),
grid=F,
col.grid = adjustcolor("gray",1),
lty.grid=par(3),
axis=T
)
# other plot options
#cex.symbols=dens*3,
#cex.symbols = ifelse(z<=0,0,0.5),
# highlight.3d=T,
# angle=70,
# append the below section starting at the '$' to the above closing bracket
#$plane3d(dens_lm, # add 3d linear model plane. # ??plane3d(Intercept, x.coef = NULL, y.coef = NULL, lty = "dashed", lty.box = NULL, draw_lines = TRUE, draw_polygon = FALSE, polygon_args = list(border = NA, col = rgb(0,0,0,0.2))
# lty="dashed",
# lty.box = NULL,
# draw_lines = F, draw_polygon = T,
# polygon_args = list(border = NA, col = adjustcolor("light green",alpha=0.4)))
# add control dates
if(add.control==1){par(new=T); scatterplot3d(x=rep(0,length(controls)),y=controls,z=rep(max(dens),length(controls)),color="gray",las=1,pch="",lty.hplot = 1,xlim=xlim,ylim=ylim,zlim=zlim,xlab="",ylab="",zlab="",box=F,grid=F,cex.symbols=2,axis=F,type="h")}
```
Adding title from separate list to plot in loop (`ggplot`)
```{r, plot19, results='hide',eval=F}
# plot all sim results in one window
gspl <- list()
ttl_list <- c("cerc","food", "juv", "adult", "infec", "infec (shed)", "host L", "parasite mass")
# choose sim to plot
global_sim_plot <- global_detritus
for(g in 1:10){
gspl[[g]] <- ggplot() +
geom_line(data = y_m, aes(x = rep.int(1:n.ticks,max(L1)) , y = value, group = L1, colour=factor(L1)), ) +
# scale_color_manual(values = viridis(length(mm))) +
#linetype=y_m$L1) +
theme_tufte() +
labs(title=ttl_list[g],x="",y="") +
if(g==length(global_sim_plot)){
theme(legend.title=element_text(size=0.2),
legend.text=element_text(size=0.2)) +
theme(legend.position = "top")
labs(x="Time")
}else{
theme(legend.position="none")
}
}
# + geom_text(x=,y=,label = max(value),check_overlap = TUE)
do.call(grid.arrange,gspl) # plot in one window
```
Using math expressions in plot labels
```{r, plot20, results='hide',eval=F}
plot(rnorm(1000),
xlab=expression(paste("X values"^2)),
ylab=expression(paste("Y values"^3,hat(beta)))
)
```
Adding faint gridlines to plot
```{r, plot21, results='hide',eval=F}
# add gridlines
grid(nx=NA,ny=NULL)
```
Storing current `par` variables for plotting
```{r, plot22, results='hide',eval=F}
og_pars <- par(no.readonly = T) # store current par values
```