简体   繁体   中英

How can I load CSV data points onto a shapefile in R?

I am trying to plot Starbucks locations from CSV data onto a city shapefile that I've loaded in. I want to essentially create a heat map or simple scatter plot with the Starbucks locations on the city shapefile. The csv file has longitude and latitude points.

This is the code I have so far, I'm having trouble with merging the two files together though. Please let me know if you can help!

install.packages("sp")
install.packages("sf")

library(sf)
library(sp)

dallas_starbucks <- st_read("STREETS.shp")
st_geometry_type(dallas_starbucks)
st_crs(dallas_starbucks)
st_bbox(dallas_starbucks)
dallas_starbucks

ggplot() + 
geom_sf(data = dallas_starbucks, size = 5, color = "white", fill = "blue") +
ggtitle("Starbucks in Dallas") +
coord_sf()

starbucks_location <- read.csv("dallas_starbucks_directory.csv")
head(starbucks_locations)

----- 
dallas1 <- merge(dallas_starbucks, starbucks_location, by.x='Longitude', by.y='Latitude')

---I've also tried the PBSmapping package, but I'm stuck:

install.packages("PBSmapping")
library(PBSmapping)
sbx <- read.csv("dallas_starbucks_directory.csv")
sbx <- points(sbx$Longitude, sbx$Latitude)
library(dplyr)

shapefile: https://gis.dallascityhall.com/shapefileDownload.aspx (City of Dallas Streets)

dput(head(starbucks_location)):

