r - 在 ggplot2 上覆盖 ggmap 、 geom_polygon(shape file)
问题描述
我有一个形状文件,我想用 ggplot 将它发布在谷歌地图的顶部,但是通过 geom_polygon(ggplot2) 发布的行显示没有意义
我的代码:
######## the shape file ftp://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/Brasil/BR/
download.file("ftp://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/Brasil/BR/br_municipios.zip",temp)
data <- unz(temp, "BRMUE250GC_SIR.shp")
data.shape<-st_read(data)
####### the map from ggmap
mapa_edital_guarulhos <- get_map(location="GUARULHOS-SP",zoom=11,color = "bw",
maptype = "roadmap")
mapa_edital_guarulhos_01<- ggmap(mapa_edital_guarulhos)
########## plotting with ggplot
mapa_edital_guarulhos1 <- mapa_edital_guarulhos_01 +
geom_polygon(aes(x=long,y=lat, group=group), data=shape.sp,
color='black',alpha=0)
结果:
箭头显示在我的图中没有意义的线条,仅绘制相同间隔的形状:
plot(shape.sp,xlim=c(-47.25,-46.95),ylim=c(-23.1,-22.7))
我的代码有什么问题?谢谢
解决方案
尝试使用 geom_sf 而不是 geom_polygon
首先安装ggplot2的开发版本
devtools::install_github('tidyverse/ggplot2')
# read using sf instead of readOGR
library(sf)
# something like:
data.shape <- st_read("www./BRMUE250GC_SIR.shp")
####### the map from ggmap
mapa_edital_guarulhos <- get_map(location="GUARULHOS-SP",zoom=11,color = "bw",
maptype = "roadmap")
mapa_edital_guarulhos_01<- ggmap(mapa_edital_guarulhos)
########## plotting with ggplot
mapa_edital_guarulhos1 <- mapa_edital_guarulhos_01 +
geom_sf(data = data.shape, color = 'black', alpha=0)
如果没有数据集,很难对此进行测试。但让我知道这是否有效!我已经看到这解决了您之前遇到的问题。
推荐阅读
- ssh - SSH 会话无响应 - 我如何激活它或通过其他方式重新启动
- regex - 使用正则表达式验证输入字段
- java - NoSuchMethodError TabLayout.OnTabSelectedListener
- java - 回调和事件之间的区别
- c++ - C++正确使用静态多态
- rust - 特征类型不匹配解决`
- datatable - jQuery DataTable 不显示显示条目、搜索并且在页面刷新之前不排序
- java - 我收到运行时错误 com.twlapps.goolarr E/AndroidRuntime: FATAL EXCEPTION: main 并且我的应用程序在我打开后崩溃
- html - 如何使用 webp 图片并支持 safari
- java - 方法和字段名称不应仅因大小写而异 (squid:S1845) 误报