-1

I'm trying to retrieve a tile of Downtown Los Angeles from Google Maps using ggmap and ggplot2, specifically the function get_googlemaps, and then I want to overlay a shapefile over it displaying the boundary of Downtown Los Angeles. (The shapefile is from the LA Times, here) I clipped the shapefile to only show DTLA by doing this to retain the attributes:

downtown <- readOGR(".","neighborhoods.shp")
downtown@data$id <- rownames(downtown@data)
downtown.points <- fortify(downtown,region="id")
downtown.df <- join(downtown.points,downtown@data,by="id")`

I then clipped out everything that wasn't DTLA by subsetting

downtown.df <- downtown.df[downtown$slug="downtown",]

downtown.df plots fine with

ggplot(downtown.df, aes(x=long,y=lat)) + geom_polygon(fill=NA, color="black") + coord_fixed().

Then I go to get the DTLA map using ggmap.

dtlamap <- get_googlemap(location = c(lon=-118.250347,lat=34.044899), 
    zoom=14,
    maptype="roadmap",
    style="element:labels|visibility:off|
    style=feature:administrative.land_parcel|
    visibility:off|style=feature:administrative.neighborhood
    |visibility:off")
ggmap(dtlamap)

The map that appears is correct, but the latitude and longitude are warped. The latitude range is 29.74 to 29.78 and the longitude range is -95.39 to -95.34. So when I try to plot downtown.df on top of it, it doesn't display because the coordinates are off.

The polygon for the DTLA shp file is in the WDS84 system and I assume the Google Maps is too, or it wouldn't have pulled up the correct map...?

I looked at this google group answer here but I can't get the solution to work for me using get_googlemap.

  • 1
    Where does join come from? Please try and include all packages that you are using in your code with library(...). Also this line downtown.df <- downtown.df[downtown$slug="downtown",] is invalid because you probaly meant to use == instead of = - please try and make a reproducible example. – Spacedman Jan 21 '18 at 11:32
1

One thing, you can't use downtown.df[downtown$slug="downtown",] because dim(downtown) != dim(downtown.df). You need to select before fortify. Also, shapefile came in NAD 83, so you need to reproject it:

downtown <- spTransform(downtown, '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ')
downtown@data$id <- rownames(downtown@data)
downtown <- downtown[downtown$slug == 'downtown',]
downtown.points <- fortify(downtown,region="id")
downtown.df <- join(downtown.points,downtown@data,by="id")

Second, get_googlemap function uses center argument, not location (this one is for get_map function) and third, ggmap doesn't work fine when the polygon is bigger than extent, so use zoom = 13 (or you can cut polygon with extent of zoom = 14):

dtlamap <- get_googlemap(center = c(lon=-118.250347,lat=34.044899), 
                         zoom=13,
                         maptype="roadmap",
                         style="element:labels|visibility:off|
                         style=feature:administrative.land_parcel|
                         visibility:off|style=feature:administrative.neighborhood
                         |visibility:off")

ggmap(dtlamap) + geom_polygon(data = downtown.df, aes(x=long, y = lat, group = group), alpha = 0.5)

enter image description here

  • 1
    Thank you. This really helped explain what I was doing incorrectly. I appreciate the time you took to answer this. :) – J. Splitz Jan 23 '18 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.