-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
2313 lines (1733 loc) · 72.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
---
output: github_document
editor_options:
chunk_output_type: inline
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
library(knitr)
```
# CCAMLRGIS R package
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/CCAMLRGIS)](https://cran.r-project.org/package=CCAMLRGIS)
[![R-hub](https://github.com/ccamlr/CCAMLRGIS/actions/workflows/rhub.yaml/badge.svg)](https://github.com/ccamlr/CCAMLRGIS/actions/workflows/rhub.yaml)
[![All downloads](https://cranlogs.r-pkg.org/badges/grand-total/CCAMLRGIS)](https://cran.r-project.org/package=CCAMLRGIS)
[![Monthly downloads](https://cranlogs.r-pkg.org/badges/CCAMLRGIS)](https://cran.r-project.org/package=CCAMLRGIS)
<!-- badges: end -->
This package was developed to simplify the production of maps in the CAMLR Convention Area. It provides two main categories of functions: load functions and create functions. Load functions are used to import spatial layers from the online [CCAMLR GIS](https://gis.ccamlr.org/) such as the ASD boundaries. Create functions are used to create layers from user data such as polygons and grids.
## Installation
You can install the CCAMLRGIS R package from CRAN with:
```{r, eval = F}
install.packages("CCAMLRGIS")
```
## Documentation
```{r, echo = FALSE, message = FALSE}
set.seed(1234)
```
<center>
### A package to load and create spatial data, including layers and tools that are relevant to CCAMLR activities.
</center>
------------------------------------------------------------------------
<center>
### Table of contents
</center>
------------------------------------------------------------------------
1. [Basemaps](#1-basemaps)
2. [Create functions](#2-create-functions)
- 2.1. [Points](#create-points), [lines](#create-lines), [polygons](#create-polygons) and [grids](#create-grids)
- 2.2. [Create Stations](#22-create-stations)
- 2.3. [Create Pies](#23-create-pies)
- 2.4. [Create Arrow](#24-create-arrow)
- 2.5. [Create Hashes](#25-create-hashes)
- 2.6. [Create Ellipse](#26-create-ellipse)
- 2.7. [Create Circular Arrow](#27-create-circular-arrow)
3. [Load functions](#3-load-functions)
- 3.1. Online use
- 3.2. Offline use
4. Other functions
- 4.1. [get_depths](#41-get_depths)
- 4.2. [seabed_area](#42-seabed_area)
- 4.3. [assign_areas](#43-assign_areas)
- 4.4. [project_data](#44-project_data)
- 4.5. [get_C\_intersection](#45-get_c_intersection)
- 4.6. [get_iso_polys](#46-get_iso_polys)
- 4.7. [Rotate_obj](#47-rotate_obj)
5. Adding colors, legends and labels
- 5.1. [Bathymetry colors](#51-bathymetry-colors)
- 5.2. [Adding colors to data](#52-adding-colors-to-data)
- 5.3. [Adding legends](#53-adding-legends)
- 5.4. [Adding labels](#54-adding-labels)
- 5.5. [Using sf](#55-using-sf)
------------------------------------------------------------------------
## Introduction
First, install the package by typing:
```{r eval=F}
install.packages("CCAMLRGIS")
```
Then, load the package by typing:
```{r eval=T, message = FALSE}
library(CCAMLRGIS)
```
In order to plot bathymetry data, you will also need to load [terra](https://CRAN.R-project.org/package=terra):
```{r eval=T, message = FALSE}
library(terra)
```
[geospatial rules]:https://github.com/ccamlr/geospatial_operations#1-geospatial-rules
All spatial manipulations are made using the South Pole Lambert Azimuthal Equal Area projection (type ?CCAMLRp for more details), and follow CCAMLR's [geospatial rules].
```{r message=F,eval=T}
#Map with axes, to illustrate projection
png(filename='ReadMeFigs/ReadMe_Fig0.png',width=2000,height=2000,res=300)
#Set the figure margins as c(bottom, left, top, right)
par(mai=c(1.2,1.3,0.2,0.2),xpd=TRUE)
#plot entire Coastline
plot(st_geometry(Coast[Coast$ID=='All',]),col='grey',lwd=0.1)
#Add reference grid
add_RefGrid(bb=st_bbox(Coast[Coast$ID=='All',]),ResLat=10,ResLon=20,LabLon=-40,fontsize=0.8,lwd=0.5)
#add axes and labels
axis(1,pos=0,at=seq(-4000000,4000000,by=1000000),tcl=-0.15,labels=F,lwd=0.8,lwd.ticks=0.8,col='blue')
axis(2,pos=0,at=seq(-4000000,4000000,by=1000000),tcl=-0.15,labels=F,lwd=0.8,lwd.ticks=0.8,col='blue')
text(seq(1000000,4000000,by=1000000),0,seq(1,4,by=1),cex=0.75,col='blue',adj=c(0.5,1.75))
text(seq(-4000000,-1000000,by=1000000),0,seq(-4,-1,by=1),cex=0.75,col='blue',adj=c(0.5,1.75))
text(0,seq(1000000,4000000,by=1000000),seq(1,4,by=1),cex=0.75,col='blue',adj=c(1.75,0.5))
text(0,seq(-4000000,-1000000,by=1000000),seq(-4,-1,by=1),cex=0.75,col='blue',adj=c(1.75,0.5))
text(0,0,0,cex=0.75,col='blue',adj=c(-0.5,-0.5))
text(4400000,0,expression('x ('*10^6~'m)'),cex=0.75,col='blue')
text(0,4200000,expression('y ('*10^6~'m)'),cex=0.75,col='blue')
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig0.png")
```
<center>
#### The South Pole Lambert Azimuthal Equal Area projection converts Latitudes and Longitudes into locations on a disk with x/y axes and units of meters. The South Pole is at x=0m ; y=0m. The tip of the Peninsula, for example, is around x=-2,500,000m ; y=2,000,000m.
</center>
<br>
## 1. Basemaps
[here]:https://github.com/ccamlr/CCAMLRGIS/blob/master/Basemaps/Basemaps.md
Additional basemaps are available [here].
#### Bathymetry:
Prior to detailing the package's capabilities, a set of basic commands are shown here to display a few core mapping elements. All scripts use the low-resolution bathymetry raster included in the package ('SmallBathy'). In order to obtain higher resolution bathymetry data, use the *Load_Bathy()* function:
```{r message=F,eval=T}
#Load_Bathy() example:
Bathy=load_Bathy(LocalFile = FALSE,Res=5000)
png(filename='ReadMeFigs/ReadMe_Fig1.1.png',width=2000,height=2000,res=300,bg="transparent")
plot(Bathy, breaks=Depth_cuts,col=Depth_cols,axes=FALSE,legend=FALSE,mar=c(0,0,0,0))
dev.off()
#Please refer to ?load_Bathy for more details, including how to save the bathymetry data so that you
#do not have to re-download it every time you need it.
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig1.1.png")
```
```{r, echo = FALSE, message = FALSE}
#Delete GEBCO file
Fs=list.files()
Fs=Fs[grep("GEBCO",Fs)]
a=file.remove(Fs)
```
<br>
#### Statistical Areas, Subareas and Divisions (ASDs):
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig1.2.png',width=2200,height=1800,res=300)
#Load ASDs and EEZs
ASDs=load_ASDs()
EEZs=load_EEZs()
#Plot the bathymetry
plot(SmallBathy(),breaks=Depth_cuts,col=Depth_cols,legend=F,axes=F,box=F,mar=c(0,0,0,5))
#Add reference grid
add_RefGrid(bb=st_bbox(SmallBathy()),ResLat=10,ResLon=20,LabLon=0,fontsize=0.75,lwd=0.75,offset = 4)
#Add color scale
add_Cscale(height=70,fontsize=0.75,offset=-50,width=14,maxVal=-1,lwd=0.5)
#Add ASD and EEZ boundaries
plot(st_geometry(ASDs),add=T,lwd=0.75,border='red',xpd=T)
plot(st_geometry(EEZs),add=T,lwd=0.75,border='red',xpd=T)
#Add coastline (for all ASDs)
plot(st_geometry(Coast[Coast$ID=='All',]),col='grey',lwd=0.01,add=T,xpd=T)
#Add ASD labels
add_labels(mode='auto',layer='ASDs',fontsize=0.6,col='red')
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig1.2.png")
```
<br>
#### Local map (*e.g.*, Subarea 48.6):
Additional local maps are available [here].
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig1.3.png',width=2000,height=1400,res=300)
#Load ASDs
ASDs=load_ASDs()
#Sub-sample ASDs to only keep Subarea 48.6
S486=ASDs[ASDs$GAR_Short_Label=='486',]
#Crop bathymetry to match the extent of S486
B486=crop(SmallBathy(),ext(S486))
#Plot the bathymetry
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=F,axes=F,mar=c(1.0,2,1.0,6))
#Add color scale
add_Cscale(height=80,fontsize=0.7,offset=250,width=15,lwd=0.5,maxVal=-1)
#Add coastline (for Subarea 48.6 only)
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T,xpd=T)
#Add reference grid
add_RefGrid(bb=st_bbox(B486),ResLat=5,ResLon=10,fontsize=0.75,lwd=0.75,offset = 50000)
#Add Subarea 48.6 boundaries
plot(st_geometry(S486),add=T,lwd=1,border='red',xpd=T)
#Add a -2000m contour
contour(B486,levels=-2000,add=T,lwd=0.5,labcex=0.3,xpd=T)
#Add single label at the centre of the polygon (see ?Labels)
text(Labels$x[Labels$t=='48.6'],Labels$y[Labels$t=='48.6'],labels='48.6',col='red',cex=1.5)
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig1.3.png")
```
## 2. Create functions
### 2.1. Points, lines, polygons and grids
These functions are used to transform user data into spatial layers with the appropriate projection. User data should be provided as a dataframe containing Latitudes and Longitudes in decimal degrees. Depending on the function used, some other variables may be required (see help).
#### Create points:
For details, type:
```{r eval=F}
?create_Points
```
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.1.png',width=2000,height=1400,res=300)
#Prepare layout for 4 sub-plots
par(mfrow=c(2,2),mai=c(0.01,0.01,0.2,0.01))
#Example 1: Simple points with labels
MyPoints=create_Points(PointData)
plot(st_geometry(MyPoints),main='Example 1',cex.main=0.75,cex=0.5,lwd=0.5)
text(MyPoints$x,MyPoints$y,MyPoints$name,adj=c(0.5,-0.5),xpd=T,cex=0.75)
box()
#Example 2: Simple points with labels, highlighting one group of points with the same name
MyPoints=create_Points(PointData)
plot(st_geometry(MyPoints),main='Example 2',cex.main=0.75,cex=0.5,lwd=0.5)
text(MyPoints$x,MyPoints$y,MyPoints$name,adj=c(0.5,-0.5),xpd=T,cex=0.75)
plot(st_geometry(MyPoints[MyPoints$name=='four',]),bg='red',pch=21,cex=1,add=T)
box()
#Example 3: Buffered points with radius proportional to catch
MyPoints=create_Points(PointData,Buffer=1*PointData$Catch)
plot(st_geometry(MyPoints),col='green',main='Example 3',cex.main=0.75,cex=0.5,lwd=0.5)
text(MyPoints$x,MyPoints$y,MyPoints$name,adj=c(0.5,0.5),xpd=T,cex=0.75)
box()
#Example 4: Buffered points with radius proportional to catch and clipped to the Coast
MyPoints=create_Points(PointData,Buffer=2*PointData$Catch,Clip=T)
plot(st_geometry(MyPoints),col='cyan',main='Example 4',cex.main=0.75,cex=0.75,lwd=0.5)
plot(st_geometry(Coast[Coast$ID=='All',]),add=T,col='grey',lwd=0.5)
box()
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.1.png")
```
<br>
#### Create lines:
For details, type:
```{r eval=F}
?create_Lines
```
```{r eval=F}
#If your data contains line end locations in separate columns, you may reformat it as follows:
#Original data:
MyData=data.frame(
Line=c(1,2),
Lat_Start=c(-60,-65),
Lon_Start=c(-10,5),
Lat_End=c(-61,-66),
Lon_End=c(-2,2)
)
#Reformat data to use as input in create_Lines as:
Input=data.frame(
Line=c(MyData$Line,MyData$Line),
Lat=c(MyData$Lat_Start,MyData$Lat_End),
Lon=c(MyData$Lon_Start,MyData$Lon_End)
)
```
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.2.png',width=2000,height=1000,res=300)
#Prepare layout for 3 sub-plots
par(mai=c(0.01,0.01,0.2,0.01),mfrow=c(1,3))
#Example 1: Simple and non-densified lines
MyLines=create_Lines(LineData)
plot(st_geometry(MyLines),col=rainbow(nrow(MyLines)),main='Example 1',cex.main=0.75,lwd=2)
box()
#Example 2: Simple and densified lines (note the curvature of the lines)
MyLines=create_Lines(LineData,Densify=T)
plot(st_geometry(MyLines),col=rainbow(nrow(MyLines)),main='Example 2',cex.main=0.75,lwd=2)
box()
#Example 3: Densified, buffered and clipped lines
MyLines=create_Lines(LineData,Densify=T,Buffer=c(10,40,50,80,100),Clip=T)
plot(st_geometry(MyLines[5:1,]),col=rainbow(nrow(MyLines)),main='Example 3',cex.main=0.75,lwd=1)
plot(Coast[Coast$ID=='All',],col='grey',add=T,lwd=0.5)
box()
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.2.png")
```
<br>
Adding a buffer with the argument *SeparateBuf* set to FALSE results in a single polygon which may be viewed as a footprint:
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.3.png',width=2000,height=1000,res=300)
#Set the figure margins as c(bottom, left, top, right)
par(mai=c(0.01,0.01,0.01,0.01))
#Buffer merged lines
MyLines=create_Lines(LineData,Buffer=10,SeparateBuf=F)
#The resulting polygon has an area of:
MyLines$Buffered_AreaKm2
plot(st_geometry(MyLines),col='green',lwd=1)
box()
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.3.png")
```
<br>
#### Create polygons:
For details, type:
```{r eval=F}
?create_Polys
```
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.4.png',width=2000,height=1200,res=300)
#Prepare layout for 3 sub-plots
par(mfrow=c(1,3),mai=c(0.01,0.01,0.2,0.01))
#Example 1: Simple and non-densified polygons
MyPolys=create_Polys(PolyData,Densify=F)
plot(st_geometry(MyPolys),col='blue',main='Example 1',cex.main=0.75,lwd=0.5)
text(MyPolys$Labx,MyPolys$Laby,MyPolys$ID,col='white',cex=0.75)
box()
#Example 2: Simple and densified polygons (note the curvature of lines)
MyPolys=create_Polys(PolyData)
plot(st_geometry(MyPolys),col='red',main='Example 2',cex.main=0.75,lwd=0.5)
text(MyPolys$Labx,MyPolys$Laby,MyPolys$ID,col='white',cex=0.75)
box()
#Example 3: Buffered and clipped polygons
MyPolysBefore=create_Polys(PolyData,Buffer=c(10,-15,120))
MyPolysAfter=create_Polys(PolyData,Buffer=c(10,-15,120),Clip=T)
plot(st_geometry(MyPolysBefore),col='green',main='Example 3',cex.main=0.75,lwd=0.5)
plot(st_geometry(Coast[Coast$ID=='All',]),add=T,lwd=0.5)
plot(st_geometry(MyPolysAfter),col='orange',add=T,lwd=0.5)
text(MyPolysAfter$Labx,MyPolysAfter$Laby,MyPolysAfter$ID,col='white',cex=0.75)
box()
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.4.png")
```
<br>
```{r message=F,eval=T}
#Convention area
#The locations of vertices are given clockwise, starting from the northwest corner of 48.3
CA=data.frame(
Name="CA",
Lat=c(-50,-50,-45,-45,-55,-55,-60,-60),
Lon=c(-50,30,30,80,80,150,150,-50)
)
png(filename='ReadMeFigs/ReadMe_Fig2.5.png',width=2000,height=1200,res=300)
#Prepare layout for 2 sub-plots
par(mfrow=c(1,2),mai=c(0,0,0.2,0))
#Example 4: Convention area contour
MyPoly=create_Polys(CA)
plot(st_geometry(MyPoly),col='blue',border='green',main='Example 4',cex.main=0.75,lwd=2)
box()
#Example 5: Convention area contour, coastline clipped
MyPoly=create_Polys(CA,Clip = TRUE)
plot(st_geometry(MyPoly),col='blue',border='green',main='Example 5',cex.main=0.75,lwd=2)
box()
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.5.png")
```
<br>
#### Create grids:
[tutorial]:https://github.com/ccamlr/CCAMLRGIS/blob/master/Advanced_Grids/Advanced_Grids.md
An advanced demo is given in this [tutorial].
For details, type:
```{r eval=F}
?create_PolyGrids
```
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.6.png',width=2000,height=800,res=300)
#Prepare layout for 3 sub-plots
par(mfrow=c(1,3),mai=c(0.01,0.01,0.2,0.01))
#Example 1: Simple grid, using automatic colors
MyGrid=create_PolyGrids(GridData,dlon=2,dlat=1)
plot(st_geometry(MyGrid),col=MyGrid$Col_Catch_sum,main='Example 1',cex.main=0.75,lwd=0.1)
box()
#Example 2: Equal area grid, using automatic colors
MyGrid=create_PolyGrids(GridData,Area=10000)
plot(st_geometry(MyGrid),col=MyGrid$Col_Catch_sum,main='Example 2',cex.main=0.75,lwd=0.1)
box()
#Example 3: Equal area grid, using custom cuts and colors
MyGrid=create_PolyGrids(GridData,Area=10000,cuts=c(0,50,100,500,2000,3500),cols=c('blue','red'))
plot(st_geometry(MyGrid),col=MyGrid$Col_Catch_sum,main='Example 3',cex.main=0.75,lwd=0.1)
box()
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.6.png")
```
<br>
Customizing a grid and adding a color scale:
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.7.png',width=2000,height=1500,res=300)
#Prepare layout for 2 sub-plots
par(mfrow=c(2,1),mai=c(0.2,0.05,0.1,1.3))
#Step 1: Generate your grid
MyGrid=create_PolyGrids(GridData,Area=10000)
#Step 2: Inspect your gridded data (e.g. sum of Catch) to determine whether irregular cuts are required
hist(MyGrid$Catch_sum,100,cex=0.75,main='Frequency distribution of data',
cex.main=0.5,col='grey',axes=F)
axis(1,pos=0,tcl=-0.15,lwd=0.8,lwd.ticks=0.8,labels=F)
text(seq(0,2500,by=500),-1.5,seq(0,2500,by=500),cex=0.75,xpd=T)
#In this case (heterogeneously distributed data) irregular cuts would be preferable
#Such as:
MyCuts=c(0,50,100,500,2000,2500)
abline(v=MyCuts,col='green',lwd=1,lty=2) #Add classes to histogram as green dashed lines
#Step 3: Generate colors according to the desired classes (MyCuts)
Gridcol=add_col(MyGrid$Catch_sum,cuts=MyCuts,cols=c('yellow','purple'))
#Step 4: Plot result and add color scale
#Use the colors generated by add_col
plot(st_geometry(MyGrid),col=Gridcol$varcol,lwd=0.1)
#Add color scale using cuts and cols generated by add_col
add_Cscale(title='Sum of Catch (t)',cuts=Gridcol$cuts,cols=Gridcol$cols,width=18,
fontsize=0.6,lwd=0.5,height = 100)
box()
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.7.png")
```
<br>
### 2.2. Create Stations
This function was designed to create random point locations inside a polygon and within bathymetry strata constraints. A distance constraint between stations may also be used if desired. The examples below use the 'SmallBathy' data for illustrative purposes; users should use a higher resolution bathymetry dataset instead, as obtained via the *load_Bathy()* function.
For details, type:
```{r eval=F}
?create_Stations
```
First, create a polygon within which stations will be created:
```{r message=F,eval=T}
#Create polygons
MyPoly=create_Polys(
data.frame(Name="mypol",
Latitude=c(-75,-75,-70,-70),
Longitude=c(-170,-180,-180,-170))
,Densify=T)
png(filename='ReadMeFigs/ReadMe_Fig2.8.png',width=2000,height=1000,res=300,bg="transparent")
par(mai=c(0,0,0,0))
plot(st_geometry(Coast[Coast$ID=='88.1',]),col='grey')
plot(st_geometry(MyPoly),col='green',add=T)
text(MyPoly$Labx,MyPoly$Laby,MyPoly$ID)
box()
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.8.png")
```
<br>
Example 1. Set numbers of stations, no distance constraint:
```{r message=F,eval=T}
#optional: crop your bathymetry raster to match the extent of your polygon
BathyCroped=crop(SmallBathy(),ext(MyPoly))
#Create stations
MyStations=create_Stations(MyPoly,BathyCroped,Depths=c(-2000,-1500,-1000,-550),N=c(20,15,10))
png(filename='ReadMeFigs/ReadMe_Fig2.9.png',width=2000,height=1500,res=300)
#add custom colors to the bathymetry to indicate the strata of interest
MyCols=add_col(var=c(-10000,10000),cuts=c(-2000,-1500,-1000,-550),cols=c('blue','cyan'))
plot(BathyCroped,breaks=MyCols$cuts,col=MyCols$cols,legend=F,axes=F,main="Example 1")
add_Cscale(height=90,fontsize=0.75,width=16,lwd=0.5,offset=-130,cuts=MyCols$cuts,cols=MyCols$cols)
plot(st_geometry(MyPoly),add=T,border='red',lwd=2,xpd=T)
plot(st_geometry(MyStations),add=T,col='orange',cex=0.75,lwd=1.5,pch=3)
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.9.png")
```
<br>
Example 2. Set numbers of stations, with distance constraint:
```{r message=F,eval=T}
#Create Stations
MyStations=create_Stations(MyPoly,BathyCroped,
Depths=c(-2000,-1500,-1000,-550),N=c(20,15,10),dist=10)
png(filename='ReadMeFigs/ReadMe_Fig2.10.png',width=2000,height=1500,res=300)
#add custom colors to the bathymetry to indicate the strata of interest
MyCols=add_col(var=c(-10000,10000),cuts=c(-2000,-1500,-1000,-550),cols=c('blue','cyan'))
plot(BathyCroped,breaks=MyCols$cuts,col=MyCols$cols,legend=F,axes=F,main="Example 2")
add_Cscale(height=90,fontsize=0.75,width=16,lwd=0.5,offset=-130,cuts=MyCols$cuts,cols=MyCols$cols)
plot(st_geometry(MyPoly),add=T,border='red',lwd=2,xpd=T)
plot(st_geometry(MyStations[MyStations$Stratum=='1000-550',]),pch=21,bg='yellow',add=T,cex=0.75,lwd=0.1)
plot(st_geometry(MyStations[MyStations$Stratum=='1500-1000',]),pch=21,bg='orange',add=T,cex=0.75,lwd=0.1)
plot(st_geometry(MyStations[MyStations$Stratum=='2000-1500',]),pch=21,bg='red',add=T,cex=0.75,lwd=0.1)
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.10.png")
```
<br>
Example 3. Automatic numbers of stations, with distance constraint:
```{r message=F,eval=T}
#Create Stations
MyStations=create_Stations(MyPoly,BathyCroped,Depths=c(-2000,-1500,-1000,-550),Nauto=30,dist=10)
png(filename='ReadMeFigs/ReadMe_Fig2.11.png',width=2000,height=1500,res=300)
#add custom colors to the bathymetry to indicate the strata of interest
MyCols=add_col(var=c(-10000,10000),cuts=c(-2000,-1500,-1000,-550),cols=c('blue','cyan'))
plot(BathyCroped,breaks=MyCols$cuts,col=MyCols$cols,legend=F,axes=F,main="Example 3")
add_Cscale(height=90,fontsize=0.75,width=16,lwd=0.5,offset=-130,cuts=MyCols$cuts,cols=MyCols$cols)
plot(st_geometry(MyPoly),add=T,border='red',lwd=2,xpd=T)
plot(st_geometry(MyStations[MyStations$Stratum=='1000-550',]),pch=21,bg='yellow',add=T,cex=0.75,lwd=0.1)
plot(st_geometry(MyStations[MyStations$Stratum=='1500-1000',]),pch=21,bg='orange',add=T,cex=0.75,lwd=0.1)
plot(st_geometry(MyStations[MyStations$Stratum=='2000-1500',]),pch=21,bg='red',add=T,cex=0.75,lwd=0.1)
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.11.png")
```
<br>
### 2.3. Create pies
The function *create_Pies()* generates pie charts that can be overlaid on maps. The *Input* data must be a dataframe with, at least, columns for latitude, longitude, class and value. For each location, a pie is created with pie pieces for each class, and the size of each pie piece depends on the proportion of each class (the value of each class divided by the sum of values). Optionally, the area of each pie can be proportional to a chosen variable (if that variable is different than the value mentioned above, the *Input* data must have a fifth column and that variable must be unique to each location). If the *Input* data contains locations that are too close together, the data can be gridded by setting *GridKm* (see Examples 6-8). Once pie charts have been created, the function *add_PieLegend()* may be used to add a legend to the figure.
For details, type:
```{r eval=F}
?create_Pies
?add_PieLegend
#The examples below use the following example datasets:
View(PieData)
View(PieData2)
```
<br>
Example 1. Pies of constant size, all classes displayed:
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.12.png',width=2000,height=1400,res=300,bg="transparent")
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(5.5,0,0.1,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
MyPies=create_Pies(Input=PieData,
NamesIn=c("Lat","Lon","Sp","N"),
Size=50
)
#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=-0.1,PosY=-1.6,Boxexp=c(0.5,0.45,0.12,0.45),
PieTitle="Species")
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.12.png")
```
<br>
Example 2. Pies of constant size, selected classes displayed:
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.13.png',width=2000,height=1400,res=300,bg="transparent")
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(5.5,0,0.1,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
MyPies=create_Pies(Input=PieData,
NamesIn=c("Lat","Lon","Sp","N"),
Size=50,
Classes=c("TOP","TOA","ANI")
)
#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=-0.1,PosY=-1.6,Boxexp=c(0.6,0.6,0.12,0.55),
PieTitle="Selected species")
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.13.png")
```
<br>
Example 3. Pies of constant size, proportions below 25% are grouped in a 'Other' class (*N.B.*: unlike Example 2, the 'Other' class may contain classes that are displayed in the legend. Please compare Example 1 and Example 3):
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.14.png',width=2000,height=1400,res=300,bg="transparent")
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(5.5,0,0.1,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
MyPies=create_Pies(Input=PieData,
NamesIn=c("Lat","Lon","Sp","N"),
Size=50,
Other=25
)
#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=-0.1,PosY=-1.6,Boxexp=c(0.55,0.55,0.12,0.45),
PieTitle="Other (%) class")
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.14.png")
```
<br>
Example 4. Pies of variable size (here, their area is proportional to 'Catch'), all classes displayed, horizontal legend:
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.15.png',width=2000,height=1400,res=300,bg="transparent")
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(5.5,0,0.1,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
MyPies=create_Pies(Input=PieData,
NamesIn=c("Lat","Lon","Sp","N"),
Size=18,
SizeVar="Catch"
)
#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=-0.1,PosY=-1.6,Boxexp=c(0.16,0.1,0.1,0.4),
PieTitle="Species",SizeTitle="Catch (t.)")
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.15.png")
```
<br>
Example 5. Pies of variable size (here, their area is proportional to 'Catch'), all classes displayed, vertical legend:
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.16.png',width=2000,height=1200,res=300,bg="transparent")
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(0,0.2,0,8.5))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
MyPies=create_Pies(Input=PieData,
NamesIn=c("Lat","Lon","Sp","N"),
Size=18,
SizeVar="Catch"
)
#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=2.32,PosY=0.1,Boxexp=c(0.35,0.32,0.02,0.15),
PieTitle="Species",SizeTitle="Catch (t.)",Horiz=FALSE,LegSp=0.6)
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.16.png")
```
<br>
Example 6. Pies of constant size, all classes displayed. Too many pies (see next example for solution):
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.17.png',width=2000,height=1400,res=300,bg="transparent")
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(5.5,0,0.1,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
MyPies=create_Pies(Input=PieData2,
NamesIn=c("Lat","Lon","Sp","N"),
Size=5
)
#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=0.4,PosY=-1.5,Boxexp=c(0.6,0.5,0.14,0.5),
PieTitle="Species")
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.17.png")
```
<br>
Example 7. Pies of constant size, all classes displayed. Gridded locations (in which case numerical variables in the *Input* are summed for each grid point):
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.18.png',width=2000,height=1400,res=300,bg="transparent")
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(4.5,0,0.1,0))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
MyPies=create_Pies(Input=PieData2,
NamesIn=c("Lat","Lon","Sp","N"),
Size=5,
GridKm=250
)
#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=0.4,PosY=-1.3,Boxexp=c(0.5,0.45,0.12,0.45),
PieTitle="Species")
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.18.png")
```
<br>
Example 8. Pies of variable size (here, their area is proportional to 'Catch'), all classes displayed, vertical legend, gridded locations (in which case numerical variables in the *Input* are summed for each grid point):
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.19.png',width=2000,height=1200,res=300,bg="transparent")
#Plot the bathymetry (See section 'Local map' where B486 was created)
plot(B486,breaks=Depth_cuts,col=Depth_cols,legend=FALSE,axes=FALSE,mar=c(0,0.1,0,8.5))
#Add coastline
plot(Coast[Coast$ID=='48.6',],col='grey',lwd=0.01,add=T)
#Create pies
MyPies=create_Pies(Input=PieData2,
NamesIn=c("Lat","Lon","Sp","N"),
Size=3,
GridKm=250,
SizeVar='Catch'
)
#Plot Pies
plot(st_geometry(MyPies),col=MyPies$col,add=TRUE)
#Add Pies legend
add_PieLegend(Pies=MyPies,PosX=2.8,PosY=0.15,Boxexp=c(0.38,0.32,0.08,0.18),
PieTitle="Species",Horiz=FALSE,SizeTitle="Catch (t.)",
SizeTitleVadj=0.8,nSizes=2)
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.19.png")
```
<br>
### 2.4. Create Arrow
This function creates an arrow, which can be curved and/or segmented. Input is a dataframe with columns Latitude, Longitude, Weight (optional). First row is start, last row is end (where the arrow will point to), and intermediate rows are points towards which the arrow's path will bend. A weight can be added to the intermediate points to make the arrow's path bend more towards them. The arrow's path is a curve along *Np* points, if it appears too segmented, increase *Np*. The arrow's path width is controlled by *Pwidth*. The arrow's head length and width are controlled by *Hlength* and *Hwidth* respectively. Two types of arrows (*Atype*) can be created: 'normal' or 'dashed'. A normal arrow is a single polygon, with a single color (set by *Acol*) and transparency (set by *Atrans*). A dashed arrow is a series of polygons which can be colored separately by setting two or more values as *Acol=c("color start","color end")* and two or more transparency values as *Atrans=c("transparency start","transparency end")*. The length of dashes is controlled by *dlength*.
For details, type:
```{r eval=F}
?create_Arrow
```
Examples 1-4:
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.20.png',width=2000,height=1400,res=300)
ASDs=load_ASDs()
ASDs=ASDs[ASDs$GAR_Short_Label%in%c("481","482","483"),]
# Example 1: straight green arrow.
myInput=data.frame(lat=c(-61,-52),
lon=c(-60,-40))
Arrow=create_Arrow(Input=myInput)
par(mai=c(0,0,0.5,0),mfrow=c(2,2))
plot(st_geometry(ASDs),main="Example 1")
plot(st_geometry(Coast[Coast$ID%in%c("48.1","48.2","48.3"),]),col="grey",add=TRUE)
plot(st_geometry(Arrow),col=Arrow$col,add=TRUE)
# Example 2: blue arrow with one bend.
myInput=data.frame(lat=c(-61,-65,-52),
lon=c(-60,-45,-40))
Arrow=create_Arrow(Input=myInput,Acol="lightblue")
plot(st_geometry(ASDs),main="Example 2")
plot(st_geometry(Coast[Coast$ID%in%c("48.1","48.2","48.3"),]),col="grey",add=TRUE)
plot(st_geometry(Arrow),col=Arrow$col,add=TRUE)
#Example 3: blue arrow with two bends
myInput=data.frame(lat=c(-61,-60,-65,-52),
lon=c(-60,-50,-45,-40))
Arrow=create_Arrow(Input=myInput,Acol="lightblue")
plot(st_geometry(ASDs),main="Example 3")
plot(st_geometry(Coast[Coast$ID%in%c("48.1","48.2","48.3"),]),col="grey",add=TRUE)
plot(st_geometry(Arrow),col=Arrow$col,add=TRUE)
#Example 4: blue arrow with two bends, with more weight on the second bend and a big head
myInput=data.frame(lat=c(-61,-60,-65,-52),
lon=c(-60,-50,-45,-40),
w=c(1,1,2,1))
Arrow=create_Arrow(Input=myInput,Acol="lightblue",Hlength=20,Hwidth=20)
plot(st_geometry(ASDs),main="Example 4")
plot(st_geometry(Coast[Coast$ID%in%c("48.1","48.2","48.3"),]),col="grey",add=TRUE)
plot(st_geometry(Arrow),col=Arrow$col,add=TRUE)
dev.off()
```
```{r fig.align="center",out.width="100%",message=F,dpi=300,echo=F}
include_graphics("ReadMeFigs/ReadMe_Fig2.20.png")
```
<br>
Examples 5-8:
```{r message=F,eval=T}
png(filename='ReadMeFigs/ReadMe_Fig2.21.png',width=2000,height=1400,res=300)
#Example 5: Dashed arrow, small dashes
myInput=data.frame(lat=c(-61,-60,-65,-52),
lon=c(-60,-50,-45,-40),
w=c(1,1,2,1))