R365: Day 28 – {maps} again

I offered to help a friend make a map of the US with some states filled in. I thought it would be a quick and easy job, and it is, but it took me a while to figure out how to do it. At first I tried plotting the US and then setting up a separate region() argument. This just created a new plot consisting of the states that I had specified! Which was pretty unfortunate and not what I wanted to do. After looking through some blogs and finding out about some awesome sounding packages (heads up, eventually I will tackle {RGoogleMaps}), I eventually figured out that within the maps() function there is an argument called ‘add’, which lets you add on another step to a previously plotted map. This way, you build the map in different layers. It’s kind of cumbersome and I am reasonably sure there is an easier way around this, but I’m going to go with ‘If it’s dumb and it works…”, and say it’s good enough for me.

I didn’t want to reveal her map, so I created a map of the places I have been and the places I want to go to soon. It was kind of tricky, because you have to kind of guess what the countries are called. I entered ‘us’ thinking that it would highlight the US, but it highlighted both the US and the former USSR. Eventually after several permutations, I figured out that it recognized both because the front end of their name (so if you put ‘south’ it would highlight South Korea, South Africa, etc) and entered ‘usa’. The map also does not recognize the term ‘russia’, but will recognize ‘ussr’ (which is a bit ominous). You can find a whole list of the countries by using:

 map("world", namesonly=TRUE, plot=FALSE)

But they seem to be in no order. Maybe there is a list somewhere out there?

Rplot04

##install and load the maps package into R
install.packages("maps")
library(maps)
##if you need help with maps, remove the hashtag on the line below
#library(help="maps")
## or check out http://cran.r-project.org/web/packages/maps/maps.pdf

##Map out World
map('world', interior = FALSE)
map('world', boundary = FALSE, lty = 5, add = TRUE)
map('world',region=c('belize','canada','italy','australia','USA'),fill=TRUE,col='blue',add=TRUE)
map('world',region=c('argentina','brazil','chile','peru','new zealand','UK','ireland','iceland','south korea','vietnam','nepal','ussr','madagascar','switzerland','netherlands','belgium','france','spain','germany','denmark','norway','finland','sweden','mexico','ghana','india'),fill=TRUE,col='green',add=TRUE)
## add axes to your map
map.axes()
##add a scale bar to your map
map.scale()
##find out about plotting legends http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/legend.html
legend(50,-50, c("Places I've Been","Places I want to Go"),fill=c("blue","green"))
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s