Latitude and Longitude Coordinates

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

Load Libraries

library(tidyverse)
library(sf)
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", show_col_types = FALSE)

Subset Data to North Carolina

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

starbucksNC %>% 
  glimpse()
Rows: 229
Columns: 23
$ Brand                 <chr> "Starbucks", "Starbucks", "Starbucks", "Starbuck…
$ `Store Number`        <dbl> 72949, 9272, 13258, 76569, 76100, 2970, 8495, 13…
$ Name                  <chr> "Farm Fresh-Elizabeth City #469", "Roanoke Rapid…
$ `Ownership Type`      <chr> "Licensed", "Company Owned", "Company Owned", "L…
$ `Facility ID`         <dbl> 15736, 14789, 10765, 10147, 12631, 16246, 15406,…
$ `Features - Products` <chr> NA, "Lunch, Oven-warmed Food", "Oven-warmed Food…
$ `Features - Service`  <chr> NA, "Starbucks Card Mobile, Wireless Hotspot", "…
$ `Features - Stations` <chr> NA, "Drive-Through", NA, NA, NA, "Drive-Through"…
$ `Food Region`         <dbl> 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, …
$ `Venue Type`          <chr> "Unknown", "Unknown", "Unknown", "Unknown", "Unk…
$ `Phone Number`        <chr> "252-331-1368", "252-533-5900", "252-255-0790", …
$ Location              <chr> "691 S Hughes Blvd\nElizabeth City, NC 27909-453…
$ `Street Address`      <chr> "691 S Hughes Blvd", "298 Premier Blvd.", "5597-…
$ `Street Line 1`       <chr> "691 S Hughes Blvd", "298 Premier Blvd.", "5597-…
$ `Street Line 2`       <chr> NA, NA, NA, NA, NA, "Ste. 109", "101", NA, "Unit…
$ City                  <chr> "Elizabeth City", "Roanoke Rapids", "Kitty Hawk"…
$ State                 <chr> "NC", "NC", "NC", "NC", "NC", "NC", "NC", "NC", …
$ Zip                   <chr> "27909-4530", "27870-5076", "27949-4090", "27804…
$ Country               <chr> "US", "US", "US", "US", "US", "US", "US", "US", …
$ Coordinates           <chr> "(36.290986, -76.25259)", "(36.4324, -77.6388)",…
$ Latitude              <dbl> 36.29099, 36.43240, 36.09835, 35.97197, 35.97441…
$ Longitude             <dbl> -76.25259, -77.63880, -75.72458, -77.81310, -78.…
$ `Insert Date`         <chr> "06/22/2012 06:31:38 PM", "06/22/2012 06:31:38 P…

Make the Map

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

mapview(starbucksNC, xcol = "Longitude", ycol = "Latitude", crs = 4269, grid = FALSE)
data
50 km
50 mi
Leaflet | © OpenStreetMap contributors © CARTO

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…

# mapview(sbux_sf)
mapview(sbux_sf, map.types = "CartoDB.DarkMatter") 
sbux_sf
50 km
50 mi
Leaflet | © OpenStreetMap contributors © CARTO

Reuse