I am new to mapping in R.
I want to have a density map of US for number of phone calls for different areas. Each area includes some zip codes. I only have number of calls for each zip code and I want to show it on a map of US by using zip codes. As I don't have polygon coordinates for my areas, I decided to use the zip3 shapefile which is available on the web and merge my data with the existing shapefile.
To be more clear, my area is formed by zips 006, 010 and 011. I defined an ID for each area and if different zips are related to the same area, they will have identical ids. Hence, my Excel file includes area name, zip3 and ids related to each zip3. I used zip3 from my Excel file as a joining variable to the shapefile (shapefile has a zip3 column) and allocated different ids to zip3s which are in the shapefile.
At this time, I wanted to merge polygons of zip3 s which forms an area to build a map of my areas from the shapefile of zip3s. I believe this is the right approach but it is not working. My problem is that maybe I'm reading my shapefile to a wrong data frame. I used readShapePoly() to read my shapefile.
My code is as follows:
data <- read.csv(“zipmatch.csv”,colClasses=c(“character”,”character”,”numeric”),header=TRUE) data2 <- readShapePoly(“zip3.shp”) data3 <- merge(data2,data,by="ZIP3") final <- unionSpatialPolygons(data3,"id")
It gives me the following error at this time:
Error in unionSpatialPolygons(data3, "id") : input lengths differ