/
markers.Rmd
181 lines (135 loc) · 6.71 KB
/
markers.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
---
title: "Add markers to leaflet"
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r,echo=FALSE}
library(leaflet)
```
Use markers to call out points on the map. Marker locations are expressed in latitude/longitude coordinates, and can either appear as icons or as circles.
# Data sources
Point data for markers can come from a variety of sources:
- `SpatialPoints` or `SpatialPointsDataFrame` objects (from the `sp` package)
- `POINT`, `sfc_POINT`, and `sf` objects (from the `sf` package); only `X` and `Y` dimensions will be considered
- Two-column numeric matrices (first column is longitude, second is latitude)
- Data frame with latitude and longitude columns. You can explicitly tell the marker function which columns contain the coordinate data (e.g. `addMarkers(lng = ~Longitude, lat = ~Latitude)`), or let the function look for columns named `lat`/`latitude` and `lon`/`lng`/`long`/`longitude` (case insensitive).
- Simply provide numeric vectors as `lng` and `lat` arguments
Note that `MULTIPOINT` objects from `sf` are not supported at this time.
# Icon Markers
Icon markers are added using the `addMarkers()` or the `addAwesomeMarkers()` functions. Their default appearance is a dropped pin. As with most layer functions, the `popup` argument can be used to add a message to be displayed on click, and the `label` option can be used to display a text label either on hover or statically.
```{r}
data(quakes)
# Show first 20 rows from the `quakes` dataset
leaflet(data = quakes[1:20,]) %>% addTiles() %>%
addMarkers(~long, ~lat, popup = ~as.character(mag), label = ~as.character(mag))
```
## Customizing Marker Icons
You can provide custom markers in one of several ways, depending on the scenario. For each of these ways, the icon can be provided as either a URL or as a file path.
For the simple case of applying a single icon to a set of markers, use `makeIcon()`.
```{r fig.height=1.75}
greenLeafIcon <- makeIcon(
iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "https://leafletjs.com/examples/custom-icons/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 64,
shadowAnchorX = 4, shadowAnchorY = 62
)
leaflet(data = quakes[1:4,]) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = greenLeafIcon)
```
If you have several icons to apply that vary only by a couple of parameters (i.e. they share the same size and anchor points but have different URLs), use `icons()` `icons()` performs similarly to `data.frame()`, in that any arguments that are shorter than the number of markers will be recycled to fit.
```{r fig.height=2}
quakes1 <- quakes[1:10,]
leafIcons <- icons(
iconUrl = ifelse(quakes1$mag < 4.6,
"https://leafletjs.com/examples/custom-icons/leaf-green.png",
"https://leafletjs.com/examples/custom-icons/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "https://leafletjs.com/examples/custom-icons/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 64,
shadowAnchorX = 4, shadowAnchorY = 62
)
leaflet(data = quakes1) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons)
```
Finally, if you have a set of icons that vary in multiple parameters, it may be more convenient to use `iconList()`. It lets you create a list of (named or unnamed) `makeIcon()` icons, and select from that list by position or name.
```{r fig.height=1.75}
# Make a list of icons. We'll index into it based on name.
oceanIcons <- iconList(
ship = makeIcon("ferry-18.png", "ferry-18@2x.png", 18, 18),
pirate = makeIcon("danger-24.png", "danger-24@2x.png", 24, 24)
)
# Some fake data
df <- sp::SpatialPointsDataFrame(
cbind(
(runif(20) - .5) * 10 - 90.620130, # lng
(runif(20) - .5) * 3.8 + 25.638077 # lat
),
data.frame(type = factor(
ifelse(runif(20) > 0.75, "pirate", "ship"),
c("ship", "pirate")
))
)
leaflet(df) %>% addTiles() %>%
# Select from oceanIcons based on df$type
addMarkers(icon = ~oceanIcons[type])
```
## Awesome Icons
Leaflet supports even more customizable markers using the [awesome markers](https://github.com/lennardv2/Leaflet.awesome-markers) leaflet plugin.
The `addAwesomeMarkers()` function is similar to `addMarkers()` function but additionally allows you to specify custom colors for the markers as well as icons from the [Font Awesome](https://fontawesome.com/icons), [Bootstrap Glyphicons](https://getbootstrap.com/components/), and [Ion icons](https://ionic.io/ionicons) icon libraries.
Similar to `makeIcon()`, `icons()`, and `iconList()` described above, you have `makeAwesomeIcon()`, `awesomeIcons()` and `awesomeIconList()` functions, which enable you to add awesome icons.
```{r}
# first 20 quakes
df.20 <- quakes[1:20,]
getColor <- function(quakes) {
sapply(quakes$mag, function(mag) {
if(mag <= 4) {
"green"
} else if(mag <= 5) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(df.20)
)
leaflet(df.20) %>% addTiles() %>%
addAwesomeMarkers(~long, ~lat, icon=icons, label=~as.character(mag))
```
The `library` argument has to be one of 'ion', 'fa', or 'glyphicon'. The `icon` argument needs to be the name of any valid icon supported by the the respective library (w/o the prefix of the library name).
## Marker Clusters
When there are a large number of markers on a map, you can cluster them using the [Leaflet.markercluster](https://github.com/Leaflet/Leaflet.markercluster) plug-in. To enable this plug-in, you can provide a list of options to the argument `clusterOptions`, e.g.
```{r fig.height=2.5, message=FALSE}
leaflet(quakes) %>% addTiles() %>% addMarkers(
clusterOptions = markerClusterOptions()
)
```
Using the `freezeAtZoom` argument of `markerClusterOptions()`, you can set the clustering to freeze as a specific zoom level. For example `markerClusterOptions(freezeAtZoom = 5)` will freeze the cluster at zoom level 5 regardless of the user's actual zoom level.
# Circle Markers
Circle markers are much like regular circles (see [Lines and Shapes](https://rstudio.github.io/leaflet/articles/shapes.html)), except that their radius in onscreen pixels stays constant regardless of zoom level.
You can use their default appearance:
```{r fig.height=1.75}
leaflet(df) %>% addTiles() %>% addCircleMarkers()
```
Or customize their color, radius, stroke, opacity, etc.
```{r fig.height=1.75}
# Create a palette that maps factor levels to colors
pal <- colorFactor(c("navy", "red"), domain = c("ship", "pirate"))
leaflet(df) %>% addTiles() %>%
addCircleMarkers(
radius = ~ifelse(type == "ship", 6, 10),
color = ~pal(type),
stroke = FALSE, fillOpacity = 0.5
)
```