library(ggmap) # this also loads ggplot2 # ggmap and ggplot2 installed in the virtual desktop # internet required since we are downloading data to construct maps. # This file contains material from the edX.org course Programming with R for Data Science # and https://cran.r-project.org/web/packages/ggmap/ggmap.pdf # The ggmap package: Interfacing ggplot2 and RGoogleMaps. # Two steps in making a mapp with ggmap: # 1. download raster dta for the map; # 2. create the map with ggmap(), then add layers. # To download raster data, need to specify # 1. location of center # 2. the zoom factor # Two ways to specify location for map download. # location or address mylocation="Eastern Illinois University" # or by latitude and longitude # geocodes finds latitude and longitude geocode("Eastern Illinois University") # Information from URL: # http://maps.googleapis.com/maps/api/geocode/json?address=Eastern%20Illinois%20University&sensor=false # lon lat # 1 -88.17591 39.47803 mylocation=c(lon=-88.17591, lat=39.47803) # both methods will give the same results when specifying the center for the map to download # zoom factor: 3=continent, 10=city, 21=building mymap=get_map(location=mylocation,color="color", source="google", maptype="satellite", zoom=16) ggmap(mymap, extent="panel", ylab="Latitude", xlab="Longitude") # Example Pg 6 # types of input geocode("houston texas") geocode("baylor university") # see known issues below geocode("1600 pennsylvania avenue, washington dc") geocode("the white house") geocode(c("baylor university", "salvation army waco")) # types of output geocode("houston texas", output = "latlona") geocode("houston texas", output = "more") geocode("Baylor University", output = "more") str(geocode("Baylor University", output = "all")) # see how many requests we have left with google geocodeQueryCheck() geocode("one bear place, waco, texas") geocode("houston texas", force = TRUE) # Example Pg 8 # geom_leg : single line segments with rounded ends map <- get_map( location = c(-77.0425, 38.8925), # painfully picked by hand source = "google", zoom = 14, maptype = "satellite" ) ggmap(map) (legs_df <- route( "the white house, dc", "lincoln memorial washington dc", alternatives = TRUE )) ggplot(data = legs_df) + geom_leg(aes( x = startLon, xend = endLon, y = startLat, yend = endLat )) + coord_map() ggplot(data = legs_df) + geom_leg(aes( x = startLon, xend = endLon, y = startLat, yend = endLat, color = route )) + coord_map() ggmap(map) + geom_leg( aes( x = startLon, xend = endLon, y = startLat, yend = endLat ), data = legs_df, color = "red" ) # Example pg 3 # bb2bbox : convert a bb specification to a bbox specification # data frame bounding box specification gc <- geocode("white house, washington dc") map <- get_map(gc) # type in the url in a web browser (bb <- attr(map, "bb")) (bbox <- bb2bbox(bb)) stamMap <- get_stamenmap(bbox) # Error, map from url should be: # http://tile.stamen.com/terrain/10/291/390.png ggmap(map) ggmap(map)+geom_point(aes(lon,lat),data=gc,colour="red",size=3) ggmap(stamMap)+geom_point(aes(x=lon, y=lat),data=gc, colour = "red", size=3) ) # Error in ggmap(stamMap) : object 'stamMap' not found # Example Pg 4 # Wind data from Hurricane Ike # wind dataframe from ggmap # from data calc_zoom(lon, lat, wind) # from range lon_range <- extendrange( wind$lon ) lat_range <- extendrange( wind$lat ) calc_zoom(lon_range, lat_range) # from bounding box box <- make_bbox(lon, lat, data = crime) calc_zoom(box) # Example Pg 16 map <- get_map() map str(map) ggmap(map) ## Not run: # not run by check to reduce time; also, # osm may error due to server overload (map <- get_map(maptype = "roadmap")) (map <- get_map(source = "osm")) # ERROR (map <- get_map(source = "stamen", maptype = "watercolor")) map <- get_map(location = "texas", zoom = 6, source = "stamen") ggmap(map, fullpage = TRUE) # Lightly cleaned Houston crime from January 2010 to August 2010 geocoded with Google Maps # http://www.houstontx.gov/police/cs/stats2.htm head(crime) # Example Pg 14 # markers and paths are easy to access d <- function(x=-95.36, y=29.76, n,r,a){ round(data.frame( lon = jitter(rep(x,n), amount = a), lat = jitter(rep(y,n), amount = a) ), digits = r) } df <- d(n=50,r=3,a=.3) map <- get_googlemap(markers = df, path = df,, scale = 2) ggmap(map) ggmap(map, extent = "device") + geom_point(aes(x = lon, y = lat), data = df, size = 3, colour = "black") + geom_path(aes(x = lon, y = lat), data = df) gc <- geocode("waco, texas", source = "google") center <- as.numeric(gc) ggmap(get_googlemap(center = center, color = "bw", scale = 2), extent = "device") # the scale argument can be seen in the following # (make your graphics device as large as possible) ggmap(get_googlemap(center, scale = 1), extent = "panel") # pixelated ggmap(get_googlemap(center, scale = 2), extent = "panel") # fine # archiving; note that you must meet google's terms for this condition map <- get_googlemap(archiving = TRUE) # default is Houston map <- get_googlemap() ggmap(map) # Example Pg 20 # Stitches map tiles to get a map image from Stamen Maps. gc <- geocode("baylor university") google <- get_googlemap("baylor university", zoom = 15) ggmap(google) + geom_point(aes(x = lon, y = lat), data = gc, colour = "red", size = 2) bbox <- c(left = -97.132, bottom = 31.536, right = -97.105, top = 31.560) ggmap(get_stamenmap(bbox, zoom = 13)) ggmap(get_stamenmap(bbox, zoom = 14)) ggmap(get_stamenmap(bbox, zoom = 15)) # ERROR see links; png instead of jpg # See examples pg 21 ggmap(get_stamenmap(bbox, maptype = "terrain", zoom = 14)) # ERROR ggmap(get_stamenmap(bbox, maptype = "toner", zoom = 14)) # NO ERROR