structure(list(Brand = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Starbucks", 
"Teavana"), class = "factor"), Store.Number = structure(c(53L, 
42L, 46L, 48L, 40L, 21L), .Label = c("10575-101278", "10648-101241", 
"11296-104022", "13723-108403", "14144-111906", "14997-149948", 
"15948-156824", "16933-169085", "17699-183444", "17766-183446", 
"17977-183445", "18138-184064", "18815-191486", "20138-185004", 
"20470-140411", "21683-212746", "22121-185008", "22384-213344", 
"23318-222609", "23558-230688", "23731-222608", "23758-216804", 
"26643-243988", "27183-246608", "2802-89421", "28351-249759", 
"28435-249745", "2935-91108", "3465-93156", "47679-258845", "48691-243986", 
"50217-272131", "6215-538", "6222-656", "6228-675", "6229-812", 
"6235-678", "6239-893", "6244-165605", "6248-1038", "6249-253984", 
"6253-1018", "6254-1019", "6264-1196", "6272-238047", "6277-1332", 
"6291-1072", "6302-1882", "6316-4938", "6329-11981", "6359-21041", 
"6361-227965", "6367-27349", "6390-29235", "6721-75402", "675-1350", 
"676-1349", "677-1351", "680-1354", "690-1366", "692-1352", "695-1353", 
"70003-103666", "72486-20060", "72617-22323", "72626-32319", 
"72966-95132", "74527-59159", "74612-90301", "74637-90311", "75174-83220", 
"75325-99901", "75328-101613", "75531-87667", "75736-96376", 
"75741-96375", "75746-96374", "75747-96377", "75821-99240", "76572-101597", 
"76611-100216", "76883-121248", "78062-115644", "79451-101610", 
"79610-113253", "79656-113252", "79679-117024", "9539-96851", 
"9850-98196"), class = "factor"), Store.Name = structure(c(55L, 
38L, 54L, 32L, 62L, 88L), .Label = c("1700 Pacific Ave.", "AAFES HQ Food Court", 
"Abrams & Mockingbird", "AT&T Global Headquarters - Lobby", "Beltline and Coit", 
"Berkshire Court", "Casa Linda", "Coit & 635", "Cole & Lemmon", 
"Columbus Square", "DAL East", "DAL West", "Dallas Galleria", 
"DFW - Concourse D, Simply Books", "DFW - Landside Ticketing", 
"DFW - Term C, Gate C21", "DFW - Term C, Gate C27 S. Station", 
"DFW - Term C, Gate C6", "DFW - Term C, Gate C8 North Station", 
"DFW - Terminal B", "DFW - Terminal B Gate 28", "DFW - Terminal D12", 
"DFW - Terminal E Satellite", "DFW A 20", "DFW Terminal D27", 
"Fairmont Dallas", "Fondren Library", "Forest & Park Central", 
"Forest & Webb Chapel", "Frankford & 190", "Frankford & Tollway", 
"Galleria North", "Garland & Gaston", "Gaston & Haskell", "Greenville & Caruth Haven", 
"Greenville & Lovers", "Highland Park Village", "Hillcrest & Arapaho", 
"Hotel Crescent Court", "I-30 & Cockrell Hill", "Inwood & Willow", 
"Inwood Village", "JPMC Tower", "Knox Street", "Kroger-Dallas #509", 
"Kroger-Dallas #518", "Kroger-Dallas #588", "Kroger-Dallas, TX #529", 
"Lakewood", "Lemmon & Inwood", "Lemmon & Knight", "Maple & Clara", 
"Mockingbird & 183", "Mockingbird & Airline", "N. Central Exp & Mockingbird", 
"Northpark Mall  Level 1", "Northwest Highway & I-35", "Northwest Hwy & Shadybrook", 
"NW Hwy & Midway", "Oaklawn & Rawlins", "Preston & Alpha", "Preston & Forest", 
"Preston & Frankford", "Preston Royal", "Randall's - Dallas #2642", 
"Randalls-Dallas #3608", "Renaissance Tower", "Ross Tower", "Royal & Abrams", 
"Shops at Park Lane", "Smu: Deadman Athletic Center", "Snider Plaza", 
"Super Target Dallas ST-1784", "Super Target North Dalls ST-1775", 
"Target Dallas-South T-2334", "Target Dallas T-875", "Target Dallas T-947", 
"Target Dallas/Medallion T-55", "Teavana - Dallas Galleria", 
"Teavana - Northpark Center", "Tom Thumb-Dallas #2990", "Tom Thumb-Dallas #3560", 
"Tom Thumb-Dallas #3637", "Tom Thumb - Dallas #1540", "Tom Thumb - Dallas #3569", 
"Tom Thumb - Dallas #3614", "Tom Thumb - Dallas #3650", "Webb Chapel & NW Highway", 
"World Trade Center Dallas"), class = "factor"), Ownership.Type = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("Company Owned", "Licensed"), class = "factor"), 
    Street.Address = structure(c(55L, 67L, 25L, 54L, 4L, 39L), .Label = c("100 Highland Park Shopping Ctr, Suite 101A", 
    "102 Preston Royal Shopping Ctr", "1111 Mockingbird Lane", 
    "11919 Preston Road", "1201 Elm Street, Suite 115", "12262 Inwood Road", 
    "13350 DALLAS PKWY, STE 2615", "13350 Dallas Pkwy, Suite 2505", 
    "13556 Preston Rd", "1380 W Campbell Rd, Dallas Galleria", 
    "14715 Coit Rd, 101", "14999 Preston Rd, Village of Preston Hollow", 
    "1601 N Cockrell Hill", "16731 Coit Rd", "1700 Pacific Avenue, Suite C-102, Preston Royal Shopping Center", 
    "1717 N. Akard, Columbus Square", "18208 Preston Road, Suite D12", 
    "2050 N Stemmons Fwy", "208 S Akard St", "212 Medallion Ctr", 
    "2200 Ross Ave", "2285 W. Northwest Highway", "2417 N Haskell Ave", 
    "2801 Allen St., Ste. 180", "3036 Mockingbird Lane, Galleria North", 
    "3100 Forest Lane", "315 South Hampton", "3200 E Airfield Dr", 
    "3200 E Airfield Dr, Frankford Crossing", "3200 E Airfield Dr, Highland Park Shopping Center", 
    "3200 E Airfield Dr, Summertree Shopping Center", "3216 Knox Street", 
    "3301 S 22nd Ave", "3301 S 22nd Ave, Lakewood Plaza", "3330 Oaklawn Avenue", 
    "3398 Tom Braniff Ln", "3555 Frankford Road", "3699 McKinney Ave", 
    "3701 W Northwest Hwy, Suite 410", "3757 Forest Lane, Dallas Galleria", 
    "3878 Oak Lawn Avenue", "3911 Walton Walker", "39739 LBJ Fwy", 
    "400 Crescent Court", "4025 Gaston Avenue", "4101 Lemmon Ave.", 
    "4241 Capitol Ave, Casa Linda Plaza", "4343 W Northwest Highway, Suite 390", 
    "4727 Frankford Road, Suite 337, Northpark Center", "4744 Maple Avenue", 
    "4901 Maple Ave", "4924 Greenville Ave, 110", "500 N. Akard St.", 
    "5223 Alpha Road, Preston Forest Square", "5331 E. Mockingbird Lane, 150, Hillcrest Village Shopping Center", 
    "5350 W. Lovers Lane, Suite 125", "5665 E. Mockingbird", 
    "5715 Lemmon Avenue", "5809 E Lovers Lane", "5905 E. Northwest Highway, Mockingbird Commons S.C.", 
    "6005 Bush Ave", "6123 Greenville Ave, Pinnacle Park", "6312 La Vista", 
    "6333 East Mockingbird, Suite 107", "6419 Skillman St", "6733 Hillcrest Avenue", 
    "6859 Arapaho", "7117 Inwood Rd", "7325 Gaston Ave, Suite 100", 
    "7700 W Northwest Hwy", "7718 Forest Lane, Town Creek Shopping Center", 
    "7995 LBJ, Suite 114", "8008 Cedar Springs Rd.", "8026 Park Lane, 100", 
    "8411 Preston Rd, Ste 100", "8520 Abrams Rd, Ste 100", "8687 N. Central Expressway, F2-926", 
    "8687 NORTH CENTRAL EXPWY, #1812", "8698 Skillman St", "9140 Forest Lane", 
    "9440 Garland Road, Suite 110-112, West Village", "9440 Marsh Ln", 
    "Hillcrest & Mockingbird"), class = "factor"), City = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("Dallas", "DALLAS"), class = "factor"), 
    State.Province = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "TX", class = "factor"), 
    Country = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "US", class = "factor"), 
    Postcode = c(75206L, 75248L, 75205L, 75240L, 75230L, 75220L
    ), PhoneNumber = structure(c(46L, 21L, 10L, 78L, 20L, 4L), .Label = c("", 
    "(214) 219-0369", "(214) 328-3401", "(214) 350-0601", "(214) 358-7587", 
    "(214) 520-2273", "(214) 528-0121", "(214) 654-0163", "(214) 654-0704", 
    "(214) 691-2484", "(214) 691-4890", "(214) 6914247", "(214) 823-5758", 
    "(214) 823-7006", "(214) 824-3675", "(214) 965-9696", "(214) 969-9031", 
    "(214) 987-3291", "(972) 243-9968", "(972) 392-1680", "(972) 458-2284", 
    "214-219-1735", "214-221-0939", "214-331-0160", "214-333-1679", 
    "214-340-1126", "214-342-6998", "214-346-1020", "214-348-0240", 
    "214-350-3883", "214-350-4063", "214-353-6907", "214-357-3980", 
    "214-360-0288", "214-361-2026", "214-363-4678", "214-369-3228", 
    "214-507-9413", "214-691-1101", "214-750-3200", "214-757-7752", 
    "214-775-0206", "214-777-5350", "214-826-0331", "214-826-2901", 
    "214-827-8101", "214-920-9530", "214/522-3531", "2142133340", 
    "2143213556", "2143511061", "2143637691", "2145594050", "2145787355", 
    "2147063561", "2147205338", "2147419830", "2147709521", "2147826655", 
    "469-341-3920", "469-374-9166", "4697867069", "4697919284", 
    "888-729-5656", "888-796-5282", "972-338-3100", "972-542-2895", 
    "972-574-4927", "972-574-8647", "972-661-2666", "972-661-8079", 
    "972-680-6010", "972-713-9066", "972-716-0838", "972-725-1269", 
    "972-788-9340", "972-880-8919", "972-960-9595", "972-973-8570", 
    "972/519-0537", "9723654089", "9726931974", "9728205613"), class = "factor"), 
    TimeZone = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "GMT-06:00 America/Chicago", class = "factor"), 
    Longitude = c(-96.78, -96.79, -96.78, -96.82, -96.8, -96.86
    ), Latitude = c(32.84, 32.96, 32.84, 32.93, 32.91, 32.86)), row.names = c(NA, 
6L), class = "data.frame")

Since the streets are a line geometry and the Starbucks locations are point geometries, they can't be merged into a single shapefile. However, they can still be plotted on the same map. I use ggplot here, but you can use any number of other plotting tools.

First step is to turn the Starbucks csv from a regular dataframe with some lat/long coordinates into an sf object that has geometries using st_as_sf() . Then, re-project that to match the projection for Dallas streets using st_transform() .

library(sf)
library(tidyverse)

streets <- st_read("STREETS.shp")

starbucks_proj <- starbucks_location %>% 
  st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326) %>% 
  st_transform(st_crs(streets))

Then, we use ggplot to map the streets and starbucks locations.

ggplot() +
  geom_sf(data = streets, color = "gray") +
  geom_sf(data = starbucks_proj, color = "red", size = 2) + 
  theme_minimal()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM