Playing with Mapboxer Pkg

Playing with the Mapboxer Pkg

maps
Author

Kevn Gilds, MPA

Published

2024-07-20

I signed up for a workshop on working with the mapboxer package and wanted to ensure I got it installed on my computer and play with it prior to the workshop.

This blog post is following the guide for the package

#Load the library
library(mapboxer)

The guide uses a pre established data set. Lets take a peak at the data set.

head(motor_vehicle_collisions_nyc)
        date time       lng      lat injured killed
1 2020-09-24 0:00 -73.99842 40.68986       0      0
2 2020-09-24 0:00 -73.88852 40.67298       0      0
3 2020-09-24 0:00 -73.88636 40.63082       0      0
4 2020-09-24 0:00 -73.97634 40.64773       1      0
5 2020-09-24 0:00 -73.88390 40.83948       1      0
6 2020-09-24 0:00 -73.82484 40.78214       1      0

Number of People Injured

# Create a source
motor_vehicle_collisions_nyc %>%
  dplyr::mutate(color = ifelse(injured > 0, "red", "yellow")) %>%
  as_mapbox_source(lng = "lng", lat = "lat") %>%
  # Setup a map with the default source above
  mapboxer(
    center = c(-73.9165, 40.7114),
    zoom = 10
  ) %>%
  # Add a navigation control
  add_navigation_control() %>%
  # Add a layer styling the data of the default source
  add_circle_layer(
    circle_color = c("get", "color"),
    circle_radius = 3,
    # Use a mustache template to add popups to the layer
    popup = "Number of persons injured: {{injured}}"
  )

Number of People Killed

mapboxer(
  style = basemaps$Carto$dark_matter,
  center = c(-73.9165, 40.7114),
  zoom = 9,
  minZoom = 8
)
motor_vehicle_collisions_nyc %>%
  dplyr::filter(killed > 0) %>%
  as_mapbox_source() %>%
  mapboxer(
    center = c(-73.9165, 40.7114),
    zoom = 9
  ) %>%
  add_circle_layer(circle_color = "red",
 # Use a mustache template to add popups to the layer
    popup = "Number of persons killed: {{killed}}"
  )

Conclusion

The add circle layer part of the code is different from typical R code. The concept is explained in the Experssions part of the guide.

expr_get_property <- c("get", "<data-property>")

Overall this looks like a fantastic mapping package.


About

Kevin is a nonprofit data professional operating out of Lakeland, Florida.
My expertise is helping nonprofits collect, manage and analyze their program data.