addTrajPaths {openairmaps} | R Documentation |
Add trajectory paths to leaflet map
Description
This function is similar (but not identical to) the leaflet::addMarkers()
function in leaflet
, which allows users to add trajectory paths to any
leaflet map and have more control over groups and layerIds than in
"all-in-one" functions like trajMap()
.
Usage
addTrajPaths(
map,
lng = "lon",
lat = "lat",
layerId = NULL,
group = NULL,
data = leaflet::getMapData(map),
npoints = 12,
...
)
Arguments
map |
a map widget object created from |
lng |
The decimal longitude. |
lat |
The decimal latitude. |
layerId |
The base string for the layer id. The actual layer IDs will be in the format "layerId-linenum" for lines and "layerId_linenum-pointnum" for points. For example, the first point of the first trajectory path will be "layerId-1-1". |
group |
the name of the group the newly created layers should belong to
(for |
data |
Data frame, the result of importing a trajectory file using
|
npoints |
A dot is placed every |
... |
Other arguments to pass to both |
Details
addTrajPaths()
can be a powerful way of quickly plotting
trajectories on a leaflet map, but users should take some care due to any
additional arguments being passed to both leaflet::addCircleMarkers()
and
leaflet::addPolylines()
. In particular, users should be weary of the use
of the color
argument. Specifically, if color
is passed a vector of
length greater than one, multiple polylines will be drawn on top of one
another. At best this will affect opacity, but at worst this will
significantly impact the performance of R and the final leaflet map.
To mitigate this, please ensure that any vector passed to color
is of
length one. This is simple if you want the whole path to be the same
colour, but more difficult if you want to colour by a pollutant, for
example. The easiest way to achieve this is to write a for loop or use
another iterative approach (e.g. the purrr
package) to add one path per
arrival date. An example of this is provided in the Examples.
Value
A leaflet object.
See Also
shiny::runExample(package = "openairmaps")
to see examples of this
function used in a shiny::shinyApp()
Examples
## Not run:
library(leaflet)
library(openairmaps)
pal <- colorNumeric(palette = "viridis", domain = traj_data$nox)
map <- leaflet() %>%
addTiles()
for (i in seq(length(unique(traj_data$date)))) {
data <- dplyr::filter(traj_data, date == unique(traj_data$date)[i])
map <- map %>%
addTrajPaths(
data = data,
color = pal(data$nox)[1]
)
}
map
## End(Not run)