Skip to content →

The complete n00b’s guide to mapping in R

You should also check out the next tutorial in the series: The Complete n00b’s Guide to Gephi

A few weeks ago, I presented to the UNL DH community about a project that I’m beginning while a fellow at the CDRH’s Digital Scholarship Incubator. The project is an effort to utilize digital tools to visualize business and organizational records related to my dissertation on industrialization in small cities. During my talk, I noted I was still uncertain as to what tool to use to create my maps, but thankfully, James Austin Wehrwein was also presenting. Afterwards he suggested I consider R and check out his tutorial on creating a density map in R.

Frankly, I was blown away how easy it was to create a map in R. His tutorial was easy to follow and acclimated me to R rather quickly. On top of this tutorial, I realized that the data I had used initially in Gephi already contained the coordinates for each geographic location and I would not need to clean up my data, reducing the number of steps even further. Convinced R was my new best friend, I began looking around for a way to create choropleth maps, which were another type of visualization I wanted in my project. I was thrilled to find someone had already done much of the heavy lifting and there was a packet that made the process so easy even I could create maps without pulling my hair out.

In the interest in helping out other n00bs, I’ve posted my steps in creating these maps below:

Creating a Density Map

Packets you’ll need:

> library(“ggmap”, lib.loc=”C:/Users/Home/Documents/R/win-library/3.1″)

Import Spreadsheet:

> ph<- read.csv("C:\\Users\\Home\\Documents\\school\\shipping.csv", header = TRUE, sep = ",") ph is just a placeholder, use whatever name you want Create Map: > map<-get_map(location='united states', zoom=4, maptype='roadmap') ggmap(map)+geom_point(aes(x=longitude, y=latitude, size=(total.cost)), data=ph, alpha=.5) This is all you need to do if you already have the longitude and latitude coordinates. Again, see creating a density map in R”>this tutorial if you don’t have already clean data.


That was easy!
That was easy!



Creating a Choropleth Map
This user guide is how I figured it out and has much more information than I give.

Packets you’ll need:

> library(“choroplethr”, lib.loc=”C:/Users/Home/Documents/R/win-library/3.1″)
> library(“Hmisc”, lib.loc=”C:/Users/Home/Documents/R/win-library/3.1″)

Import Spreadsheet:

> df<- read.csv("C:\\Users\\Home\\Desktop\\W1923.csv", header = TRUE, sep = ",") You'll see that it's the same process as above, I've just switched the letters I'm using as the name to help confuse you. The beauty of choroplethr is that you don't need any latitude or longitude coordinates. The program can identify states by either full name or postal abbreviation, counties by FIPS code and even by zip code. For your spreadsheet, you'll just need to creat two columns: "value" which has your data, and "region" which is your state/county code/zip code. To Create an Choropleth Automatically: > choroplethr(df, “state”, num_buckets = 6, title = “W1923”, scaleName = “Buyers”, showLabels = T, states = state.abb)

The size of the buckets will be automatically configured, but you can also have a continuous scale if you designate the number of buckets as 1. Here you should change the title and scaleName to whatever you want it to say. Note “df” tells the program the name of my spreadsheet and “state” tells the program what kind of “region” to look for in my data. You’re line would read “county” or “zip” if you are not using state names.


It's a continuous scale!
It’s a continuous scale!



Sizing your buckets
Now if you don’t want the program to automatically determine the size of your buckets, you can do the following:

> df.map = bind_df_to_map(df, “state”)
> df.map$value = cut2(df.map$value, cuts=c(0,50,100,150,Inf))
> render_choropleth(df.map, “state”, “Grand Rapids Winter Market 1923”, “Buyers Attending”)

Here I’ve told the program to create buckets with dividing lines at 0, 50, 100, and 150. Simply add or subtract numbers here to create the desired number and size of your buckets. Also notice that the data you are pull from has changed from df to df.map which I created with “bind_df_to_map”. Again, “state” would be replaced with “county” or “zip” if using one of them them.


Ta Da!
Ta Da!



I’m still learning R and figuring out how to better improve these maps, but if you’re looking for something quick and effective these maps are hard to beat.

The only question remains is: R you ready to give it a try?

(Be thankful I only included one “R” based pun)




Try Audible and Get Two Free Audiobooks

Brian is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com

Published in Digital Humanities Research

3 Comments

  1. Brian I appreciate you mentioning my tutorial! It was an eye opening DH presentation all around. I did another map on international shipping data you may be interested in. Possible collaboration in the works for some future data? (I like making pretty graphs)

Comments are closed.