-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-webmaps-in-R-tutorial.Rmd
1693 lines (1068 loc) · 38.1 KB
/
leaflet-webmaps-in-R-tutorial.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "WebMaps in R with Leaflet"
author: "Patty Frontiera"
date: "Spring 2019"
output:
html_document:
toc: true
number_sections: false
toc_float: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## WebMaps in R with Leaflet
Welcome! While we're waiting:
* Please download the workshop files from: [https://github.com/dlab-geo/r-leaflet-workshop](https://github.com/dlab-geo/r-leaflet-workshop)
* Unzip the Zipfile: [https://github.com/dlab-geo/r-leaflet-workshop/archive/master.zip](https://github.com/dlab-geo/r-leaflet-workshop/archive/master.zip)
* Open RStudio
* Open a new R script file
## Workshop Outline
**Introductions**
####**Basic Maps**
* Marker Maps
* Adding Popups
####**Data Maps**
* Symbology for mapping point data
* Choropleth maps
* Adding Legends
####**Doing More**
* Customizing the UI
* Sharing your maps
# Leaflet
## Leaflet
[Leaflet](http://leafletjs.com/) is a lightweight, yet powerful javascript library for creating interactive web maps.
<img src="http://leafletjs.com/docs/images/logo.png" width="500px"></img>
Leaflet maps are a combination of **HTML** and **Javascript** code that is meant to be rendered in a web browser.
You can use the R **leaflet** package to create Leaflet maps in R without knowing HTML and Javascript!
## Leaflet in R Workflow
1. Find the geospatial data that you want to map.
2. Use the `leaflet` R package to make a custom, interactive map of your data.
3. Save your leaflet map to an `html` file.
4. View your html file locally in your web browser.
4. Share your map by emailing the html file or hosting it on a website.
## Why the package `leaflet`?
There are a number of R packages for making Leaflet maps.
- **leaflet** is actively maintained, highly customizable, and integrates well with other R libraries.
- **tmap** is easier for getting started *but less customizeable*
- also great for interactive data analysis
- also great for static maps (similar to ggplot2)
- *tmap may be a better option for some folks!*
- we cover it in our `Geospatial Data in R` workshop series.
## Getting Started
Download workshop files: [https://github.com/dlab-geo/r-leaflet-workshop](https://github.com/dlab-geo/r-leaflet-workshop)
Follow along:
* *Tutorial Page* (leaflet-webmaps-in-R.html)
* *Raw code* (leaflet-webmaps-in-R.R)
* *Slides* (leaflet-webmaps-in-R-slides.html)
*Make sure you can copy and paste from one of the above into the script.*
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=8, echo=TRUE, warning=FALSE, message=FALSE)
```
## Load R Packages
Load the packages we will use today
```{r, message=FALSE, warning=FALSE}
library(leaflet)
library(RColorBrewer)
library(sp)
library(rgdal)
library(htmlwidgets)
library(dplyr)
```
*Install any packages that you do not have on your computer*
## Set working directory
to the folder in which you unzipped the workshop files
<img src="./data/swd.png" width="600px"></img>
# Our First Leaflet Map
## Our first Leaflet map
```{r,eval=F}
map1 <- leaflet() # Initialize the leaflet map object
map1 <- addTiles(map1) # Add basemap - default is OpenStreetMap
map1 # Display the map
```
**WARNING** don't call your map object `map`
## Our first Leaflet map
```{r}
map1 <- leaflet() # Initialize the leaflet map object
map1 <- addTiles(map1) # Add basemap - default is OpenStreetMap
map1 # Display the map
```
## Piping Syntax
Its very common use `piping` or `chaining` syntax....
The output of one command becomes the input of the next command.
```{r, eval=F}
map1 <- leaflet() %>% # Initialize the leaflet map object
addTiles() # Add basemap - default is OpenStreetMap
map1 # Display the map
```
## Setting the Map's Intial View
Use **setView** to specify the `center` of the map and the initial `zoom` level.
```{r, message=FALSE, results="hide"}
map1 <- leaflet() %>%
addTiles() %>%
setView(lat=37.87004, lng=-122.25817, zoom = 15)
map1
```
## Setting the view
```{r}
map1 # setView(lat=37.87004, lng=-122.25817, zoom = 15)
```
## Challenge
Create a leaflet map centered on San Francisco with an intial zoom level of 14.
- You can get the coordinates by right clicking in Google Maps and selecting **what's here**
**Questions**
- What zoom level shows all of the city without much more?
- What is the max possible zoom level?
## Challenge - Solution
The maximum zoom level is 18
```{r}
leaflet() %>%
addTiles() %>%
setView(lat=37.76175, lng=-122.4470, zoom = 12)
```
## Maximum Control
How does this code limit the map?
```{r}
map1 <- leaflet(options=leafletOptions(minZoom=15, maxZoom=18)) %>%
addTiles() %>%
setView(lat=37.87004, lng=-122.25817, zoom = 16) %>%
setMaxBounds(-122.2570, 37.866458, -122.2553,37.877167)
map1
```
## Basemaps
By default, Leaflet uses the [OpenStreetMap](https://www.openstreetmap.org/#map=5/38.007/-95.844) basemap, which is added with the `addTiles()` function
```{r, message=F}
leaflet() %>% addTiles() %>%
setView(lat=37.870, lng=-122.258, zoom = 15)
```
## addProviderTiles
Use `addProviderTiles` with the name of the `basemap` to add a different basemap.
Create a leaflet map with the `ESRI World Street Map` basemap.
```{r}
map2 <- leaflet() %>%
addProviderTiles("Esri.WorldStreetMap") %>%
setView(lat=37.870044, lng=-122.258169, zoom = 12)
```
## View it
```{r}
map2 #Using ESRI WorldStreetMap basemap
```
## Specify a Basemap
Add a different basemap by taking a look at this web page of available basemaps
[http://leaflet-extras.github.io/leaflet-providers/preview/](http://leaflet-extras.github.io/leaflet-providers/preview/)
Use the `provider name` in quotes to access the basemap.
## CartoDB Positron Basemap
```{r}
leaflet() %>% addProviderTiles("CartoDB.Positron") %>%
setView(lat=37.870044, lng=-122.258169, zoom = 12)
```
## Getting Help
For more info, read the documentation
```{r, eval=F}
?addProviderTiles
```
# Mapping Data
## Mapping Data
<img src="images/bart_service_map.png" width="800px"></img>
## Map Point Data with Markers
**Barrows Hall**, longitude=-122.25817, latitude=37.87004
```{r, echo=F}
map3 <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
#setView(lat=37.870044, lng=-122.258169, zoom = 17) %>%
addMarkers(lat=37.87004, lng=-122.25817, popup="Barrows Hall")
map3
```
## addMarkers
Use `addMarkers` to add one or more data points to the map.
```{r, eval=F}
map3 <- leaflet() %>%
addTiles() %>%
addMarkers(lat=37.87004, lng=-122.25817, popup="Barrows Hall")
map3
```
## addMarkers
Markers show location and a little information
```{r, echo=FALSE}
map3 <- leaflet() %>%
addTiles() %>%
addMarkers(lat=37.870044, lng=-122.258169, popup="Barrows Hall")
map3
```
## Review the Code
```{r, eval=F}
map3 <- leaflet() %>%
addTiles() %>%
#setView(lat=37.870044, lng=-122.258169, zoom = 17) %>%
addMarkers(lat=37.87004, lng=-122.25817, popup="Barrows Hall")
map3
```
- You don't need to use `setView` (commented out).
- The map will automatically center on the marker data and determine the zoom level.
- But, you can override this with `setView`.
- *What does `popup=` do?*
## addMarkers Documentation
You can always check the function documentation to see your options!
`?addMarkers`
## Challenge
How would you add a second marker for Cafe Milano?
- 37.868641, -122.258537
- Try it?
## Two Markers
```{r}
map3 <- leaflet() %>%
addTiles() %>%
addMarkers(lat=37.87004, lng=-122.25817, popup="Barrows Hall") %>%
addMarkers(lat=37.86850, lng=-122.25830, popup="Cafe Milano")
map3
```
## Two Markers - another way
```{r}
map3 <- leaflet() %>%
addTiles() %>%
addMarkers(lat = c(37.87004,37.86850),
lng = c(-122.25817,-122.25830),
popup = c("Go Bears", "Cafe Milano"))
map3
```
# Questions?
# Mapping Data Frames
## Bart Stations
Source: [Caltrans GIS Data](http://www.dot.ca.gov/hq/tsip/gis/datalibrary/Metadata/BART_13.html)
```{r}
bart <- read.csv('data/bart.csv', stringsAsFactors = FALSE)
str(bart)
```
*What column(s) contain the geographic data?*
## Mapping Bart Stations as Markers
```{r, results="hide"}
map4 <- leaflet() %>%
addTiles() %>%
addMarkers(lat=bart$Y, lng=bart$X,
popup= paste("Station:", bart$STATION))
map4
```
## Mapping Bart Stations as Markers
```{r}
map4
```
## Geographic Data Files
Point data are often stored in `CSV` files and loaded into R data frames.
These point data are manipulated like other R data frames.
Leaflet can map these data if the points use geographic coordinates (longitude & latitude).
More complex geographic data are commonly stored in `ESRI Shapefiles`.
To get the most out of spatial data in R you should use packages specifically for working with spatial data.
# Spatial Data in R
## Spatial Data in R
We can use the `sp` and `rgdal` packages to import, manipulate and map more complex spatial objects.
`sp` - **R classes and methods for spatial data**
`rgdal` - **Functions for importing and transforming spatial data**
Let's use these to import data in ESRI Shapefiles
## Read in an ESRI Shapefile
`BART Lines`
Source: [Caltrans GIS Data](http://www.dot.ca.gov/hq/tsip/gis/datalibrary/Metadata/BART_13.html)
```{r}
dir("data/BART_13/")
```
## BART Lines
Use `rgdal` to read in the data from a Shapefile
```{r}
library(rgdal)
bart_lines <- readOGR(dsn="data/BART_13",layer="BART_13")
```
## BART Lines
Take a look at the data
```{r}
summary(bart_lines)
```
## BART Lines
Use `addPolyLines` to add linear features
```{r, results="hide"}
map4 <- leaflet() %>%
addTiles() %>%
addMarkers(lat=bart$Y, lng=bart$X,
popup= paste("Station:", bart$STATION)) %>%
addPolylines(data=bart_lines, color="black", weight=3)
map4
```
*Leaflet can map both data frames & spatial objects!*
## BART Stations & Lines
```{r}
map4
```
## BART Service Areas
Let's add BART Service Area data, a subset from the [Metropolitan Transportation Commission (MTC)](http://opendata.mtc.ca.gov) Transit Service Area data.
```{r}
dir("data/Transit_Service_Areas_2016")
```
## BART Service Areas
Let's add BART Service Area data from the [Metropolitan Transportation Commission (MTC)](http://opendata.mtc.ca.gov)
```{r}
bart_service <- readOGR(dsn="data/Transit_Service_Areas_2016",layer="bart_service_area")
```
## BART Service Areas
Take a look at the data
```{r}
summary(bart_service)
```
## BART Service Areas
Use `addPolygons` to add area features
```{r, results="hide"}
map4 <- leaflet() %>%
setView(lat=37.857900, lng=-122.245156, zoom = 12) %>%
addTiles() %>%
addMarkers(lat=bart$Y, lng=bart$X,
popup= paste("Station:", bart$STATION)) %>%
addPolylines(data=bart_lines, color="black", weight=4) %>%
addPolygons(data=bart_service, color="blue", opacity = 0.6)
map4
```
## BART Stations, Lines & Service Areas
```{r}
map4
```
## CHALLENGE
Redo the previous map and:
- change the basemap to "CartoDB.Positron"
- set the default view to center on San Francisco
## Challenge Solution
```{r}
map4 <- leaflet() %>%
setView(lat=37.76175, lng=-122.4470, zoom = 12) %>%
addProviderTiles("CartoDB.Positron") %>%
addMarkers(lat=bart$Y, lng=bart$X,
popup= paste("Station:", bart$STATION)) %>%
addPolylines(data=bart_lines, color="black", weight=4) %>%
addPolygons(data=bart_service, color="blue", opacity = 0.6)
```
## Challenge Solution
```{r}
map4
```
# Questions?
## A note about working with Geospatial Data
All geospatial data are referenced to a `coordinate reference system`, or `CRS`.
- A CRS is also referred to as a `projection` or `map projection`
A discussion of `CRSs` is beyond the scope of this workshop!
- Please take our `Geospatial Data in R` workshop series to learn more.
CRSs and R Leaflet
- Leaflet package expects data to be specified in latitude and longitude coordinates and assumes the using WGS 84 (a.k.a. EPSG:4326) CRS.
- See the [Leaflet for R tutorial](https://rstudio.github.io/leaflet/projections.html) for more information.
# Saving your maps
## Save the Map
Save the map as an HTML file that you can then open in a browser, share with friends, put online.
You will need to have the `htmlwidgets` package installed and loaded to save to HTML.
```{r}
#library(htmlwidgets)
saveWidget(map4, file="bartmap.html")
```
# Questions?
# Part II: Mapping larger datasets
## Mapping SF Property Data
[San Francisco Open Data Portal](https://data.sfgov.org)
[SF Property Tax Rolls](https://data.sfgov.org/Housing-and-Buildings/Assessor-Historical-Secured-Property-Tax-Rolls/wv5m-vpq2)
This data set includes the Office of the Assessor-Recorder’s secured property tax roll spanning from 2015.
We are using this as a proxy for home values.
We are working with a simplified version of the full data set.
## Load the CSV file into a data frame
*Set your working directory first to the folder where you downloaded the workshop files!*
```{r }
sfhomes <- read.csv('data/sfhomes15.csv', stringsAsFactors = FALSE)
str(sfhomes)
```
## Explore the data
```{r }
head(sfhomes)
```
## Map the data
```{r}
map4 <- leaflet() %>%
addTiles() %>%
addMarkers(lat=sfhomes$lat, lng=sfhomes$lon,
popup= paste("Address:", sfhomes$Address,
"<br>", # add line break
"Property Value: ", sfhomes$Value))
```
## Display the Map
```{r}
map4
```
## Popups Made Easier
We can add to and save the popup code and re-use it instead of typing it over and over again.
```{r}
popup_content <- paste("<b>Address:</b>", sfhomes$Address,"<br>",
"<b>Property Value</b>: ", sfhomes$Value, "<br>",
"<b>Neighborhood:</b> ", sfhomes$Neighborhood, "<br>",
"<b>Num Bedrooms: </b>", sfhomes$NumBeds, "<br>",
"<b>Num Bathrooms:</b>", sfhomes$NumBaths
)
```
```{r}
map4 <- leaflet() %>%
addTiles() %>%
addMarkers(lat=sfhomes$lat, lng=sfhomes$lon,
popup= popup_content)
```
## Customizing the Popup
```{r}
map4
```
## Shorter syntax
Instead of this:
```{r, eval=F}
leaflet() %>%
addTiles() %>%
addMarkers(lat=sfhomes$lat, lng=sfhomes$lon, popup= popup_content)
```
We can use this syntax:
```{r, eval=F}
leaflet(sfhomes) %>%
addTiles() %>%
addMarkers(~lon, ~lat, popup = popup_content)
```
When the addMarkers function arguments `lng=` and `lat=` are not named they must be in the expected order (longitude, latitude)!
## Too Many Markers!
Read the `addMarker` documentation for options to address this.
```{r, eval=FALSE}
addMarkers(map, lng = NULL, lat = NULL, layerId = NULL,
group = NULL, icon = NULL, popup = NULL,
options = markerOptions(),
clusterOptions = NULL, clusterId = NULL,
data = getMapData(map))
```
## Cluster Option
```{r}
map4 <- leaflet(sfhomes) %>%
addTiles() %>%
addMarkers(~lon, ~lat, popup= popup_content,
clusterOptions = 1)
```
## Cluster Option
```{r}
map4 # Explore the Map - hover over a cluster marker, zoom in.
```
## Mapping Points as Circles
`addCircleMarker`
```{r}
map4 <- leaflet(sfhomes) %>%
addTiles() %>%
addCircleMarkers(~lon, ~lat, popup = popup_content)
```
## Mapping Points as Circles
`addCircleMarker`
```{r}
map4
```
## addCircleMarkers
```{r, eval=F}
addCircleMarkers(map, lng = NULL, lat = NULL, radius = 10,
layerId = NULL, group = NULL, stroke = TRUE, color = "#03F",
weight = 5, opacity = 0.5,
fill = TRUE, fillColor = color, ....)
```
## Customize the circleMarkers
Change color, radius and stroke weight of circle markers
```{r}
map4 <- leaflet(sfhomes) %>%
addTiles() %>%
addCircleMarkers(~lon, ~lat, popup = popup_content,
color="white", radius=6, weight=2, # stroke
fillColor="red",fillOpacity = 0.75 # fill
)
```
- Use `colors()` to see a list of all R named colors.
- You can expand on these with [hexidecimal color values](http://www.color-hex.com).
## Customize the circleMarkers
```{r }
map4
```
## Question
Can you cluster `circleMarkers`?
## Recap
Basic leaflet map with a basemap
Mapping point locations with Markers
Mapping linear features with addPolylines
Mapping polygon (or area) features with addPolygons
Clustering lots of point features
Reading in data from CSV and Spatial Data Files
Working with Popups
# Data Driven Symbology
## Data Driven Symbology
Not just interested in location
Interested in how data values vary by location
Use data values to determine the `size` and `color` of symbols.
## Mapping Data by Size
We can map points & lines by making their size a function of a data value.
**Earthquakes by Magnitude, 1989 - 2019**
<img src="images/bayarea_quakes.png" height="400px"></img>
## Fetch Data to map
```{r}
quake_url <-"http://earthquake.usgs.gov/fdsnws/event/1/query?format=csv&minmagnitude=3.5"
startdate <- "1989-01-01"
enddate <- "2019-01-31"
#
bayminlat <- 36.433
baymaxlat <- 38.694
bayminlon <- -123.865
baymaxlon <- -120.951
quake_url <- paste0(quake_url, "&starttime=", startdate, "&endtime=", enddate,
"&minlatitude=",bayminlat, "&maxlatitude=", baymaxlat,
"&minlongitude=", bayminlon,"&maxlongitude=",baymaxlon)
quakes <- read.csv(quake_url)
#print(quake_url)
```
## Mapping Points by size
Set the radius of the circle to be a function of the magnitude of the earthquake.
```{r}
quake_map <- leaflet(quakes) %>%
addProviderTiles("Esri.WorldTopoMap") %>%
addCircleMarkers(~longitude, ~latitude,
popup=paste0("Magnitude: ", quakes$mag, "<br>Date: ", quakes$time),
fillColor= "Red", color="Red", weight=1, fillOpacity = 0.25,
radius= 1.75^quakes$mag #exponential
)
```
## Earthquakes by Magnitude
With `addCircleMarkers` the radii are **pixels** so they adjust dynamically with the map.
```{r, echo=FALSE}
quake_map
```
## addCircles
With `addCircle` radii are specified absolutely in **meters**
```{r}
quake_map <- leaflet(quakes) %>%
addProviderTiles("Esri.WorldTopoMap") %>%
addCircles(~longitude, ~latitude,
fillColor= "Red", color="black", weight=1, fillOpacity = 0.25,
popup=paste0("Magnitude: ", quakes$mag, "<br>Date: ", quakes$time),
radius= 5000 # 5 kilometer neighborhood
)
```
## addCircles
Use `addCircle` when you want to map specific not relative size.
*5 KM radius around earthquake epicenters*
```{r, echo=FALSE}
quake_map
```
## Layering addCircleMarkers and addCircles
```{r}
quake_map <- leaflet(quakes) %>%
addProviderTiles("Esri.WorldTopoMap") %>%
addCircles(~longitude, ~latitude,
color="black", fillColor= "Red", weight=1, fillOpacity = 0.25,
radius= 5000 # 5 kilometer neighborhood
) %>%
addCircleMarkers(~longitude, ~latitude,
popup=paste0("Magnitude: ", quakes$mag, "<br>Date: ", quakes$time),
color="black", fillColor="black",fillOpacity=1,
radius=4
)
```
## addCircleMarkers and addCircles
```{r, echo=FALSE}
quake_map
```
# Questions?
# Mapping Data by Color
## Color
<img width="800px" src="https://c1.staticflickr.com/9/8341/29158196811_b6c1734cc8_b.jpg">
## Mapping Data by Color
Data values & Color palettes
```{r, echo=FALSE}
numColor_function <- colorNumeric("YlOrRd", sfhomes$Value)
map1<- leaflet(sfhomes) %>%
addProviderTiles("CartoDB.Positron") %>%
addCircleMarkers(~lon, ~lat, popup=popup_content,
fillColor= ~numColor_function(Value),
radius=6, color="grey", weight=1, fillOpacity = 1
) %>%
addLegend(title = "Property Values", pal = numColor_function,
values = ~Value, opacity = 1,
position="bottomleft")
map1
```
## Mapping Data by Color
Data Binning & Classification methods
<table border=0>
<tr>
<td><img src="images/prop_color_map.png" width="380px"></img></td>
<td><img src="images/quant_color_map.png" width="400px"></img></td>
</tr>
</table>
# Color Palettes
## Qualitative Palettes
emphasize different categories with contrasting colors
```{r}
display.brewer.all(type="qual")
display.brewer.pal(7, "Set3" ) # Try a different number of colors
```
## Sequential Palettes
highlight trends in numerical data
```{r}
display.brewer.all(type="seq")
```
## Diverging Palettes
highlight outliers
```{r}
display.brewer.all(type="div")
```
# Color Palettes and Data Values
## Map Homes by Neighborhood
Let's map `sfhomes` by the values in the `Neighborhood` column.
First, check out the RColorBrewer qualitative color palettes
```{r}
display.brewer.all(type="qual")
```
## Associate Colors with Data
`colorfactor` takes as input a color palette and a *domain* that contains the full range of possible values to be mapped.
`colorfactor` returns a **function** specific to that domain that can be used to output a set of color values.
```{r, message=T, warning=T}
# Create a qualitative color palette
myColor_function <- colorFactor("Paired", sfhomes$Neighborhood)
```
## Using colorFactor Function
```{r}