forked from king0708/spatial-data-viz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sf_basemap_workshop_ecodatasci.html
1669 lines (1643 loc) · 156 KB
/
sf_basemap_workshop_ecodatasci.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.1.189">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Rachel King">
<meta name="dcterms.date" content="2024-03-08">
<title>Spatial Data Visualization</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="sf_basemap_workshop_ecodatasci_files/libs/clipboard/clipboard.min.js"></script>
<script src="sf_basemap_workshop_ecodatasci_files/libs/quarto-html/quarto.js"></script>
<script src="sf_basemap_workshop_ecodatasci_files/libs/quarto-html/popper.min.js"></script>
<script src="sf_basemap_workshop_ecodatasci_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="sf_basemap_workshop_ecodatasci_files/libs/quarto-html/anchor.min.js"></script>
<link href="sf_basemap_workshop_ecodatasci_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="sf_basemap_workshop_ecodatasci_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="sf_basemap_workshop_ecodatasci_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="sf_basemap_workshop_ecodatasci_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="sf_basemap_workshop_ecodatasci_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article toc-left">
<div id="quarto-sidebar-toc-left" class="sidebar toc-left">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Sections</h2>
<ul>
<li><a href="#helpful-background" id="toc-helpful-background" class="nav-link active" data-scroll-target="#helpful-background">Helpful Background</a></li>
<li><a href="#goals-and-learning-outcomes" id="toc-goals-and-learning-outcomes" class="nav-link" data-scroll-target="#goals-and-learning-outcomes">Goals and Learning Outcomes</a></li>
<li><a href="#set-up" id="toc-set-up" class="nav-link" data-scroll-target="#set-up">Set Up</a>
<ul class="collapse">
<li><a href="#loading-packages" id="toc-loading-packages" class="nav-link" data-scroll-target="#loading-packages">Loading Packages</a></li>
</ul></li>
<li><a href="#part-i-intro-to-working-with-spatial-data-with-the-sf-package" id="toc-part-i-intro-to-working-with-spatial-data-with-the-sf-package" class="nav-link" data-scroll-target="#part-i-intro-to-working-with-spatial-data-with-the-sf-package">Part I: Intro to working with spatial data with the <strong><code>sf</code></strong> package</a>
<ul class="collapse">
<li><a href="#load-data" id="toc-load-data" class="nav-link" data-scroll-target="#load-data">Load Data</a>
<ul class="collapse">
<li><a href="#about-the-data" id="toc-about-the-data" class="nav-link" data-scroll-target="#about-the-data">About the Data</a></li>
<li><a href="#method-1-from-a-.csv-file" id="toc-method-1-from-a-.csv-file" class="nav-link" data-scroll-target="#method-1-from-a-.csv-file">Method 1: From a <code>.csv</code> file</a></li>
<li><a href="#method-2-from-a-shapefile" id="toc-method-2-from-a-shapefile" class="nav-link" data-scroll-target="#method-2-from-a-shapefile">Method 2: From a shapefile</a></li>
<li><a href="#method-3-from-a-package" id="toc-method-3-from-a-package" class="nav-link" data-scroll-target="#method-3-from-a-package">Method 3: From a package</a></li>
</ul></li>
<li><a href="#working-with-sf-data" id="toc-working-with-sf-data" class="nav-link" data-scroll-target="#working-with-sf-data">Working with <code>sf</code> data</a>
<ul class="collapse">
<li><a href="#identify-and-set-the-crs" id="toc-identify-and-set-the-crs" class="nav-link" data-scroll-target="#identify-and-set-the-crs">Identify and set the CRS</a></li>
<li><a href="#basic-spatial-data-operations" id="toc-basic-spatial-data-operations" class="nav-link" data-scroll-target="#basic-spatial-data-operations">Basic Spatial Data Operations</a></li>
</ul></li>
<li><a href="#making-the-map" id="toc-making-the-map" class="nav-link" data-scroll-target="#making-the-map">Making the map</a></li>
</ul></li>
<li><a href="#part-ii-add-a-basemap" id="toc-part-ii-add-a-basemap" class="nav-link" data-scroll-target="#part-ii-add-a-basemap">Part II: Add a Basemap</a>
<ul class="collapse">
<li><a href="#my-favorite-basemap-providers-packages-and-functions" id="toc-my-favorite-basemap-providers-packages-and-functions" class="nav-link" data-scroll-target="#my-favorite-basemap-providers-packages-and-functions">My Favorite Basemap Providers, Packages and Functions</a></li>
<li><a href="#retrieving-your-basemap" id="toc-retrieving-your-basemap" class="nav-link" data-scroll-target="#retrieving-your-basemap">Retrieving your basemap</a>
<ul class="collapse">
<li><a href="#using-ggspatial" id="toc-using-ggspatial" class="nav-link" data-scroll-target="#using-ggspatial">Using <code>ggspatial</code></a></li>
<li><a href="#using-maptiles" id="toc-using-maptiles" class="nav-link" data-scroll-target="#using-maptiles">Using <code>maptiles</code></a></li>
<li><a href="#additional-basemap-options" id="toc-additional-basemap-options" class="nav-link" data-scroll-target="#additional-basemap-options">Additional basemap options</a></li>
<li><a href="#basemap-from-file" id="toc-basemap-from-file" class="nav-link" data-scroll-target="#basemap-from-file">Basemap from File</a></li>
</ul></li>
<li><a href="#add-a-scalebar-and-north-arrow" id="toc-add-a-scalebar-and-north-arrow" class="nav-link" data-scroll-target="#add-a-scalebar-and-north-arrow">Add a scalebar and north arrow</a>
<ul class="collapse">
<li><a href="#manipulating-scale-and-north-arrow" id="toc-manipulating-scale-and-north-arrow" class="nav-link" data-scroll-target="#manipulating-scale-and-north-arrow">Manipulating scale and north arrow</a></li>
</ul></li>
<li><a href="#exercises" id="toc-exercises" class="nav-link" data-scroll-target="#exercises">Exercises</a></li>
</ul></li>
<li><a href="#part-iii-tips-and-tricks" id="toc-part-iii-tips-and-tricks" class="nav-link" data-scroll-target="#part-iii-tips-and-tricks">Part III: Tips and Tricks</a>
<ul class="collapse">
<li><a href="#making-an-inset-map" id="toc-making-an-inset-map" class="nav-link" data-scroll-target="#making-an-inset-map">Making an inset map</a></li>
<li><a href="#creating-a-theme-and-setting-defaults" id="toc-creating-a-theme-and-setting-defaults" class="nav-link" data-scroll-target="#creating-a-theme-and-setting-defaults">Creating a Theme and Setting Defaults</a>
<ul class="collapse">
<li><a href="#create-your-own-theme" id="toc-create-your-own-theme" class="nav-link" data-scroll-target="#create-your-own-theme">Create your own theme</a></li>
<li><a href="#set-geometry-defaults" id="toc-set-geometry-defaults" class="nav-link" data-scroll-target="#set-geometry-defaults">Set Geometry Defaults</a></li>
</ul></li>
<li><a href="#functional-programming" id="toc-functional-programming" class="nav-link" data-scroll-target="#functional-programming">Functional Programming</a></li>
</ul></li>
<li><a href="#additional-resources" id="toc-additional-resources" class="nav-link" data-scroll-target="#additional-resources">Additional Resources</a></li>
<li><a href="#references" id="toc-references" class="nav-link" data-scroll-target="#references">References</a></li>
</ul>
</nav>
</div>
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Spatial Data Visualization</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Rachel King </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">March 8, 2024</p>
</div>
</div>
</div>
</header>
<section id="helpful-background" class="level1">
<h1>Helpful Background</h1>
<p>This workshop will be most helpful if you have spent some time working with data in R, either for data analysis or visualization. Specifically, the workshop will be most helpful if you are:</p>
<ul>
<li><p>comfortable with the basics of working with data in R (reading in data, tidyverse functions, making a graph with ggplot)</p></li>
<li><p>familiar with different types of spatial data (not necessarily working with them in R)</p></li>
<li><p>have an interest in spatial data and learning how R can help you with spatial data visualization</p></li>
</ul>
<p>The primary purpose is to demonstrate how to use R to visualize spatial data, rather than give a full tutorial on the basics of geospatial data though we will review some basic concepts quickly. Therefore we may not cover some important concepts that are important for spatial data analysis, For more in-depth learning I recommend reading “Geocomputation with R” by Lovelace et al. (2024), a free ebook available here: <a href="https://r.geocompx.org/"></a>.</p>
</section>
<section id="goals-and-learning-outcomes" class="level1">
<h1>Goals and Learning Outcomes</h1>
<p>By the end of the session you should be able to:</p>
<ol type="1">
<li><p>Read in and map simple spatial data</p></li>
<li><p>Add a basemap, scalebar and north arrow to your maps</p></li>
<li><p>Understand how you can use R to streamline the map-making process (using themes and functions)</p></li>
</ol>
</section>
<section id="set-up" class="level1">
<h1>Set Up</h1>
<section id="loading-packages" class="level2">
<h2 class="anchored" data-anchor-id="loading-packages">Loading Packages</h2>
<p>First we need to load packages and data for our mapping. Note that some of these packages will only work if you have already installed GDAL (Geospatial Data Abstraction Library) - a “computer software library for reading and writing raster and vector geospatial data formats”.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># General Packages</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse) <span class="co"># data manipulation and plotting </span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2) <span class="co"># plotting and mapping</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(patchwork) <span class="co"># combining maps/plots</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Spatial Packages</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf) <span class="co"># manipulating spatial data</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggspatial) <span class="co"># retrieving basemap, adding scale and arrowbar</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(maptiles) <span class="co"># retrieving basemap</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(terra) <span class="co"># working with raster data</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyterra) <span class="co"># functions for working with raster</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="co"># Data </span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tigris) <span class="co"># shapefiles with various administrative boundaries and roads</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Another good option for plotting spatial data in R is the <code>tmap</code> package with companion package <code>tmaptools</code>. However, the syntax is slightly different so I won’t cover that today. It does allow an interactive view feature via Leaflet which is nice, and maybe the subject for a future EcoDataScience workshop.</p>
</section>
</section>
<section id="part-i-intro-to-working-with-spatial-data-with-the-sf-package" class="level1">
<h1>Part I: Intro to working with spatial data with the <strong><code>sf</code></strong> package</h1>
<section id="load-data" class="level2">
<h2 class="anchored" data-anchor-id="load-data">Load Data</h2>
<section id="about-the-data" class="level3">
<h3 class="anchored" data-anchor-id="about-the-data">About the Data</h3>
<p>The data used here are open source data available either on the KNB data repository or from the US Government. The specific data sources and citations are listed below:</p>
<table class="table">
<thead>
<tr class="header">
<th>Data</th>
<th>Original Dataset</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Study Sites</td>
<td>cj lortie, M Zuliani, Nargol Ghazian, Jenna Braun, S Haas, & Rachel King. 2022. A list of ecological study sites within Central California Drylands. Knowledge Network for Biocomplexity. <a href="https://doi.org/10.5063/F1F76B1R">doi:10.5063/F1F76B1R</a>.</td>
</tr>
<tr class="even">
<td>California Ecoregions</td>
<td>U.S. Environmental Protection Agency. 2012. Level IV Ecoregions of California. U.S. EPA Office of Research and Development (ORD) - National Health and Environmental Effects Research Laboratory (NHEERL), Corvallis, OR. <a href="https://www.epa.gov/eco-research/ecoregion-download-files-state-region-9#pane-04">California Level IV Ecoregions</a></td>
</tr>
<tr class="odd">
<td>California State Boundary</td>
<td>U.S. Census Bureau TIGER/Line Shapefiles. (2023). <a href="https://www.census.gov/geographies/mapping-files/time-series/geo/tiger-line-file.html"></a></td>
</tr>
</tbody>
</table>
</section>
<section id="method-1-from-a-.csv-file" class="level3">
<h3 class="anchored" data-anchor-id="method-1-from-a-.csv-file">Method 1: From a <code>.csv</code> file</h3>
<p>We will download the site data from KNB to show how to read in data from a <code>.csv</code> file and prepare it for mapping. When you use a <code>.csv</code> you will have to convert it to an <code>sf</code> object to use the <code>sf</code> package functions for easier plotting, which I will show in a moment.</p>
<p>Download site data from: <a href="https://doi.org/10.5063/F1F76B1R">California Dryland Sites</a></p>
<p>https://doi.org/10.5063/F1F76B1R</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># read in data from .csv file</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>site_df <span class="ot"><-</span> <span class="fu">read_csv</span>(<span class="st">"https://knb.ecoinformatics.org/knb/d1/mn/v2/object/urn%3Auuid%3A5d23a3f4-6ed8-47f0-b34e-000f6cfb8313"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Rows: 51 Columns: 19
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (10): state, desert, region, experiment, site_acronym, site_code, founda...
dbl (9): elevation, area_m2, area_block_m2, MAT, MAP, long, lat, aridity, n...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># alternatively download into your working directory and run </span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="co"># site_df <- read_csv("tidy_regional_2023.csv")</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="method-2-from-a-shapefile" class="level3">
<h3 class="anchored" data-anchor-id="method-2-from-a-shapefile">Method 2: From a shapefile</h3>
<p>The second data layer we will read in is a shapefile with ecoregions for California that is provided by the US EPA: <a href="https://www.epa.gov/eco-research/ecoregion-download-files-state-region-9#pane-04">California Level IV Ecoregions</a></p>
<p>You’ll need to download and unzip the data file into your working directory for the following code to work.</p>
<p>We will use the <code>read_sf</code> function from the <code>sf</code> package, which loads the shapefile as a <code>tidyverse</code> tibble. You can also use <code>st_read</code> to load shapefiles, but it contains more messages and loads the data as a base R data.frame instead of a tibble.</p>
<div class="callout-important callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>When reading in a shapefile, you use the filename with the <code>.shp</code> extension, but the associated files from the zipped folder are required for the data to read in properly. So, if you only unzip and then only copy the <code>.shp</code> file into your working directory or data folder for a project you will get an error message.</p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># read in data from shapefile</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>ecoreg_sf <span class="ot"><-</span> sf<span class="sc">::</span><span class="fu">read_sf</span>(<span class="st">"data/ca_ecoregion_shape/ca_eco_l4.shp"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="method-3-from-a-package" class="level3">
<h3 class="anchored" data-anchor-id="method-3-from-a-package">Method 3: From a package</h3>
<p>The <code>tigris</code> package allows you to easily download TIGER/Line shapefiles from the US Census Bureau for things like state boundaries, roads, county lines, etc. The <code>tidycensus</code> package that was in a previous EcoDataScience Workshop uses this package when getting spatial data.</p>
<p>We’ll use it to download some boundaries for the state of California.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># read in data using package </span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>states_sf <span class="ot"><-</span> tigris<span class="sc">::</span><span class="fu">states</span>(<span class="at">progress_bar =</span> <span class="cn">FALSE</span>)</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>CA_counties_sf <span class="ot"><-</span> tigris<span class="sc">::</span><span class="fu">counties</span>(<span class="at">state =</span> <span class="st">"CA"</span>, <span class="at">progress_bar =</span> <span class="cn">FALSE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
<section id="working-with-sf-data" class="level2">
<h2 class="anchored" data-anchor-id="working-with-sf-data">Working with <code>sf</code> data</h2>
<p>Now we’ll see some useful features of the <code>sf</code> package and how to access and set important spatial information from your data.</p>
<p>Most functions in <code>sf</code> start with <code>st_[function_name]</code>, which stands for <em>spatial transformation</em>. These functions perform various spatial data operations so we can manipulate our data for better visualizations.</p>
<section id="identify-and-set-the-crs" class="level3">
<h3 class="anchored" data-anchor-id="identify-and-set-the-crs">Identify and set the CRS</h3>
<p>The <strong>Coordinate Reference System (CRS)</strong> is an important piece of information that contains the instructions for how to display the spatial data. It tells R where the points are located and how to project the points from a spherical (or ellipsoidal) 3D object to a 2D surface. See <a href="https://datacarpentry.org/organization-geospatial/03-crs.html">this</a> data carpentry lesson, <a href="https://r.geocompx.org/spatial-class#crs-intro">Section 2.4</a> in Geocomputation in R, or <a href="https://www.esri.com/arcgis-blog/products/arcgis-pro/mapping/gcs_vs_pcs/">this</a> post from ESRI for more information on CRS’s.</p>
<p>When you read in data from a shapefile or a package the data should already have the CRS information associated with the data. If it is an unusual CRS you should check the metadata for additional details about the CRS.</p>
<p>When you are reading in data from a <code>.csv</code> file, you need to set the CRS yourself when you convert your <code>data.frame</code> into an <code>sf</code> object. Thus, you’ll need to check the metadata to find the appropriate CRS.</p>
<p>You’ll often see lat/long data use the WGS84 CRS, which is a geographic (unprojected) coordinate reference system. This is the CRS used by the <code>site_df</code>.</p>
<p>An easy way to specify this is to use the <strong>EPSG</strong> code (European Petroleum Survey Group), which references a database of the associated geodetic parameters for common CRS. The code for the WGS84 is <strong>EPSG:4326</strong>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(site_df) <span class="co"># check column names</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "state" "desert" "region" "experiment"
[5] "site_acronym" "site_code" "foundation" "elevation"
[9] "aspect" "area_m2" "area_calc" "polygon_block"
[13] "area_block_m2" "MAT" "MAP" "long"
[17] "lat" "aridity" "ndvi_2023" </code></pre>
</div>
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># The coordinate information is in the "long" and "lat column</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>site_sf <span class="ot"><-</span> site_df <span class="sc">%>%</span> </span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">st_as_sf</span>(</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="at">coords =</span> <span class="fu">c</span>(<span class="st">"long"</span>, <span class="st">"lat"</span>), <span class="co"># specify where spatial data is; "longitude" is first</span></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> <span class="at">crs =</span> <span class="st">"EPSG:4326"</span> <span class="co"># need to tell it what the CRS is</span></span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Now you’ll see that our site data not just a data frame but is also now an <code>sf</code> object, which allows us to use all of the <code>sf</code> functions with this data now.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">class</span>(site_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "sf" "tbl_df" "tbl" "data.frame"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(site_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Simple feature collection with 6 features and 17 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -120.1018 ymin: 34.87623 xmax: -114.9 ymax: 36.06219
Geodetic CRS: WGS 84
# A tibble: 6 × 18
state desert region experiment site_acronym site_code foundation elevation
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl>
1 Californ… Mojave Mojave invasion Antelope_va… Antelope… Ephedra c… NA
2 Californ… San J… San J… BNLL diet Avenal - hw… Avenal Ephedra c… NA
3 Californ… Mojave Mojave ERG Barstow Barstow_1 Ephedra c… 496.
4 Californ… Mojave Mojave invasion Barstow_2 Barstow_2 Ephedra c… NA
5 Nevada Mojave Mojave invasion Cal-Nev-Ari Cal_nev_… Larrea tr… NA
6 Californ… San J… Carri… BNLL plant-animal Carrizo_1 Ephedra c… NA
# ℹ 10 more variables: aspect <chr>, area_m2 <dbl>, area_calc <chr>,
# polygon_block <chr>, area_block_m2 <dbl>, MAT <dbl>, MAP <dbl>,
# aridity <dbl>, ndvi_2023 <dbl>, geometry <POINT [°]></code></pre>
</div>
</div>
<p>You’ll also see that we have some new attribute information associated with this object:</p>
<ul>
<li><p>Geometry Type: Point</p></li>
<li><p>Dimension</p></li>
<li><p>Bounding Box</p></li>
<li><p>CRS</p></li>
</ul>
<p>We can also access this info with some common <code>sf</code> functions:</p>
<ul>
<li><p><code>st_crs</code> to check the CRS of the object</p></li>
<li><p><code>st_bbox</code> retrieves the bounding box around the entire object</p></li>
<li><p><code>st_geometry_type</code> identifies the geometry of each observation/row in the dataset</p></li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co"># get the crs</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="fu">st_crs</span>(site_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Coordinate Reference System:
User input: EPSG:4326
wkt:
GEOGCRS["WGS 84",
ENSEMBLE["World Geodetic System 1984 ensemble",
MEMBER["World Geodetic System 1984 (Transit)"],
MEMBER["World Geodetic System 1984 (G730)"],
MEMBER["World Geodetic System 1984 (G873)"],
MEMBER["World Geodetic System 1984 (G1150)"],
MEMBER["World Geodetic System 1984 (G1674)"],
MEMBER["World Geodetic System 1984 (G1762)"],
MEMBER["World Geodetic System 1984 (G2139)"],
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]],
ENSEMBLEACCURACY[2.0]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["geodetic latitude (Lat)",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["geodetic longitude (Lon)",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
USAGE[
SCOPE["Horizontal component of 3D system."],
AREA["World."],
BBOX[-90,-180,90,180]],
ID["EPSG",4326]]</code></pre>
</div>
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># retrieve the bounding box around all points in the object</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="fu">st_bbox</span>(site_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> xmin ymin xmax ymax
-120.81229 34.20568 -114.07000 36.76000 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">st_geometry_type</span>(site_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT
[13] POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT
[25] POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT
[37] POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT POINT
[49] POINT POINT POINT
18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE</code></pre>
</div>
</div>
<p>In addition to the <code>sf</code> operations, objects of class <code>sf</code> usually function very similarly to other <code>tibbles</code> or <code>data.frames</code> and you can pretty much perform operations on these objects as usual. However, the <code>geometry</code> column can complicate some things, and if you want to remove it you need to call <code>st_drop_geometry</code>.</p>
<p>When working with multiple data sources, it is important to have them in the same CRS for visualizing or they won’t map properly. So, we need to check the CRS for the other datasets to see if they match:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="co"># crs for ecoregions</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="fu">st_crs</span>(ecoreg_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Coordinate Reference System:
User input: USA_Contiguous_Albers_Equal_Area_Conic_USGS_version
wkt:
PROJCRS["USA_Contiguous_Albers_Equal_Area_Conic_USGS_version",
BASEGEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["Degree",0.0174532925199433]]],
CONVERSION["USA_Contiguous_Albers_Equal_Area_Conic_USGS_version",
METHOD["Albers Equal Area",
ID["EPSG",9822]],
PARAMETER["Latitude of false origin",23,
ANGLEUNIT["Degree",0.0174532925199433],
ID["EPSG",8821]],
PARAMETER["Longitude of false origin",-96,
ANGLEUNIT["Degree",0.0174532925199433],
ID["EPSG",8822]],
PARAMETER["Latitude of 1st standard parallel",29.5,
ANGLEUNIT["Degree",0.0174532925199433],
ID["EPSG",8823]],
PARAMETER["Latitude of 2nd standard parallel",45.5,
ANGLEUNIT["Degree",0.0174532925199433],
ID["EPSG",8824]],
PARAMETER["Easting at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8826]],
PARAMETER["Northing at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8827]]],
CS[Cartesian,2],
AXIS["(E)",east,
ORDER[1],
LENGTHUNIT["metre",1]],
AXIS["(N)",north,
ORDER[2],
LENGTHUNIT["metre",1]],
USAGE[
SCOPE["Not known."],
AREA["United States (USA) - CONUS onshore - Alabama; Arizona; Arkansas; California; Colorado; Connecticut; Delaware; Florida; Georgia; Idaho; Illinois; Indiana; Iowa; Kansas; Kentucky; Louisiana; Maine; Maryland; Massachusetts; Michigan; Minnesota; Mississippi; Missouri; Montana; Nebraska; Nevada; New Hampshire; New Jersey; New Mexico; New York; North Carolina; North Dakota; Ohio; Oklahoma; Oregon; Pennsylvania; Rhode Island; South Carolina; South Dakota; Tennessee; Texas; Utah; Vermont; Virginia; Washington; West Virginia; Wisconsin; Wyoming."],
BBOX[24.41,-124.79,49.38,-66.91]],
ID["ESRI",102039]]</code></pre>
</div>
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">st_crs</span>(states_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Coordinate Reference System:
User input: NAD83
wkt:
GEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["latitude",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["longitude",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]]</code></pre>
</div>
</div>
<p>So, all of our spatial data sets currently have different CRS. We will need to reproject the data from two of the three so that we will be able to map all our data at the same time.</p>
</section>
<section id="basic-spatial-data-operations" class="level3">
<h3 class="anchored" data-anchor-id="basic-spatial-data-operations">Basic Spatial Data Operations</h3>
<p>We can use the <code>st_transform</code> function to project our data into the same CRS.</p>
<p>But which CRS should we choose for our map making? It depends on what you are doing… there are projections that preserve area, distance, angles, so you will need to decide based on what is important in your situation. For a static map it may make sense to use a projection more specific to your area or goal (e.g. equal area). For simplicity, today I’ll transform the state and site data to the CRS of the ecoregion data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="co"># project site and state lines to ecoreg CRS</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>states_proj_sf <span class="ot"><-</span> <span class="fu">st_transform</span>(states_sf, <span class="fu">st_crs</span>(ecoreg_sf))</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>site_proj_sf <span class="ot"><-</span> <span class="fu">st_transform</span>(site_sf, <span class="fu">st_crs</span>(ecoreg_sf))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The states dataset also contains data for ALL US states. We can filter this data just like any other dataset so we just have the outline for California:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>ca_proj_sf <span class="ot"><-</span> states_proj_sf <span class="sc">%>%</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(NAME <span class="sc">==</span> <span class="st">"California"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>There are some sites that are outside the state of California, and I only want to map the California locations. While we could filter based on the <code>state</code> column in the dataset, sometimes you won’t have that. Another way to filter is to use the state outline to filter our site data to just include the sites within our area of interest:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">unique</span>(site_proj_sf<span class="sc">$</span>state) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "California" "Nevada" </code></pre>
</div>
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>site_ca_proj_sf <span class="ot"><-</span> site_proj_sf <span class="sc">%>%</span> </span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">st_filter</span>(ca_proj_sf, <span class="at">.predicate =</span> st_covered_by)</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a><span class="fu">unique</span>(site_ca_proj_sf<span class="sc">$</span>state)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "California"</code></pre>
</div>
</div>
<p>Here we used the <code>st_filter</code> function which can take a user-specified predicate function to “keep the geometries in x where .predicate(x,y) returns any match in y for x”. So, <code>st_covered_by</code> will return all values in x (<code>site_proj_sf</code>) that are covered by y (<code>ca_proj_sf</code>).</p>
<p>Now, if I wanted to know the ecoregion of each of my sites I could also find that out using a spatial join. Using <code>st_join</code> we will add in the attributes of the <code>ecoreg_sf</code> object at any location that intersects the coordinates from the <code>site_ca_proj_sf</code> object.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>site_ecoreg_sf <span class="ot"><-</span> <span class="fu">st_join</span>(site_ca_proj_sf, ecoreg_sf, <span class="at">join =</span> st_intersects)</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="co"># st_intersects is the default</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(site_ecoreg_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "state" "desert" "region" "experiment"
[5] "site_acronym" "site_code" "foundation" "elevation"
[9] "aspect" "area_m2" "area_calc" "polygon_block"
[13] "area_block_m2" "MAT" "MAP" "aridity"
[17] "ndvi_2023" "geometry" "OBJECTID" "US_L4CODE"
[21] "US_L4NAME" "US_L3CODE" "US_L3NAME" "NA_L3CODE"
[25] "NA_L3NAME" "NA_L2CODE" "NA_L2NAME" "NA_L1CODE"
[29] "NA_L1NAME" "STATE_NAME" "EPA_REGION" "L4_KEY"
[33] "L3_KEY" "L2_KEY" "L1_KEY" "Shape_Leng"
[37] "Shape_Area" </code></pre>
</div>
</div>
<p>There are many other spatial operations you can perform with the <code>sf</code> package. For additional details you can see <a href="https://r.geocompx.org/spatial-operations">Section 4.2</a> of <code>Geocomputation in R</code>. Geometry operations are covered in Ch. 5.</p>
</section>
</section>
<section id="making-the-map" class="level2">
<h2 class="anchored" data-anchor-id="making-the-map">Making the map</h2>
<p>Finally it’s time to make our map! One of the simplest ways to do this is to use <code>ggplot2</code> which works well with <code>sf</code> objects. To plot <code>sf</code> objects, we use the <code>geom_sf</code> instead of <code>geom_point</code> or <code>geom_line</code> that you might use with graphs or figures in <code>ggplot2</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> ca_proj_sf) <span class="sc">+</span></span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>() </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>When adding multiple layers, I usually specify the data in the <code>geom_sf</code> call so it is a bit more clear what each call is plotting. However, this means you must add <code>data =</code> before the data type or it will throw an error.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>() <span class="sc">+</span> <span class="co"># don't specify data here since we have multiple data sets to plot</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ca_proj_sf) <span class="sc">+</span> <span class="co"># must specify "data = " or it will throw an error</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ecoreg_sf) <span class="sc">+</span> </span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> site_ca_proj_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>One of the things I like about mapping with <code>ggplot</code> is that if you are familiar with it already, modifying the plot appearance is very similar so you don’t have to learn many new things to visualize your data. For example, you can set fill and color in the same way via the aesthetics or in the main call to <code>geom_sf</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>() <span class="sc">+</span> <span class="co"># don't specify data here since we have multiple data sets to plot</span></span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ca_proj_sf, <span class="at">fill =</span> <span class="cn">NA</span>) <span class="sc">+</span> <span class="co"># specify no fill</span></span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ecoreg_sf, <span class="fu">aes</span>(<span class="at">fill =</span> US_L3NAME), <span class="at">color =</span> <span class="st">"white"</span>) <span class="sc">+</span> <span class="co"># like other data you can set fill </span></span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> site_ca_proj_sf) <span class="sc">+</span></span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_discrete</span>(<span class="at">name =</span> <span class="st">"Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>When visualizing the data, you can see that it will scale to automatically include all of the data plotted. However, here we may want to focus in on the sites since there are no sites in norther California. Normally to modify the extent of the plot with <code>ggplot</code> you can use <code>coord_cartesian</code>, but with <code>geom_sf</code> you have to use <code>coord_sf</code>. You can also use <code>scale_x_continuous</code> or <code>scale_y_continuous</code> to set limits for each axis as well.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="co"># find extent of sites to get x and y limits</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="fu">st_bbox</span>(site_ca_proj_sf)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> xmin ymin xmax ymax
-2171214 1424401 -1733924 1802929 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="co"># with coord_sf</span></span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>() <span class="sc">+</span> <span class="co"># don't specify data here since we have multiple data sets to plot</span></span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ca_proj_sf, <span class="at">fill =</span> <span class="cn">NA</span>) <span class="sc">+</span> <span class="co"># specify no fill</span></span>
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ecoreg_sf, <span class="fu">aes</span>(<span class="at">fill =</span> US_L3NAME), <span class="at">color =</span> <span class="st">"grey40"</span>) <span class="sc">+</span> <span class="co"># like other data you can set fill </span></span>
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> site_ca_proj_sf) <span class="sc">+</span></span>
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_discrete</span>(<span class="at">name =</span> <span class="st">"Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span> </span>
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_sf</span>(<span class="at">xlim =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">2171214</span>, <span class="sc">-</span><span class="dv">1733924</span>), <span class="at">ylim =</span> <span class="fu">c</span>(<span class="dv">1424401</span>, <span class="dv">1802929</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid" width="672"></p>
</div>
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="co"># with scale_*_continuous</span></span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>() <span class="sc">+</span> <span class="co"># don't specify data here since we have multiple data sets to plot</span></span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ca_proj_sf, <span class="at">fill =</span> <span class="cn">NA</span>) <span class="sc">+</span> <span class="co"># specify no fill</span></span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ecoreg_sf, <span class="fu">aes</span>(<span class="at">fill =</span> US_L3NAME), <span class="at">color =</span> <span class="st">"grey40"</span>) <span class="sc">+</span> <span class="co"># like other data you can set fill </span></span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> site_ca_proj_sf) <span class="sc">+</span></span>
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_discrete</span>(<span class="at">name =</span> <span class="st">"Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span> </span>
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">2171214</span>, <span class="sc">-</span><span class="dv">1733924</span>)) <span class="sc">+</span></span>
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">1424401</span>, <span class="dv">1802929</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-11-2.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Zooming in is nice, but it does mean you lose some geographic context for the broader region. In Part III I will show you how to create an inset map so you can zoom in on a focal region as well as include a larger region for context.</p>
<p>You can also use <code>facet_wrap()</code> to split your map into smaller maps with any of your variables.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>() <span class="sc">+</span> <span class="co"># don't specify data here since we have multiple data sets to plot</span></span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ca_proj_sf, <span class="at">fill =</span> <span class="cn">NA</span>) <span class="sc">+</span> <span class="co"># must specify "data = " or it will throw an error</span></span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ecoreg_sf, <span class="fu">aes</span>(<span class="at">fill =</span> US_L3NAME)) <span class="sc">+</span> <span class="co"># like other data you can set fill </span></span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> site_ca_proj_sf) <span class="sc">+</span></span>
<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_discrete</span>(<span class="at">name =</span> <span class="st">"Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb39-6"><a href="#cb39-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb39-7"><a href="#cb39-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>desert) <span class="sc">+</span></span>
<span id="cb39-8"><a href="#cb39-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_sf</span>(<span class="at">xlim =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">2171214</span>, <span class="sc">-</span><span class="dv">1733924</span>), <span class="at">ylim =</span> <span class="fu">c</span>(<span class="dv">1424401</span>, <span class="dv">1802929</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Unfortunately, when using <code>geom_sf</code> the <code>scales = "free"</code> option doesn’t work with faceting, so if you want to create a map with something that looks like facets it is probably easier to create two separate maps and combine them using either the <code>patchwork</code> or <code>cowplot</code> package.</p>
</section>
</section>
<section id="part-ii-add-a-basemap" class="level1">
<h1>Part II: Add a Basemap</h1>
<p>Basemaps provide important geographical context for your data and can serve as an important visual references for your maps. In GIS programs it is often pretty easy to add a basemap layer, but in R there are a few additional steps and different packages needed.</p>
<section id="my-favorite-basemap-providers-packages-and-functions" class="level2">
<h2 class="anchored" data-anchor-id="my-favorite-basemap-providers-packages-and-functions">My Favorite Basemap Providers, Packages and Functions</h2>
<p>There are many options for basemaps for your mapping projects in R, as well as several different packages for working with those map layers. Basemaps are provided as a series of tiles from various basemap providers, some are freely available and others require registration and/or a subscription to access them via an API key. Below I list the providers of various basemaps</p>
<p><strong>Basemap Data Providers</strong>:</p>
<p>*: requires registration but has a free tier</p>
<p>**: paid service</p>
<ul>
<li><p>OpenStreetMap</p></li>
<li><p>Stamen via Stadia*</p></li>
<li><p>Thunderforest*</p></li>
<li><p>Carto**</p></li>
<li><p><a href="https://www.mapbox.com/static-maps">Mapbox</a>*</p></li>
<li><p>Google</p></li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="data/basemap_types.png" class="img-fluid figure-img"></p>
<p></p><figcaption class="figure-caption">Image: <a href="https://github.com/riatelab/maptiles">maptiles package documentation</a></figcaption><p></p>
</figure>
</div>
<p><strong>Basemap R Packages</strong></p>
<p>Make it “easy” to download and bring in the basemap tiles to R. In reality, I have found that some packages are much easier than others to work with.</p>
<p>My favorites:</p>
<ul>
<li><p><a href="https://paleolimbot.github.io/ggspatial/index.html"><code>ggspatial</code></a></p>
<ul>
<li><p>uses <code>rosm::osm.image()</code> to display/fetch map tiles</p></li>
<li><p>easy integration with ggplot (returns a ggplot layer)</p></li>
<li><p>can’t directly specify spatial extent, uses your data</p></li>
</ul></li>
<li><p><a href="https://github.com/riatelab/maptiles"><code>maptiles</code></a></p>
<ul>
<li><p>pretty easy to download</p></li>
<li><p>doesn’t</p></li>
</ul></li>
</ul>
<p><strong>Other Packages to Examine</strong></p>
<ul>
<li><p><a href="https://hypertidy.github.io/ceramic/"><code>ceramic</code></a>: another package for webmap tiles, defaults to Mapbox map with other options; visualize with <code>terra</code></p></li>
<li><p><a href="https://github.com/dkahle/ggmap"><code>ggmap</code></a>: requires google maps API key to use, even if tile provider doesn’t require one; visualizes with <code>ggplot</code>; good for getting Google Maps</p></li>
<li><p><a href="https://walker-data.com/mapboxapi/"><code>mapboxapi</code></a>: requires Mapbox account (there is a free tier)</p></li>
<li><p><a href="https://github.com/ifellows/ROSM"><code>OpenStreetMap</code></a>: requires Java installation</p></li>
<li><p><a href="https://github.com/paleolimbot/rosm"><code>rosm</code></a>: not designed to work with <code>ggplot</code>, uses <code>prettymapr</code></p></li>
<li><p><a href="https://github.com/Chrisjb/basemapR"><code>basemapR</code></a>: works with <code>ggplot</code> but not on CRAN</p></li>
<li><p><a href="https://jakob.schwalb-willmann.de/basemaps/"><code>basemaps</code></a>: many formats to download basemaps, but removed from CRAN and currently down’t work for me</p></li>
<li><p><a href="https://github.com/markusloecher/rgooglemaps"><code>rgooglemaps</code></a>: another way to access google maps</p></li>
<li><p><a href="https://michaeldorman.github.io/mapsapi/"><code>mapsapi</code></a>: another interface to google maps API</p></li>
<li><p><a href="https://mikkovihtakari.github.io/ggOceanMaps/"><code>ggOceanMaps</code></a>: designed for ocean sciences, visualizes with <code>ggplot2</code></p>
<ul>
<li>some additional ocean map packages: <code>marmap</code>, <code>oceanmap</code>, <code>oce</code></li>
</ul></li>
</ul>
</section>
<section id="retrieving-your-basemap" class="level2">
<h2 class="anchored" data-anchor-id="retrieving-your-basemap">Retrieving your basemap</h2>
<p>This usually requires you to set an extent or bounding box for your download, as well as specify the zoom level you want.</p>
<p>The zoom is important as if you specify one that is too low (coarse, low resolution), your basemap may appear fuzzy/blurred but if it is too high (fine, high resolution) it may take a very long time to retrieve the necessary tiles.</p>
<p>This table lists some appropriate zoom levels, obtained from the <a href="https://wiki.openstreetmap.org/wiki/Zoom_levels">OpenStreetMap wiki</a>:</p>
<table class="table">
<thead>
<tr class="header">
<th>Zoom</th>
<th># Tiles</th>
<th>Example Area to Represent</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>0</td>
<td>1</td>
<td>Whole World</td>
</tr>
<tr class="even">
<td>3</td>
<td>64</td>
<td>Largest Country</td>
</tr>
<tr class="odd">
<td>5</td>
<td>1024</td>
<td>Large African Country</td>
</tr>
<tr class="even">
<td>6</td>
<td>4096</td>
<td>Large European Country</td>
</tr>
<tr class="odd">
<td>7</td>
<td>16384</td>
<td>Small country, US State</td>
</tr>
<tr class="even">
<td>10</td>
<td>1048576</td>
<td>Metropolitan Area</td>
</tr>
</tbody>
</table>
<p>They go higher, but this gives you a sense of how many more tiles there are to download at different zoom levels.</p>
<section id="using-ggspatial" class="level3">
<h3 class="anchored" data-anchor-id="using-ggspatial">Using <code>ggspatial</code></h3>
<p>What are the built-in basemap options?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>rosm<span class="sc">::</span><span class="fu">osm.types</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "osm" "opencycle" "hotstyle"
[4] "loviniahike" "loviniacycle" "stamenbw"
[7] "stamenwatercolor" "osmtransport" "thunderforestlandscape"
[10] "thunderforestoutdoors" "cartodark" "cartolight" </code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>map_nobase <span class="ot"><-</span> <span class="fu">ggplot</span>() <span class="sc">+</span> <span class="co"># don't specify data here since we have multiple data sets to plot</span></span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ca_proj_sf, <span class="at">fill =</span> <span class="cn">NA</span>) <span class="sc">+</span> <span class="co"># must specify "data = " or it will throw an error</span></span>
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> site_ecoreg_sf, <span class="fu">aes</span>(<span class="at">color =</span> US_L3NAME)) <span class="sc">+</span></span>
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_discrete</span>(<span class="at">name =</span> <span class="st">"Level 3 Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">ncol =</span> <span class="dv">1</span>, <span class="at">title.position =</span> <span class="st">"top"</span>)) <span class="sc">+</span></span>
<span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb42-7"><a href="#cb42-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb42-8"><a href="#cb42-8" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb42-9"><a href="#cb42-9" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.direction =</span> <span class="st">"vertical"</span></span>
<span id="cb42-10"><a href="#cb42-10" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb42-11"><a href="#cb42-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-12"><a href="#cb42-12" aria-hidden="true" tabindex="-1"></a>map_nobase</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid" width="768"></p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>map_wbase <span class="ot"><-</span> <span class="fu">ggplot</span>() <span class="sc">+</span> <span class="co"># don't specify data here since we have multiple data sets to plot</span></span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a> ggspatial<span class="sc">::</span><span class="fu">annotation_map_tile</span>(<span class="at">type =</span> <span class="st">"osm"</span>, <span class="at">zoom =</span> <span class="dv">6</span>) <span class="sc">+</span> </span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> ca_proj_sf, <span class="at">fill =</span> <span class="cn">NA</span>) <span class="sc">+</span> <span class="co"># must specify "data = " or it will throw an error</span></span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">data =</span> site_ecoreg_sf, <span class="fu">aes</span>(<span class="at">color =</span> US_L3NAME)) <span class="sc">+</span></span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_discrete</span>(<span class="at">name =</span> <span class="st">"Level 3 Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">ncol =</span> <span class="dv">1</span>, <span class="at">title.position =</span> <span class="st">"top"</span>)) <span class="sc">+</span></span>
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb43-9"><a href="#cb43-9" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb43-10"><a href="#cb43-10" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.direction =</span> <span class="st">"vertical"</span></span>
<span id="cb43-11"><a href="#cb43-11" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb43-12"><a href="#cb43-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-13"><a href="#cb43-13" aria-hidden="true" tabindex="-1"></a>map_wbase</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Zoom: 6</code></pre>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/ggspatial-basemap-1.png" class="img-fluid figure-img" width="768"></p>
<p></p><figcaption class="figure-caption">Basemap imagery: (C) OpenStreetMap contributors.</figcaption><p></p>
</figure>
</div>
</div>
</div>
<p>Compare the visual with and without the basemap. Aside from the basemap, can you notice any differences?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(patchwork)</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a>map_nobase <span class="sc">+</span> map_wbase</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Zoom: 6</code></pre>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
<p></p><figcaption class="figure-caption">Basemap imagery: (C) OpenStreetMap contributors.</figcaption><p></p>
</figure>
</div>
</div>
</div>
<p>Yes, the projection is different! That is one of the annoying things about using ggspatial - it downloads the basemaps in a specific CRS - the Web Mercator projection (<code>EPSG:3857</code>). If it is the first layer added, then other layers are reprojected to Web Mercator, which is why the two maps look different.</p>
<p>If you want to have the map in the CRS of the data, specify a data argument in the call to <code>ggplot()</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> ca_proj_sf) <span class="sc">+</span> <span class="co"># specify data here to set CRS </span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a> ggspatial<span class="sc">::</span><span class="fu">annotation_map_tile</span>(<span class="at">type =</span> <span class="st">"osm"</span>, <span class="at">zoom =</span> <span class="dv">6</span>) <span class="sc">+</span> </span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">fill =</span> <span class="cn">NA</span>) <span class="sc">+</span> </span>
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(</span>
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> site_ecoreg_sf, </span>
<span id="cb47-6"><a href="#cb47-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">color =</span> US_L3NAME)</span>
<span id="cb47-7"><a href="#cb47-7" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb47-8"><a href="#cb47-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_discrete</span>(<span class="at">name =</span> <span class="st">"Level 3 Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb47-9"><a href="#cb47-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">ncol =</span> <span class="dv">1</span>, <span class="at">title.position =</span> <span class="st">"top"</span>)) <span class="sc">+</span></span>
<span id="cb47-10"><a href="#cb47-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb47-11"><a href="#cb47-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb47-12"><a href="#cb47-12" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb47-13"><a href="#cb47-13" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.direction =</span> <span class="st">"vertical"</span></span>
<span id="cb47-14"><a href="#cb47-14" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Loading required namespace: raster</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Zoom: 6</code></pre>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="768"></p>
<p></p><figcaption class="figure-caption">Basemap imagery: (C) OpenStreetMap contributors.</figcaption><p></p>
</figure>
</div>
</div>
</div>
<p>Now the map is back in the projection of the data, which is helpful if you don’t want to be stuck with only one option for the CRS of your map.</p>
<div class="callout-caution callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Danger
</div>
</div>
<div class="callout-body-container callout-body">
<p>I am still figuring out how R does this under the hood. If the above code involves reprojecting the basemap tiles, what I suspect, as opposed to downloading the tiles in a different CRS I’d be careful as reprojecting involves resampling the raster into a new grid, which may slightly alter the appearance of the basemap.</p>
</div>
</div>
</section>
<section id="using-maptiles" class="level3">
<h3 class="anchored" data-anchor-id="using-maptiles">Using <code>maptiles</code></h3>
<p><code>maptiles</code> has different basemap options than <code>ggspatial</code>. See the help for <code>get_tiles</code> to see all of the options, some of which do require an API key. The default is OpenStreetMap.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb50"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>ca3857 <span class="ot"><-</span> <span class="fu">st_transform</span>(ca_proj_sf, <span class="st">"epsg:3857"</span>)</span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a>ca_osm <span class="ot"><-</span> <span class="fu">get_tiles</span>(ca3857, <span class="at">crop =</span> <span class="cn">TRUE</span>)</span>
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb50-4"><a href="#cb50-4" aria-hidden="true" tabindex="-1"></a>ca_osm</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>class : SpatRaster
dimensions : 273, 236, 3 (nrow, ncol, nlyr)
resolution : 4891.97, 4891.97 (x, y)
extent : -13858950, -12704446, 3830412, 5165920 (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857)
source(s) : memory
colors RGB : 1, 2, 3
names : lyr.1, lyr.2, lyr.3
min values : 35, 35, 35
max values : 252, 252, 250 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb52"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> ca3857) <span class="sc">+</span> </span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_spatraster_rgb</span>(<span class="at">data =</span> ca_osm, <span class="at">r =</span> <span class="dv">1</span>, <span class="at">g =</span> <span class="dv">2</span>, <span class="at">b =</span> <span class="dv">3</span>) <span class="sc">+</span></span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">fill =</span> <span class="cn">NA</span>) <span class="sc">+</span></span>
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(</span>
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> <span class="fu">st_transform</span>(site_ecoreg_sf, <span class="at">crs =</span> <span class="st">"epsg:3857"</span>), </span>
<span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">color =</span> US_L3NAME) </span>
<span id="cb52-7"><a href="#cb52-7" aria-hidden="true" tabindex="-1"></a> <span class="co">#inherit.aes = FALSE # I've found I sometimes need to add this if I am having issues</span></span>
<span id="cb52-8"><a href="#cb52-8" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb52-9"><a href="#cb52-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_discrete</span>(<span class="at">name =</span> <span class="st">"Level 3 Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb52-10"><a href="#cb52-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">ncol =</span> <span class="dv">1</span>, <span class="at">title.position =</span> <span class="st">"top"</span>)) <span class="sc">+</span></span>
<span id="cb52-11"><a href="#cb52-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb52-12"><a href="#cb52-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb52-13"><a href="#cb52-13" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb52-14"><a href="#cb52-14" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.direction =</span> <span class="st">"vertical"</span></span>
<span id="cb52-15"><a href="#cb52-15" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
<p></p><figcaption class="figure-caption">Basemap imagery: (C) OpenStreetMap contributors.</figcaption><p></p>
</figure>
</div>
</div>
</div>
</section>
<section id="additional-basemap-options" class="level3">
<h3 class="anchored" data-anchor-id="additional-basemap-options">Additional basemap options</h3>
<p>In addition to the built-in tile servers, you can also source a basemap from using a URL for an xyz raster source with ggspatial. Make sure you include attributions for the basemap service you use. The <a href="https://wiki.openstreetmap.org/wiki/Raster_tile_providers">OpenStreetMap wiki</a> page has a list of Tile providers and the urls for you to use to access them. Some of these servers require you to register, but many have a free tier for low volume users.</p>
<p>For this to work with <code>ggspatial</code>, you just put the URL in the “type” argument. You also need to make sure that the URL you use has a “$” in front of the {z}, {y}, and {x} otherwise the call returns an error. Additionally, make sure that there is an extension on the URL (.png, .jpg, or .jpeg) - this is also required for the function call to run properly.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb53"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="co"># the OpenTopo map tile provider</span></span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a>open_topo <span class="ot"><-</span> <span class="st">"https://a.tile.opentopomap.org/${z}/${x}/${y}.png"</span></span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> ca_proj_sf) <span class="sc">+</span> <span class="co"># specify data here to set CRS </span></span>
<span id="cb53-5"><a href="#cb53-5" aria-hidden="true" tabindex="-1"></a> ggspatial<span class="sc">::</span><span class="fu">annotation_map_tile</span>(</span>
<span id="cb53-6"><a href="#cb53-6" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> open_topo,</span>
<span id="cb53-7"><a href="#cb53-7" aria-hidden="true" tabindex="-1"></a> <span class="at">zoom =</span> <span class="dv">7</span></span>
<span id="cb53-8"><a href="#cb53-8" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span> </span>
<span id="cb53-9"><a href="#cb53-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">fill =</span> <span class="cn">NA</span>, <span class="at">linewidth =</span> <span class="dv">2</span>, <span class="at">color =</span> <span class="st">"black"</span>) <span class="sc">+</span> <span class="co"># don't need to specify data here since we did in ggplot call</span></span>
<span id="cb53-10"><a href="#cb53-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(</span>
<span id="cb53-11"><a href="#cb53-11" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> site_ecoreg_sf, </span>
<span id="cb53-12"><a href="#cb53-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">color =</span> US_L3NAME) </span>
<span id="cb53-13"><a href="#cb53-13" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb53-14"><a href="#cb53-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_discrete</span>(<span class="at">name =</span> <span class="st">"Level 3 Ecoregion"</span>) <span class="sc">+</span></span>
<span id="cb53-15"><a href="#cb53-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">fill =</span> <span class="fu">guide_legend</span>(<span class="at">ncol =</span> <span class="dv">1</span>, <span class="at">title.position =</span> <span class="st">"top"</span>)) <span class="sc">+</span></span>
<span id="cb53-16"><a href="#cb53-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb53-17"><a href="#cb53-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb53-18"><a href="#cb53-18" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">"bottom"</span>,</span>
<span id="cb53-19"><a href="#cb53-19" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.direction =</span> <span class="st">"vertical"</span></span>
<span id="cb53-20"><a href="#cb53-20" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Zoom: 7</code></pre>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="sf_basemap_workshop_ecodatasci_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="768"></p>
<p></p><figcaption class="figure-caption">Basemap imagery: © OpenStreetMap-Mitwirkende, SRTM | Kartendarstellung: © OpenTopoMap (CC-BY-SA)</figcaption><p></p>
</figure>
</div>
</div>
</div>
<p>You can also do this with the <code>maptiles</code> package using the <code>create_provider</code> function:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="co"># code not run, shown as example: </span></span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a>opentopomap <span class="ot"><-</span> <span class="fu">create_provider</span>(</span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a> <span class="at">name =</span> <span class="st">"otm"</span>,</span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a> <span class="at">url =</span> <span class="st">"https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png"</span>,</span>
<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a> <span class="at">sub =</span> <span class="fu">c</span>(<span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>),</span>
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a> <span class="at">citation =</span> <span class="st">"map data: © OpenStreetMap contributors, SRTM | map style: © OpenTopoMap (CC-BY-SA)"</span></span>
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p><strong>Places to Retrieve Basemaps</strong></p>
<ul>
<li><p><a href="https://documentation.maptiler.com/hc/en-us">maptiler</a>: XYZ rasters, free tier, requires API</p></li>
<li><p><a href="https://xyzservices.readthedocs.io/en/stable/introduction.html">xyzservices</a>: list of xyz sources, some free some require subscriptions</p></li>
<li><p><a href="https://wiki.openstreetmap.org/wiki/Raster_tile_providers">OpenStreetMap wiki</a></p></li>
<li><p>https://www.xyht.com/spatial-itgis/using-openstreetmap-basemaps-qgis-3-0/</p></li>
<li><p>https://github.com/roblabs/xyz-raster-sources</p></li>
</ul>
</section>
<section id="basemap-from-file" class="level3">
<h3 class="anchored" data-anchor-id="basemap-from-file">Basemap from File</h3>
<p>You can also add a basemap from a file in a similar way to the <code>maptiles</code> code, if your basemap layer is a GeoTiff file (.tif). Simply read in your data layer with <code>terra</code> and add it to your plot with <code>tidyterra::geom_spatraster_rgb()</code> or <code>tidyterra::geom_spatraster()</code> if it is a single layer.</p>
<p>Example:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb56"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="co"># This code not run, shown as a demo</span></span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a><span class="co"># load aerial imagery from National Agricultural Imagery Program</span></span>
<span id="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a>naip_tif <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data/naip_imagery.tif"</span>) </span>
<span id="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a>samp_pts_sf <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="st">"data/shapefiles/sample_points.shp"</span>)</span>
<span id="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> samp_pts_sf) <span class="sc">+</span></span>
<span id="cb56-8"><a href="#cb56-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_spatraster_rgb</span>(<span class="at">data =</span> naip_tif) <span class="sc">+</span> </span>
<span id="cb56-9"><a href="#cb56-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
<section id="add-a-scalebar-and-north-arrow" class="level2">
<h2 class="anchored" data-anchor-id="add-a-scalebar-and-north-arrow">Add a scalebar and north arrow</h2>
<p>It is usually helpful to have a scalebar and north arrow for reference and orientation. There are built-in functions in <code>ggspatial</code> to make this easy:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb57"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>ca_osm <span class="ot"><-</span> <span class="fu">get_tiles</span>(ca_proj_sf, <span class="at">crop =</span> <span class="cn">TRUE</span>, <span class="at">zoom =</span> <span class="dv">6</span>, <span class="at">provider =</span> <span class="st">"Esri.WorldImagery"</span>)</span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(<span class="at">data =</span> ca_proj_sf) <span class="sc">+</span> <span class="co"># specify data here to set CRS </span></span>
<span id="cb57-4"><a href="#cb57-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_spatraster_rgb</span>(<span class="at">data =</span> ca_osm) <span class="sc">+</span></span>
<span id="cb57-5"><a href="#cb57-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(<span class="at">fill =</span> <span class="st">"transparent"</span>, <span class="at">color =</span> <span class="st">"black"</span>, <span class="at">linewidth =</span> <span class="dv">1</span>) <span class="sc">+</span> <span class="co"># don't need to specify data here since we did in ggplot call</span></span>
<span id="cb57-6"><a href="#cb57-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_sf</span>(</span>
<span id="cb57-7"><a href="#cb57-7" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> site_ecoreg_sf, </span>