A Napa Valley wine tasting map, made with R and ggmap

July 9, 2012

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

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:

Napa Valley wine 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: 


Happy mapping!

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)