Latitude and Longitude Coordinates

Use the mapview package to make interactive maps. In this example, georeference Starbucks coffee shop locations in North Carolina (2012). 1

Load Libraries

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 %>% 
  glimpse()
Rows: 229
Columns: 23
$ Brand                 <chr> "Starbucks", "Starbucks", "Starbucks",~
$ `Store Number`        <dbl> 72949, 9272, 13258, 76569, 76100, 2970~
$ Name                  <chr> "Farm Fresh-Elizabeth City #469", "Roa~
$ `Ownership Type`      <chr> "Licensed", "Company Owned", "Company ~
$ `Facility ID`         <dbl> 15736, 14789, 10765, 10147, 12631, 162~
$ `Features - Products` <chr> NA, "Lunch, Oven-warmed Food", "Oven-w~
$ `Features - Service`  <chr> NA, "Starbucks Card Mobile, Wireless H~
$ `Features - Stations` <chr> NA, "Drive-Through", NA, NA, NA, "Driv~
$ `Food Region`         <dbl> 9999, 9999, 9999, 9999, 9999, 9999, 99~
$ `Venue Type`          <chr> "Unknown", "Unknown", "Unknown", "Unkn~
$ `Phone Number`        <chr> "252-331-1368", "252-533-5900", "252-2~
$ Location              <chr> "691 S Hughes Blvd\nElizabeth City, NC~
$ `Street Address`      <chr> "691 S Hughes Blvd", "298 Premier Blvd~
$ `Street Line 1`       <chr> "691 S Hughes Blvd", "298 Premier Blvd~
$ `Street Line 2`       <chr> NA, NA, NA, NA, NA, "Ste. 109", "101",~
$ City                  <chr> "Elizabeth City", "Roanoke Rapids", "K~
$ State                 <chr> "NC", "NC", "NC", "NC", "NC", "NC", "N~
$ Zip                   <chr> "27909-4530", "27870-5076", "27949-409~
$ Country               <chr> "US", "US", "US", "US", "US", "US", "U~
$ Coordinates           <chr> "(36.290986, -76.25259)", "(36.4324, -~
$ Latitude              <dbl> 36.29099, 36.43240, 36.09835, 35.97197~
$ Longitude             <dbl> -76.25259, -77.63880, -75.72458, -77.8~
$ `Insert Date`         <chr> "06/22/2012 06:31:38 PM", "06/22/2012 ~

Make the Map

In this example, plot latitude (y coordinates) and longitude (x coordinates). Then set the map projection2 to a common projection standard such as 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(). Again, set the map projection to a common standard such as WGS84 via the crs= argument.

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.

This time with a different basemap…

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. If you leave out the map.types argument the end-user can pick from a set of default base maps via the layering button.

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

  1. 5 Visualizations in 5 Minutes. ComputerWorld.com by Sharon Machlis↩︎

  2. Projection / CRS↩︎

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. Source code is available at https://github.com/libjohn/mapping-with-R, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".