R has had a maps package available since the very early days. It's great for simple geographic maps, but the political boundaries can be out of date. For more detailed maps, you can also download shape files and use the sp package to draw borders directly.
But for accurate and attractive maps of countries, roads and satellite imagery, nothing beats online sources like OpenStreetMap and Google Maps. At the useR!2012 conference last month, David Kahle introduced his ggmap package, which makes it trivial to import such maps into R. You can then use maps as a background layer in a data visualization with ggplot2. (David gives detailed information in his useR slides.)
I wanted to try out the ggmap package, so I thought it would be fun to try and solve a problem that's been bugging me for a while: it's hard to find a good map of wineries you can visit in Napa Valley. (Yes, first-world problems, I know.) While there are more than 400 wineries in the Valley (plus hundreds more garage-based and custom crush producers), you can't visit all of them for a public tasting. The Napa Valley Wine project manages a database of all the wineries and notes which can be visited, but doesn't provide a map. I decided to use ggmap to create one.
First, I imported the Wine Project database as a data frame in R, and filtered out any wineries with an missing address (or where only a P.O. Box was provided). Then, I selected all of the wineries that offered tastings (either as walk-ins, or by appointment) — there were 345, in all. Then I used ggmap's geocode function to convert the addresses into latitude/longitude coordinates:
addresses <- with(visit.wine, paste(Address, City, "CA", sep=", ") ) locs <- geocode(addresses)
Finally, I chose a map center of the city of Saint Helena, CA (the launch-point for my bike rides in the Valley) and grabbed the map from Google using ggmap's qmap function. Then, I superimposed a scatterplot of the winery locations using the standard ggplot2 geom_point function:
map.center <- geocode("Saint Helena, CA") SHmap <- qmap(c(lon=map.center$lon, lat=map.center$lat), source="google", zoom=12) SHmap + geom_point( aes(x=lon, y=lat, colour=App), data=visit.wine) + scale_colour_manual(values=c("dark blue","orange"))+ labs(colour="Appointment Required")
And here's the final map:
My next project will be to use mapdist(mode="cycling") to find all the wineries within a 30 minute ride from the town center. (You can download my R script if you want to try it yourself.)
In short, the ggmap package is an incredibly handy tool for creating data visualizations based on geocoded data, where you want to use a Google map or similar as the background reference. If you'd like to create such maps yourself, as long as you have R 2.14 or later (or Revolution R 6 or later) you can install the ggmap package now from CRAN witht the R command: