Con*_*rad 2 gis r shapefile ggplot2 ggmap
我正努力在谷歌地图上叠加邻居边界.我正在尝试遵循此代码.我的版本如下.你看到什么明显错了吗?
#I set the working directory just before this...
chicago = readOGR(dsn=".", layer="CommAreas")
overlay <- spTransform(chicago,CRS("+proj=longlat +datum=WGS84"))
overlay <- fortify(overlay)
location <- unlist(geocode('4135 S Morgan St, Chicago, IL 60609'))+c(0,.02)
ggmap(get_map(location=location,zoom = 10, maptype = "terrain", source = "google",col="bw")) +
geom_polygon(aes(x=long, y=lat, group=group), data=overlay, alpha=0)+
geom_path(color="red")
Run Code Online (Sandbox Code Playgroud)
任何见解都会非常感激.感谢您的帮助和耐心.
这对我有用:
library(rgdal)
library(ggmap)
# download shapefile from:
# https://data.cityofchicago.org/api/geospatial/cauq-8yn6?method=export&format=Shapefile
# setwd accordingly
overlay <- readOGR(".", "CommAreas")
overlay <- spTransform(overlay, CRS("+proj=longlat +datum=WGS84"))
overlay <- fortify(overlay, region="COMMUNITY") # it works w/o this, but I figure you eventually want community names
location <- unlist(geocode('4135 S Morgan St, Chicago, IL 60609'))+c(0,.02)
gmap <- get_map(location=location, zoom = 10, maptype = "terrain", source = "google", col="bw")
gg <- ggmap(gmap)
gg <- gg + geom_polygon(data=overlay, aes(x=long, y=lat, group=group), color="red", alpha=0)
gg <- gg + coord_map()
gg <- gg + theme_bw()
gg
Run Code Online (Sandbox Code Playgroud)

如果环境中的任何内容导致问题,您可能需要重新启动R会话,但是您可以在geom_polygon调用中设置线颜色和alpha 0填充(就像我一样).
你也可以这样做:
gg <- gg + geom_map(map=overlay, data=overlay,
aes(map_id=id, x=long, y=lat, group=group), color="red", alpha=0)
Run Code Online (Sandbox Code Playgroud)
而不是geom_polygon使你能够在一次调用和两次调用中绘制地图并执行美学映射(如果你根据其他值着色).