How keep information from shapefile after fortify()

后端 未结 4 878
萌比男神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:18

    Assumption that data$CD_GEOCODI and shape@data are both unique for each row. Below is a better and simpler answer:

    library(dplyr)
    library(ggplot2)
    
    # fortify the shape file
      map.df <- fortify(shape, region ="CD_GEOCODI")
    
    # change data$CD_GEOCODI to character, as shp.df$CD_GEOCODI is of character class.
      data <- data %>% mutate(data, CD_GEOCODI = as.character(CD_GEOCODI))
    
    # merge data
      map.df <- left_join(map.df, data, by=c('id'='CD_GEOCODI'))
    

提交回复
热议问题