How keep information from shapefile after fortify()

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()

Maybe this is a slightly simpler solution

library(dplyr)
library(ggplot2)

# fortify the shape file
  map.df <- fortify(shape, region ="CD_GEOCODI")

# merge data
  map.df <- left_join(map.df, data, by=c('id'='CD_GEOCODI'))

Tags:

R

Ggplot2

Rgdal