One of the great pieces of the new ggmap
package is the geocoding functionality. Other R functions can be used to geocode but they fail to provide detailed output like geocode accuracy which is often critical. You need to know if the lat/long in the output refers to a rooftop location or a city center, for example. In the past, our work around has been to use Python (e.g., the module pygeocoder
) which allows us to get detailed output. But ggmap
makes this even easier.
For example, you could simply type the following to grab coordinates:
library(ggmap)
geocode("someplace, nyc")
## lon lat
## 1 -74.01 40.71
But this won't tell you what the coordinates refer to. Without additional detail you might assume that this would be a good geocode. To address this ggmap
provides some alternatives using the output
argument. For example:
geocode("someplace, nyc", output = "more")
## lon lat type loctype address north south east
## 1 -74.01 40.71 locality approximate new york, ny, usa 40.92 40.5 -73.7
## west postal_code country administrative_area_level_2
## 1 -74.26 <NA> united states new york county
## administrative_area_level_1 locality street streetNo point_of_interest
## 1 new york new york <NA> NA <NA>
## query
## 1 someplace, nyc
Pay particular attention to loctype
in the output where you can see that this particular geocode is only approximate. Approximate may not be accurate enough depending on your project. Let's try a real address:
# address for the empire state building
mygeocode <- geocode("350 5th Ave, New York, NY 10118", output = "more")
mygeocode$loctype
## [1] rooftop
## Levels: rooftop
Here we see a rooftop geocode, a geocode we can have much more confidence in. Now let's try mapping:
qmap(location = c(lon = mygeocode$lon, lat = mygeocode$lat), zoom = 17, maptype = "satellite")
A simple, concise, and extremely impressive application for a new R user (and old SAS user) to see! How about batch input processing a list of addresses from a .csv file, getting the latitude/longitude coordinates, and writing those to a file? (It is extremely nice to see a R post that carefully, in understandable detail, documents processes-being new to R.)