Though not specifically geared towards text analysis I thought that this tutorial would be helpful to anyone. When I had to learn how to create these maps using R there was no thoroughly comprehensive how-to guide. So I thought I would put the steps down on how to make a density map in R from a .csv file with zip codes.
First we will need a file with some zipcode data. Let’s grab some data from the government’s raw data files.
What you will want to do is remove the intro information at the top so your column headings are on the top and save it as a CSV, once you are done with that we can begin.
Make sure you have these packages loaded:
First we need to import the data into our R document, your working directory needs to be set to where your files are stored. Once that is done we can use “read.csv” with a few specifications to import our data file.
fm<- read.csv("FM.csv", header = TRUE, sep = ",")
This next section is what we would use if latitude and longitude was not included. We could have done this count by zip codes which would have needed some cleaning up. Thankfully a great package called Zipcode is available and here is the code we would have needed for that. First you clean the zips, then using the data from the package merge all the cleaned ones back into your file. Latitude and Longitude are also created from this.
data(zipcode) fm$zip<- clean.zipcodes(fm$zip) fm<- merge(fm, zipcode, by.x='zip', by.y='zip')
Next we are going to get counts of the Farmer’s Markets in each city, we do this by using the plyr package since we are going to use ggmap to make the map. Then we change out the column in our data frame for the count of the city.
density<- ddply(fm, .(city.x), "nrow") names(density) <- "count" FM<- merge(fm, density)
This section is to edit our data frame so we get rid of duplicates so we are left with just the unique counts of Farmer’s Markets in those cities.
duplicated(FM$city.x) FM[duplicated(FM$city.x),] unique(FM[duplicated(FM$city.x),]) FM<-FM[!duplicated(FM$city.x),]
Now we are ready to plot.
map<-get_map(location='united states', zoom=4, maptype='roadmap') ggmap(map)+geom_point(aes(x=longitude, y=latitude, size=(count)), data=FM, alpha=.5)