To introduce the mapview package, make an interactive map with Starbucks coffee shop locations in North Carolina (2012). (Adapted from Machlis.)1

Load Libraries

library(tidyverse)
library(sf)
library(leaflet)
library(mapview)

Load Data

2012 Starbucks locations (data source)

starbucks <- read_csv("https://raw.githubusercontent.com/libjohn/mapping-with-R/master/data/All_Starbucks_Locations_in_the_US_-_Map.csv")

Subset Data to North Carolina

starbucksNC <- starbucks  %>% 
  filter(State == "NC")

starbucksNC

Make the Map

In this example, plot latitude (y coordinates) and longitude (x coordinates), then set the map projection to a common projection standard, WGS84, via the argument crs = 4326.)

mapview(starbucksNC, xcol = "Longitude", ycol = "Latitude", crs = 4269, grid = FALSE)

Alternative: Transform data to Spatial object

Another way to plot the x & y coordinates is by transforming the starbucksNC tibble (i.e. the starbucksNC data frame) into a spacial data frame via the simple features function, st_as_sf(). It’s important to set the map projection to a common standard, WGS84, via the argument crs = 4326.)

sbux_sf <- st_as_sf(starbucksNC, coords = c("Longitude", "Latitude"),  crs=4326)

Now Map the sf object.

Below, you can plot the latitude and longitude coordinate, and set the map.types argument to openStreetMap. Base maps are set with map.types. For example a high contrast, black and white basemap can be set with the argument map.types = "Stamen.Toner". See available map types, or leave out the map.types argument for a set of default base maps to choose via the layering button.

#mapview(sbuxsfc, map.types = "Stamen.Toner")
mapview(sbux_sf)