0

I'm trying to plot trips between zipcodes in R. Specifically, I'm trying to create an interactive where you can click on each zipcode, and see the other zipcodes colored according to how many people traveled from the zip you clicked on to the other zipcodes. Sort of like this: https://www.forbes.com/special-report/2011/migration.html

But less fancy; just showing "out-migration" would be super.

I've been messing with this in R using the leaflet package, but I haven't managed to figure it out. Could someone with better R skills help me out? Any insight would be much appreciated.

I've downloaded a shapefile of zipcodes in LA county from here: https://data.lacounty.gov/Geospatial/ZIP-Codes/65v5-jw9f

Then I used the code below to create some toy data.

You can find the zipcode shapefiles here: https://drive.google.com/file/d/0B2a3BZ6nzEGJNk55dmdrdVI2MTQ/view?usp=sharing

And you can find the toy data here: https://drive.google.com/open?id=0B2a3BZ6nzEGJR29EOFdjR1NPR3c

Here's the code I've got so far:

require(rgdal)
setwd("~/Downloads/ZIP Codes")
# Read SHAPEFILE.shp from the current working directory (".")
shape <- readOGR(dsn = ".", layer = "geo_export_89ff0f09-a580-4844-988a-c4808d510398")

plot(shape) #Should look like zip codes in LA county

#get a unique list of zipcodes
zips <- as.numeric(as.character(unique(shape@data$zipcode)))
#create a dataframe with all the possible combination of origin and destination zipcodes
zips.df <- data.frame(expand.grid(as.character(zips),as.character(zips)), rpois(96721,10))
#give the dataframe some helpful variable names
names(zips.df) <- c("origin_zip", "destination_zip","number_of_trips")

Like I said, any help would be much appreciated. Thanks!

  • Can you ask a more specific question? Possibly break your question into parts. – CCurtis Sep 27 '17 at 14:27
  • 1
    Thanks for your reply. It's good; I figured out a couple different ways to do this. One based on this answer: https://stackoverflow.com/questions/44283774/flow-maptravel-path-using-lat-and-long-in-r And one using the geosphere package and the gcIntermediate function – ConfusedEconometricsUndergrad Oct 04 '17 at 22:01

0 Answers0