在ggplot2

时间:2018-05-04 15:50:00

标签: r ggplot2 shapefile ggmap

我有一个形状文件,我想在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)

结果:

enter image description here

箭头显示我的情节中没有意义的线条,仅绘制相同间隔的形状:

plot(shape.sp,xlim=c(-47.25,-46.95),ylim=c(-23.1,-22.7)) 

enter image description here

我的代码中存在什么问题?感谢

1 个答案:

答案 0 :(得分:1)

尝试使用geom_sf而不是geom_polygon

首先安装ggp​​lot2的开发版

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)

如果没有数据集,很难对此进行测试。但如果有效,请告诉我!我已经看到这解决了你以前遇到的问题。