/
vignette_map.Rmd
1900 lines (1486 loc) · 66.5 KB
/
vignette_map.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: "User Guide"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{User Guide}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
resourse_files:
- vignettes/vignetteFigs
---
<!-------------------------->
<!-------------------------->
<!-- HTML styles items -->
<!-------------------------->
<!-------------------------->
<style>
.button {
background-color: #555555;
border-radius: 8px;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button:hover {
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
background-color: #555555;
color: gold;
}
</style>
<!-- ------------------------>
<!-- ------------------------>
# Install
<!-- ------------------------>
<!-- ------------------------>
<p align="center"> <img src="vignetteFigs/divider.png"></p>
1. Download and install:
- R (https://www.r-project.org/)
- R studio (https://www.rstudio.com/) (Optional)
2. In R or R studio:
```r
install.packages("devtools")
devtools::install_github("JGCRI/rmap")
```
Additional steps for UBUNTU from a terminal
```
sudo add-apt-repository ppa:ubuntugis/ppa
sudo apt-get update
sudo apt-get install -y libcurl4-openssl-dev libssl-dev libxml2-dev libudunits2-dev libgdal-dev libgeos-dev libproj-dev libavfilter-dev libmagick++-dev
```
Additional steps for MACOSX from a terminal
```
brew install pkg-config
brew install gdal
brew install geos
brew install imagemagick@6
```
<!-------------------------->
<!-------------------------->
# Input Structure {#inputs}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
The main input is the `data` argument in the `map()` function. This will be an R table which can be created within R or read in from a csv file as shown below. As shown later if a shape file (`SpatialPolygonDataFrame`) is provided as the data argument (see Section [Built-in Maps](#maps)) then the `map()` function will produce a map without any value data.
<p align="center"> <img src="vignetteFigs/mapsProcess_inputs.PNG"></p>
<!-------------------------->
<!-------------------------->
# Custom Columns {#customcolumns}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Users can also assign other column names to `x` (the time dimension), `class`, `subRegion`, `scenario` and `value` if desired using the `x`, `class`, `subRegion`, `scenario` and `value` arguments as follows:
```{r, results = "hide", eval=FALSE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
data = data.frame(my_column_1 = c("CA","FL","ID","CA","FL","ID","CA","FL","ID","CA","FL","ID"),
my_column_2 = c(5,10,15,34,22,77,15,110,115,134,122,177),
my_column_3 = c(2010,2010,2010,2020,2020,2020,2010,2010,2010,2020,2020,2020),
my_column_4 = c("class1","class1","class1","class1","class1","class1",
"class2","class2","class2","class2","class2","class2"),
my_column_5 = c("my_scenario","my_scenario","my_scenario","my_scenario","my_scenario","my_scenario",
"my_scenario","my_scenario","my_scenario","my_scenario","my_scenario","my_scenario"))
rmap::map(data,
subRegion = "my_column_1",
value = "my_column_2",
x = "my_column_3",
class = "my_column_4",
scenario = "my_column_5")
```
<!-------------------------->
<!-------------------------->
# Cleaning Data {#clean}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Users will need to check that the names of the regions in their data tables match those of the `subRegion` column of the [built-in maps](#maps) or a [custom shapefile](#maps) if they are using one. Sometimes user data may have a few names that have different spelling or accents and these will be flagged when rmap plots the maps. Users can then go back to their original data and fix those regions as shown in the example below.
```{r, results = 'hide', eval=T, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap); library(dplyr)
# Create data table with a misspelled subRegion
data = data.frame(subRegion = c("Italy","Spain","Greice"),
value = c(10,15,34))
# Plot data
rmap::map(data,labels = T, title = "Misspelled subRegion 'Greice'")
# Will return: 'Warning: subRegions in data not present in shapefile are: Greice'
# As well as the built-in map used: 'Using map: mapCountries'
# Check the subRegions in the map used
rmap::mapCountries$subRegion%>%unique()%>%sort()
# Fix the subRegion that was misspelled
data = data.frame(subRegion = c("Italy","Spain","Greece"),
value = c(10,15,34))
# Re-plot cleaned data
rmap::map(data,labels = T, title = "Correctly spelled subRegion 'Greece'")
```
<!-------------------------->
<!-------------------------->
# Output Formats {#outputs}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
The output of the `map()` function is a named list with all maps and animations created in the function. The elements of the list can be called individually and re-used as layers in new maps as decribed in the [Layers](#layers) section. Since the output maps are `ggplot` elements all the features of the map can easily be modified using the taditional `ggplot2` theme options. Examples are provided in the [Themes](#themes) section.
```{r, results = "hide", eval=FALSE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
my_map_list <- rmap::map(mapUS49) # my_map_list will contain a list of the maps produced. In this case a single map.
my_map <- my_map_list[[1]] # my_map will be the first map in the list of maps produced
my_map_names <- names(my_map_list) # Will give you a list of the maps produced.
rmap::map(mapUS49) # Will print out the maps to the console as well save the maps as well as the related data tables to a default directory.
rmap::map(mapUS49, show = F) # will return a map and save to disk without printing to console.
rmap::map(mapUS49, folder = "myOutputs") # Will save outputs to a directory called myOutputs
rmap::map(mapUS49, save = F) # Will simply print the maps to the console and not save the maps to file.
```
```{r, results="hide", eval=TRUE, echo=F, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapUS49, save = F)
```
<!-------------------------->
<!-------------------------->
# Built-in Maps {#maps}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
rmap comes with a set of preloaded maps. A full list of maps is available in the reference list of [maps](https://jgcri.github.io/rmap/reference/index.html). The pre-loaded maps are all `sf` objects. Each map comes with a data column named **subRegion**. For each map the data contained in the shape and the map itself can be viewed as follows:
```{r, results = 'hide', eval=FALSE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
head(mapUS49) # To view data for chosen map
plot(mapUS49[,"subRegion"]) # plot regular sf object
# Plot Using rmap
rmap::map(mapUS49)
rmap::map(mapUS52)
rmap::map(mapUS52Compact)
rmap::map(mapUS49County)
rmap::map(mapUS52County)
rmap::map(mapUS52CountyCompact)
rmap::map(mapCountries)
rmap::map(mapCountriesUS52)
rmap::map(mapGCAMReg32)
rmap::map(mapGCAMReg32US52)
rmap::map(mapGCAMBasins)
rmap::map(mapGCAMBasinsUS49)
rmap::map(mapGCAMBasinsUS52)
rmap::map(mapGCAMLand) # Intersection of GCAM 32 regions and GCAM Basins
rmap::map(mapStates)
rmap::map(mapHydroShed1)
rmap::map(mapHydroShed2)
rmap::map(mapHydroShed3)
rmap::map(mapIntersectGCAMBasinCountry)
rmap::map(mapIntersectGCAMBasinUS52)
rmap::map(mapIntersectGCAMBasinUS52County)
```
## mapUS49
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapUS49)
```
## mapUS52
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapUS52)
```
## mapUS52Compact
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapUS52Compact)
```
## mapUS49County
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapUS49County)
```
## mapUS52County
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapUS52County)
```
## mapUS52CountyCompact
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapUS52CountyCompact)
```
## mapCountries
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapCountries)
```
## mapCountriesUS52
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapCountriesUS52)
```
## mapGCAMReg32
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapGCAMReg32)
```
## mapGCAMReg32US52
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapGCAMReg32US52)
```
## mapGCAMReg32EU
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapGCAMReg32EU)
```
## mapGCAMBasins
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapGCAMBasins)
```
## mapGCAMBasinsUS49
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapGCAMBasinsUS49)
```
## mapGCAMBasinsUS52
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapGCAMBasinsUS52)
```
## mapGCAMLand
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapGCAMLand)
```
## mapStates
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapStates)
```
## mapHydroShed1
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapHydroShed1)
```
## mapHydroShed2
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapHydroShed2)
```
## mapHydroShed3
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapHydroShed3)
```
## mapIntersectGCAMBasinCountry
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapIntersectGCAMBasinCountry)
```
## mapIntersectGCAMBasinUS52
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapIntersectGCAMBasinUS52)
```
## mapIntersectGCAMBasinUS52County
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
rmap::map(mapIntersectGCAMBasinUS52County)
```
<!-------------------------->
<!-------------------------->
# Projections
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Users can change the projection of maps using the `crs` argumet as follows:
```{r, results = 'hide', eval=TRUE, echo=T, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
# ESRI:54032 World Azimuthal Equidistant, https://epsg.io/54032
rmap::map(mapUS49,
crs="+proj=aeqd +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs")
```
<!-------------------------->
<!-------------------------->
# Map Find
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Users can have rmap automatically search for an appropriate map using the `map_find` function as follows:
```{r, results = 'hide', eval=TRUE, echo=T, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
map_chosen <- rmap::map_find(data)
map_chosen # Will give a dataframe for the chosen map
rmap::map(map_chosen)
```
<!-------------------------->
<!-------------------------->
# Format & Themes
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
<!-------------------------->
<!-------------------------->
## Layers {#layers}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
The `map()` funciton in `rmap` works in layers. The central layer is created using the `data` provided which fills out `values` for the `subRegion` or `lat` and `lon` columns provided. In addition to the central layer, users can choose an `underLayer` and an `overLayer` to show different kinds of intersecting boundaries or surrounding regions. The colors, border size and text labels for each layer can be individually be modified or as a global option. Examples of using layers are provided below for a basic dataset.
```{r, results = 'hide', eval=TRUE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
rmap::map(data) # Will choose the contiguous US map and plot this data for you as the only layer
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
rmap::map(data,
underLayer=rmap::mapUS49) # Will add an underlayer of the US49 map zoomed in to your data set
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
rmap::map(data,
underLayer=rmap::mapUS49,
overLayer=rmap::mapGCAMBasinsUS49) # will add GCAM basins ontop of the previous map
```
<!-------------------------->
<!-------------------------->
## Layer Colors {#layercolors}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
The line width, color and fill of each layer can be individually be modified or as a global option. Examples are provided below for a basic dataset.
```{r, results = 'hide', eval=TRUE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
# Create a basic map with no data and modify fill as well as line color and lwd.
library(rmap);
# Create data table with a few US states
mapShape = rmap::mapUS49
# Modify the line color and lwd of the base layer
rmap::map(mapShape,
fill = "black",
color = "white",
lwd = 1.5) # Will adjust the borders of the different layers to highlight them.
# Create a basic map with data and modify line colors and lwd.
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
# Modify the line color and lwd of the base layer
rmap::map(data,
color = "blue",
lwd = 1.5) # Will adjust the borders of the different layers to highlight them.
# Modify under and overlayer fill, line color and lwd.
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
# Modify the fill and outline of the different layers
rmap::map(data,
underLayer=rmap::mapUS49,
underLayerFill = "black",
underLayerColor = "green",
underLayerLwd = 0.5,
overLayer=rmap::mapGCAMBasinsUS49,
overLayerColor = "red",
overLayerLwd = 2) # Will adjust the borders of the different layers to highlight them.
```
<!-------------------------->
<!-------------------------->
## Labels {#labels}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Labels can be added to the different layers of the maps as follows:
```{r, results = 'hide', eval=TRUE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
# Add Blue Labels
rmap::map(data,
labels = T,
labelSize = 10,
labelColor = "blue")
# Add labels with a transparent background box
rmap::map(data,
labels = T,
labelSize = 10,
labelColor = "black",
labelFill = "white",
labelAlpha = 0.8,
labelBorderSize = 1)
# Repel the labels out with a leader line for crowded labels
rmap::map(data=rmap::mapUS49,
labels = T,
labelSize = 6,
labelColor = "black",
labelFill = "white",
labelAlpha = 0.8,
labelBorderSize = 1,
labelRepel = 1)
# Choose which layers to apply these label options to.
# Labels for data (labels=T), labels for underlayer (underLayerLabels=T), labels for overLayer (overLayerLabels=T)
# UnderLayer Labels Examples
rmap::map(data,
underLayer = rmap::mapUS49,
underLayerLabels = T)
# OverLayer Examples
rmap::map(data,
underLayer = rmap::mapUS49,
overLayer = rmap::mapGCAMBasinsUS49,
overLayerColor = "red",
overLayerLabels = T,
labelSize = 3,
labelColor = "red",
labelFill = "white",
labelAlpha = 0.8,
labelBorderSize = 0.1,
labelRepel = 0)
# All Labels (Not recommended as too confusing)
rmap::map(data,
underLayer = rmap::mapUS49,
overLayer = rmap::mapGCAMBasinsUS49,
labels = T,
underLayerLabels = T,
overLayerLabels = T,
labelSize = 2,
labelColor = "black",
labelFill = "white",
labelAlpha = 0.8,
labelBorderSize = 0.1,
labelRepel = 1)
```
<!-------------------------->
<!-------------------------->
## Crop {#crop}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
By default the maps are cropped to the extent of the subRegions provided in the data. If the argument `crop` is set to `FALSE` then the map will zoom to extents of the largest layer in the map as shown below. Maps can also be cropped to the underLayer or overLayer by setting the `crop_to_underLayer` and `crop_to_overLayer` arguments to `TRUE`.
```{r, results = 'hide', eval=T, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("FL","ID","MO","TX","WY"),
value = c(10,15,34,2,7))
# Setting crop to F will zoom out to the extent of the largest layer (In this case the underLayer)
rmap::map(data,
underLayer = rmap::mapUS49,
underLayerLabels = T,
labels = T,
crop = T,
title = "crop = T")
rmap::map(data,
underLayer = rmap::mapUS49,
underLayerLabels = T,
labels = T,
crop = F,
title = "crop = F")
rmap::map(data,
underLayer = rmap::mapUS49,
overLayer = rmap::mapGCAMBasinsUS52,
crop_to_underLayer = T,
title = "crop_to_underLayer = T")
rmap::map(data,
underLayer = rmap::mapUS49,
overLayer = rmap::mapGCAMBasinsUS52,
crop_to_overLayer = T,
title = "crop_to_overLayer = T")
```
<!-------------------------->
<!-------------------------->
## Zoom {#zoom}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Users can use `zoom` to zoom out or into a cropped map as desired. Options are available to zoom along x only (`zoomx`), along y only (`zoomy`) or both (`zoom`).
```{r, results = 'hide', eval=FALSE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("FL","ID","MO","TX","WY"),
value = c(10,15,34,2,7))
# Set different zoom levels
m0 = rmap::map(data,underLayer = rmap::mapUS49,zoom = 0)
m1 = rmap::map(data,underLayer = rmap::mapUS49,zoom = 7)
m2 = rmap::map(data,underLayer = rmap::mapUS49,zoom = -10)
m3 = rmap::map(data,underLayer = rmap::mapUS49,zoomx = 3)
m4 = rmap::map(data,underLayer = rmap::mapUS49,zoomy = 4)
```
```{r, results = 'hide', eval=TRUE, echo=FALSE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap); library(cowplot)
# Create data table with a few US states
data = data.frame(subRegion = c("FL","ID","MO","TX","WY"),
value = c(10,15,34,2,7))
# Setting crop to F will zoom out to the extent of the largest layer (In this case the underLayer)
m0 = rmap::map(data,underLayer = rmap::mapUS49,zoom = 0, show=F, save = F)
m1 = rmap::map(data,underLayer = rmap::mapUS49,zoom = 1, show=F, save = F)
m2 = rmap::map(data,underLayer = rmap::mapUS49,zoom = 3, show=F, save = F)
m3 = rmap::map(data,underLayer = rmap::mapUS49,zoom = -1, show=F, save = F)
m4 = rmap::map(data,underLayer = rmap::mapUS49,zoom = -3, show=F, save = F)
m5 = rmap::map(data,underLayer = rmap::mapUS49,zoomx = 3, show=F, save = F)
m6 = rmap::map(data,underLayer = rmap::mapUS49,zoomy = 3, show=F, save = F)
cowplot::plot_grid(m0[[1]],m1[[1]], m2[[1]],m3[[1]], m4[[1]], m5[[1]], m6[[1]],
labels = c('zoom = 0',
'zoom = 1',
'zoom = 3',
'zoom = -1',
'zoom = -3',
'zoomx = 3',
'zoomy = 3'), label_size = 14)
```
<!-------------------------->
<!-------------------------->
## Background {#background}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
While users can adjust all the elements of the map using ggplot themes as explained in the [Themes](#themes) section, we have provided a quick argument to simply add blue for oceans and a border to the map if the `background` argument is set to `T`. Additionally if the `background` argument is set to any color such as `grey10` then the map with border will be produced with that color.
```{r, results = 'hide', eval=T, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
# Setting background will add blue for water and a border to the map.
rmap::map(data,
labels = T,
underLayer = rmap::mapCountriesUS52,
background = F,
title = "background = F")
rmap::map(data,
labels = T,
underLayer = rmap::mapCountriesUS52,
background = T,
title = "background = T")
rmap::map(data,
labels = T,
underLayer = rmap::mapCountriesUS52,
background = "grey10",
title = "background = 'grey10")
```
<!-------------------------->
<!-------------------------->
## Themes {#themes}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
The outputs of `rmap::map()` is a list with `ggplot2` objects which can be modified using standard ggplot2 [themes](https://ggplot2.tidyverse.org/reference/ggtheme.html) or by adjusting any individual component of [ggplot2 elements](https://ggplot2.tidyverse.org/reference/theme.html). Some examples are shown below.
```{r, results = 'hide', eval=T, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap); library(ggplot2)
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
# Setting m1 to the first element of the map outputs from rmap::map() with save = F to avoid printing
m1 <- rmap::map(data,
labels = T,
underLayer = rmap::mapCountriesUS52,
background = T,
show = F)
# Now Print with different ggplot elements
m1[[1]] + ggplot2::ggtitle("No Theme")
# Apply the ggplot theme_dark()
m1[[1]] +
ggplot2::theme_dark() +
ggplot2::ggtitle("Theme Dark") +
ggplot2::xlab("MY X Label") +
ggplot2::ylab("MY Y Label")
# Apply custom ggplot theme to an element
m1[[1]] +
ggplot2::ggtitle("Themes: x label and legend position") +
ggplot2::xlab("x label") +
ggplot2::theme(legend.position = "bottom",
legend.text = element_text(size=20),
axis.title.x = element_text(size=20))
```
<!-------------------------->
<!-------------------------->
## Show NA {#showna}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Users have the option to `showNA` with a default (`gray50`) or custom colors.
```{r, results = 'hide', eval=T, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap)
# Create data table with a few US states
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,NA,15,34,2,7))
# Without NA
rmap::map(data,
labels = T,
underLayer = rmap::mapUS49,
title = "showNA is Default (F)")
# showNA = T
rmap::map(data,
labels = T,
underLayer = rmap::mapUS49,
showNA = T,
title = "showNA = T")
# showNA = T, colorNA = 'red'
rmap::map(data,
labels = T,
underLayer = rmap::mapUS49,
showNA = T, colorNA = 'green',
title = "showNA = T & colorNA = 'green'")
```
<!-------------------------->
<!-------------------------->
# Plot Polygon Data
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
For a given data table in the correct format (see [Input Format](#inputs)) `map()` will search through the list of [maps](https://jgcri.github.io/rmap/reference/index.html) to see if it can find the "subRegions" provided in the data and then plot the data on the most appropriate map. Users can specify a specific map. Some examples are provided below:
```{r, results = 'hide', eval=TRUE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
# US Contiguous States
data = data.frame(subRegion = c("CA","FL","ID","MO","TX","WY"),
value = c(5,10,15,34,2,7))
map(data)
```
<!-------------------------->
<!-------------------------->
# Polygon Data Select Map {#select}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Sometimes subRegions can be present on multiple maps. For example "Colombia", China" and "India" are all members of `rmap::mapGCAMReg32` as well as `rmap::mapCountries`. If a user knows which map they want to plot their data on they should specify the map in the `shape` argument.
```{r, results = 'hide', eval=TRUE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap)
data = data.frame(subRegion = c("China","India","Pakistan"),
x = c(2050,2050,2050),
value = c(5,12,30))
# Auto selection by rmap will choose rmap::mapCountries
rmap::map(data)
# User can specify that they want to plot this data on rmap::mapGCAMReg32
rmap::map(data,
shape = rmap::mapGCAMReg32,
crop = F # Because will crop to the shapes boundaries when shape is specified
)
```
<!-------------------------->
<!-------------------------->
# Plot Gridded Data
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
For a given data table in the correct format (see [Input Format](#inputs)) `map()` will search through the list of [maps](https://jgcri.github.io/rmap/reference/index.html) to see if it can find the "subRegions" provided in the data and then plot the data on the most appropriate map. Users can specify a specific map. Some examples are provided below:
```{r, eval=TRUE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap); library(dplyr)
# Using example grid data provided in rmap
# example_gridData_GWPv4To2015
# Original data from https://sedac.ciesin.columbia.edu/data/set/gpw-v4-population-count-rev11/data-download#
# Center for International Earth Science Information Network (CIESIN) - Columbia University. 2016.
# Gridded Population of the World, Version 4 (GPWv4): Population Count. NASA Socioeconomic Data and Applications Center (SEDAC),
# Palisades, NY. DOI: http://dx.doi.org/10.7927/H4X63JVC
# Subset data
data = example_gridData_GWPv4To2015 %>%
filter(x == 2015);
head(data)
```
```{r, results = 'hide', eval=TRUE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
rmap::map(data)
# Plot part of data (first 10000 rows)
rmap::map(data %>% head(10000))
# Add an underlayer to gridded data
rmap::map(data,
overLayer = rmap::mapCountriesUS52,
background=T)
```
<!-------------------------->
<!-------------------------->
# Using Custom Shapes {#custom}
<!-------------------------->
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
Users can provide custom shapefiles for their own data if needed. The example below shows how to create a custom shapefile and then plot data on it.
## Subset existing shape
```{r, results = 'hide', eval=TRUE, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap)
shapeSubset <- rmap::mapStates # Read in World States shape file
shapeSubset <- shapeSubset[shapeSubset$region %in% c("Colombia"),] # Subset the shapefile to Colombia
rmap::map(shapeSubset) # View custom shape
head(shapeSubset) # review data
unique(shapeSubset$subRegion) # Get a list of the unique subRegions
# Plot data on subset
data = data.frame(subRegion = c("Cauca","Valle del Cauca","Antioquia","Córdoba","Bolívar","Atlántico"),
x = c(2050,2050,2050,2050,2050,2050),
value = c(5,10,15,34,2,7))
rmap::map(data,
shape = shapeSubset,
underLayer = shapeSubset,
crop=F) # Must rename the states column to 'subRegion'
```
## Crop a shape to another shape
For example if someone wants to analyze counties in Texas.
```{r, results = 'hide', eval=F, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap); library(raster); library(sf)
shapeSubRegions <- rmap::mapUS49County
shapeCropTo <- rmap::mapUS49
shapeCropTo <- shapeCropTo[shapeCropTo$subRegion %in% c("TX"),]
shapeCrop<- sf::st_transform(shapeCropTo,sf::st_crs(shapeSubRegions))
shapeCrop <-sf::st_as_sf(raster::crop(as(shapeSubRegions,"Spatial"),as(shapeCropTo,"Spatial")))
rmap::map(shapeCrop)
# Plot data on subset
data = data.frame(subRegion = c("Wise_TX","Scurry_TX","Kendall_TX","Frio_TX","Hunt_TX","Austin_TX"),
value = c(5,10,15,34,2,7))
rmap::map(data,
shape = shapeCrop,
underLayer = shapeCrop,
crop=F)
```
## Save Custom Shapefile {#saveShape}
A cropped or custom shapefile which may be much smaller in size can be saved offline as an `ESRI Shapefile` for later use in `rmap` or other software if desired as follows:
```{r, results = 'hide', eval=F, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rgdal)
rgdal::writeOGR(obj=shapeCrop,
dsn=paste0("path/to/shapefile/shapeCrop"),
layer="shapeCrop",
driver="ESRI Shapefile", overwrite_layer=TRUE)
```
## Read data from a Shapefile
User can read data from their own offline `ESRI Shapefile`. Usually the shapefile is contained in a single folder (e.g. `custom_shape_folder`) with the following subfiles, where `custom_shape` will be a different name. These kinds of files can be created in R, ArcGIS, QGIS and other softwares (see Section [Save Custom Shapefile](#saveShape)).
- custom_shape.dbf
- custom_shape.prj
- custom_shape.shp
- custom_shape.shx
After reading in the shapefile (e.g. as `my_custom_map`), users will need to insure that it contains a `subRegion` column corresponding to the features in the shape.
```{r, results = 'hide', eval=F, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap); library(rgdal); library(sp)
# Assuming custom_shape files are in folder custom_shape_folder
my_custom_map = sf::st_read("path/to/custom_shape_file.shp")
head(my_custom_map)
plot(my_custom_map) # Quick view of shape
# Rename subRegion column
my_custom_map <- my_custom_map %>% dplyr::mutate(subRegion="CHOOSE_APPROPRIATE _COLUMN");
# Now a data frame with data corresponding to the custom shape regions can be created and plotted
# Generate some random data for each subRegion
data <- data.frame(subRegion=unique(my_custom_map$subRegion),
value = 100*runif(length(unique(my_custom_map$subRegion))));
head(data)
# And then it can be plotted using rmap and any of the built-in maps as underLayers.
rmap::map(data,
shape=my_custom_map,
underLayer=rmap::mapCountries,
underLayerLabels = T)
```
<!-------------------------->
<!-------------------------->
# Multi-Year-Class-Scenario-Facet
<!-------------------------->
<!-------------------------->
<!-------------------------->
## Multi-Year & Animations
<!-------------------------->
<p align="center"> <img src="vignetteFigs/divider.png"></p>
### Polygon Data Multi-Year
```{r, results = 'hide', eval=F, echo=TRUE, warning=FALSE, error = FALSE, message = FALSE}
library(rmap);
data = data.frame(subRegion = c("Austria","Spain", "Italy", "Germany","Greece",
"Austria","Spain", "Italy", "Germany","Greece",
"Austria","Spain", "Italy", "Germany","Greece",
"Austria","Spain", "Italy", "Germany","Greece"),
year = c(rep(2025,5),
rep(2050,5),
rep(2075,5),
rep(2100,5)),
value = c(32, 38, 54, 63, 24,
37, 53, 23, 12, 45,
23, 99, 102, 85, 75,
12, 76, 150, 64, 90))
rmap::map(data = data,
underLayer = rmap::mapCountries,
ncol=4,
background = T)
# NOTE: Animation files are only saved to file and not as a direct output of the rmap::map() function.
```
```{r, results = 'hide', eval=T, echo=F, warning=FALSE, error = FALSE, message = FALSE}