Friday, June 12, 2015

Maps in R for dummies

You're an absolute beginner to R, or you've not used R for a long time, and are wondering how to draw maps in R. If this describes your situation, then this post is for you.

First off, open R and you should get into this window.


(It's fine if you use more intuitive interfaces such as Quick-R.) Next, if you have not installed the maps package, you will need to install it using the following command:

install.packages('maps')
For the uninitiated, packages are collections of R functions, compiled code, and data. Think of it as an "add on".
R may tell you that
package ‘maps’ successfully unpacked and MD5 sums checked
upon a successful installation. It may also tell you that you've already installed the package, which is fine.
Although you've installed the maps package, you've not loaded it into memory. Thus, R can't use the maps package yet. As such, type
library(maps)
to load the package into the R workspace. Notice there is no need for quotes.
Let's try to plot the first map:
map('usa')

What if I want to see state boundaries?
map('state')
 Not a problem. Let's try California. Since it's a pretty large state, we add axes to see the longitude and latitude:
map('state','california')
map.axes() 

And what about US county boundaries?
map('county')
Of course, you may want to add a title to your map. After typing in map('county'), type the following command without closing the map window:
title("Counties of the United States")
What if I wanted to save the picture to disk?

getwd() # gets the working directory
png(filename="counties.png")# creates a file called "counties.png"
map('county') # writes the county map to the png file
title("Counties of the United States") # adds title
dev.off() # turns off recording, i.e. output no longer goes to the png.

If you're not used R for a long time, remember that the pound sign (#) is for comments. Thus everything I've highlighted in green is a comment and will not be processed by R. It's good practice to annotate your code extensively. It will help you learn faster, and when you

When you open counties.png, you should get the following map:
So far, so good. But it's pretty boring if we can only see US states and counties. Anyone can Google those up. What else can we do?

Let's label the main cities of Rhode Island. First, we load data of US cities into R. Then we create a pdf file and save Rhode Island and its cities into the file.

data(us.cities)
pdf(filename="rhodeisland.pdf")
map('state','rhode island')
map.cities(us.cities,country="RI")
dev.off()

gets us this map:



You may be wondering whether R's map package contains only US maps. Nope! Try other countries such as New Zealand:
map('nz')
We have only touched the tip of the iceberg. There's so much more the package can do. For example, you can plot US unemployment, pollution, and voting data on the maps. The first step is to load these datasets:

data(ozone)
data(unemp)
data(votes.repub)

Challenge: plot these data onto US maps.

R provides extensive documentation for the maps package, which includes the solution to these challenges.


No comments:

Post a Comment