simplevis: making leaflet sf maps

[This article was first published on David Hodge, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

In addition to gglot2 wrapper functions, simplevis also provides leaflet wrapper functions as a bonus.

The way these functions have been designed is to follow the logic of the ggplot2 wrapper functions.

library(simplevis)
library(dplyr)
library(palmerpenguins)

sf objects

The sf package makes it easy to work with vector data (e.g. points, lines or polygons).

sf objects have a list column called geometry on the end of the dataset, as well as some meta data describing the coordinate reference system etc.

example_sf_point
#> Simple feature collection with 112 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1175354 ymin: 4853914 xmax: 2025939 ymax: 6096100
#> Projected CRS: NZGD2000 / New Zealand Transverse Mercator 2000
#> First 10 features:
#>      site_id median trend_category                geometry
#> 1  ARC-00001 0.0140      Improving POINT (1735609 5916179)
#> 2  ARC-00008 0.0610      Improving POINT (1753479 5976281)
#> 3  ARC-00013 0.1310      Improving POINT (1742066 5915382)
#> 4  ARC-00014 0.9900      Improving POINT (1764285 5907017)
#> 5  ARC-00015 1.0300      Improving POINT (1767401 5907336)
#> 6  ARC-00016 0.2980      Improving POINT (1768314 5908177)
#> 7  ARC-00017 0.3550      Improving POINT (1751305 5933319)
#> 8  ARC-00018 0.7350  Indeterminate POINT (1769952 5912814)
#> 9  ARC-00019 0.5000      Improving POINT (1769452 5910614)
#> 10 ARC-00026 0.1295      Improving POINT (1748608 5953465)

simplevis ggplot wrappers

Note that to create sf ggplot2 maps with simplevis:

  • Data must be an sf object
  • Data must be of POINT/MULTIPOINT, LINESTRING/MULTILINESTRING, or POLYGON/MULTIPOLYGON geometry type
  • Data must have a coordinate reference system (CRS) defined
  • No x_var and y_var variables are required
gg_sf_col(example_sf_point,  
          col_var = trend_category, 
          borders = nz)

These maps can be facetted or made interactive in the same way as other ggplot2 objects with plotly::ggplotly.

The borders argument is any sf object of any sort of administrative or natural contextial borders and boundaries that you would like for your map. In the example above, a New Zealand coastline sf object has been provided to the borders argument.

simplevis leaflet wrappers

The simplevis leaflet wrappers largely work in exactly the same way.

leaflet_sf_col(example_sf_point, 
               col_var = median, 
               col_method = "quantile",
               col_cuts = seq(0, 1, 0.25),
               title = "Monitored medians, 2008-17")
leaflet_sf_col(example_sf_polygon, density,
     col_method = "bin", 
     col_cuts = c(0, 10, 50, 100, 150, 200, Inf),
     title = "Modelled density, 2017")

The clickable popup will default to a leafpop::popupTable of all variables, but popups can be adjusted to a subset of column using the popup_vars_vctr argument.

leaflet_sf_col(example_sf_point, 
               col_var = trend_category, 
               popup_vars_vctr = c("site_id", "median"))