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"))

The hover label will default to the colour variable, but can be adjusted using the label_var variable.

leaflet_sf_col(example_sf_point, 
               col_var = trend_category, 
               label_var = site_id)

Users have a basemap argument that defaults to “light”, but there are other options.

leaflet_sf(example_sf_point, 
           basemap = "dark")

Adding additional layers

As a leaflet object is produced, you can add additional layers with leaflet – although this may effect popups and labels.

leaflet_sf_col(example_sf_point, 
               col_var = trend_category) %>% 
  leaflet::addPolygons(data = nz, 
                       color = "#35B779", 
                       weight = 3, 
                       fillOpacity = 0, 
                       opacity = 1)

leaflet basemap stack for shiny

A leaflet basemap stack is available for use in shiny apps. It defaults to the top layer being “light”. You can set the bounds by adding a vector or bbox of bounds.

leaflet_basemap(bounds = c(166.70047,-34.45676, 178.52966,-47.06345))

You can also specify the top layer.

library(sf)
leaflet_basemap(bounds = c(166.70047,-34.45676, 178.52966,-47.06345), 
                top_layer = "satellite") 

Further information

Other simplevis posts include:

More blogs to come on simplevis.

In the meantime, see the vignette and articles on the simplevis website.

To leave a comment for the author, please follow the link and comment on their blog: David Hodge.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)