An Easy Way to Map Data in R with Plotly

A couple of years ago, I wrote The complete n00bs guide to mapping in R, my first adventure into R. While that tutorial still holds up, if you’re looking to make a state-level Choropleth Map, there really isn’t anything easier than working with Ploty in R.

Once you get R and RStudio installed and set up, there’s only a few steps that you need to take. If you have a spreadsheet or can make one easily enough of state-level data, like this ranking of mental health and access in the USA by states , you only need a couple of lines of code (minus all the comments that follow the #s).

Let’s start by getting plotly set up.
install.packages("plotly") #installs the Plotly library for R
library("plotly") #tells R you want to use the Plotly library package

I just made a CSV file by copying the information from the website. One column was “state” and the other was “rank.” Because the data was which state and its rankings it was simple. Warning: In this case, the states do need to be copied as postal abbreviations for this to work. Plotly can also do countries. Check out their documentation for the changes you’ll need to make.

Import your spreadsheet and give it a name to use in R
mh<- read.csv("C:\\Users\\Documents\\mentalhealth.csv", header = TRUE, sep = ",")

And we’re ready to plot it!
plot_ly(type="choropleth", locations=mh$state, locationmode="USA-states", z=mh$rank) %>% layout(geo=list(scope="usa"), title="State of Mental Health in America Overall Ranking: The Bluer, The Better")

Boom!
And did I mention it’s interactive!

A map of the US with the states shaded in colors ranging from dark blue to yellow
Click for a full size map

Another Example
Because plotly makes the mapping so simple, I finally got around to looking at the geographic distribution of the All-America City Award. It started in 1949 and the city I grew up in (Grand Rapids, MI) was one of the inaugural winners in 1949. Since finding out about the award, I was curious if there was any states that did particularly well. However, it was one of those curiosities that was never really worth the effort. Until Plotly made it super easy!

I copied the table straight from Wikipedia and stripped it down to the just the state column. A few metropolitan areas are listed with multiple state winners so split the entry giving both states their own line in the data. I also deleted Puerto Rico (sorry Puerto Rico). I then brought it the file as above.
usacities<- read.csv("C:\\Users\\Documents\\allamericanwins.csv", header = TRUE, sep = ",")

This time, I took a few extra steps. Because my data was just a list of states over and over again (Alabama, Alabama, Wisconsin, North Carolina, Wisconsin, etc), I needed to count how many times each state was in the file. R makes it easy to generate a frequency table.
table(usacities)
(I told you it was easy)

I then made a new file out of the frequency table because that’s how I roll.
write.csv(table(usacities), file = "C:\\Users\\Documents\\allamericanwinsfreq.csv")

I changed the state names to postal abbreviations and add NV and UT who had zero wins, then I was ready to bring in the file and plot the map.
usa% layout(geo=list(scope="usa"), title="All-America City Award Winners by State")
I suppose I could have done this at the beginning and skipped the whole writing a new document thing but hey that’s hindsight for you.

A map of the US with states shaded from dark red to light grey
Click for a full size map

It looks like the upper Midwest and and North Carolina/Virginia are the big winners of the award. Because I wanted to get an idea of how this fit with the state’s populations, I did some more simple calculations (finding the difference in each state’s ranking of number of times they won the award and the rank in total population. This map shows Alaska as a big winner. The Great Plains stays as a winner and this time the South is shown to be a loser when it comes to this award. New England did ok too.

A map of the US with states shaded in colors ranging from blue to red
Click for a full size map


Now go forth and create choropleth maps!

%d bloggers like this: