How keep information from shapefile after fortify()

后端 未结 4 917
萌比男神i
萌比男神i 2021-02-05 10:06

How can I keep polygons\'s information after shapefile? Let me try to explain:

I have a shapefile with this data:

> head(mapa@data)
         ID      C         


        
4条回答
  •  甜味超标
    2021-02-05 10:22

    Since you did not provide your shapefile or data, it's impossible to test, but something like this should work:

    # not tested...
    library(plyr)      # for join(...)
    library(rgdal)     # for readOGR(...)
    library(ggplot2)   # for fortify(...)
    
    mapa <- readOGR(dsn=".",layer="shapefile name w/o .shp extension")
    map@data$id <- rownames(mapa@data)
    mapa@data   <- join(mapa@data, data, by="CD_GEOCODI")
    mapa.df     <- fortify(mapa)
    mapa.df     <- join(mapa.df,mapa@data, by="id")
    
    ggplot(mapa.df, aes(x=long, y=lat, group=group))+
      geom_polygon(aes(fill=Population))+
      coord_fixed()
    

提交回复
热议问题