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:
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:
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:
install.packages(c("ggmap","mapproj"))
Happy mapping!
I love how you've used the epic power of R to find loads of places to get drunk.
R. Is there anything it can't do?
Posted by: Chris Beeley | July 09, 2012 at 14:45
I don't suppose I could convince you to do this for Woodinville, Prosser, Yakima, and Walla Walla?
Posted by: Terry Christiani | July 09, 2012 at 15:44
@Terry, if you have a data file with the winery addresses I could give it a go.
Posted by: David Smith | July 09, 2012 at 15:45
Does ggmap's geocode function work for addresses outside the USA?
Posted by: Hywel | July 09, 2012 at 16:43
Awesome. Any way to plot while taking into account the winery's average pricing, or average Wine Spectator score? :-)
Posted by: Atul Butte | July 09, 2012 at 23:02
Would it be possible to use this package in conjunction with Google Vis to allow users to scroll over the wineries and see their names or other information?
Posted by: chandler | July 10, 2012 at 00:05
Now you just need to add an optimized route, a la Travelling Sales Problem.
Posted by: Pat Burns | July 10, 2012 at 02:16
Google Vis? No you don't need that. You just need OpenLayers and me to fix my webmaps package. I can work round it, see my dynamic 'World Wine Web' blog entry:
http://geospaced.blogspot.co.uk/2012/07/world-wine-web.html
Posted by: Barry Rowlingson | July 10, 2012 at 11:30
Please be sure to see the Napa Valley Vintners website at www.napavintners.com. I have been using their website and recommending it for over 2 years and it's really pretty neat. You can search for wineries that have (for example) picnicking, are dog friendly, have caves, have a chef on site -- on and on. Once you sort for your selection, the map appears immediately and you can print it out or click on the "dot" for any winery and it will bring up the winery's website/information.
If you're planning a trip, it's indispensable! Go play with it!
Posted by: Mary Pisor | July 11, 2012 at 12:21
@Hywel, I just tested the geocode function on a couple of addresses in the UK and Australia and it seems to work fine.
Posted by: David Smith | July 12, 2012 at 11:47
This is really, really cool. Like some others have mentioned, it would be sweet if we could create some maps like this for other wine regions. I am going to study this a bit more; doesn't seem too difficult to create.
When you say that we need a data file of all the wineries, what format? Like a CSV or Excel file?
Posted by: Mike | July 27, 2012 at 11:55
Dear Mr. Smith
I will be grateful if you can help me. I want to create a map by the help of ggmap. My problem is that the specific zooms are defined for the function "get_map ()”. It means that I can take a map of the size:
get_map ( … , zoom = 10, …)
or
get_map ( … , zoom = 9, …)
But I want a map at the between level. I mean a map at the size of 9.5 for instance:
get_map ( … , zoom = 9.5, …)
Is there any solution to take a map in the middle?
Many thanks for your consideration.
Regards,
Tinoush
Posted by: Tinoush Jamali | August 24, 2012 at 15:43
I believe the zoom levels are predetermined by the map provider, so you can't get "in-between" zoom levels.
Posted by: David Smith | August 24, 2012 at 15:45
Mr. Smith: In implementing your code, I received the following error message:
SHmap <- qmap(c(lon=map.center$lon, lat=map.center$lat), source="google", zoom=12)
Error in p + o : non-numeric argument to binary operator
Could you offer any suggestions on resolving this error?
Thank you!
Posted by: Michael Youngblood | September 08, 2012 at 14:38