R - Map of Brazilian cities

5

Good morning,

I'm trying to create a Brazilian map that shows how many cooperatives are present in each city.

I have a table with the municipalities IBGE code and the amount of coop.

I took shapefile from the IBGE website:

link

and I tried to follow this example:

link

However, when applied to municipalities, the R will process for a few minutes and suddenly crashes. What can it be?

One more question, I would like to leave the division for States and Regions appearing. Is it possible?

Here are some lines of code:

shpmun <- readOGR("G:/Lupa Econômica/Ricardo/Qtd de 
Coop/dados/MAPA/BRMUE250GC_SIR.shp", stringsAsFactors=FALSE, encoding="UTF-8")

load('G:/Lupa Econômica/Ricardo/Qtd de 
Coop/dados/Rais_Coop_Estb_2016.RData') 

Rais_Coop_Estb_2016$Cont <- as.numeric(c(1))

Rais_Coop_Estb_2016mapamun <- Rais_Coop_Estb_2016 %>% group_by(Município) 
%>% mutate(cumsum = cumsum(Cont))
Rais_Coop_Estb_2016mapamun <- Rais_Coop_Estb_2016mapamun %>% 
group_by(Município) %>%  summarise(Score= max(cumsum))
Rais_Coop_Estb_2016mapamun <- as.data.frame(Rais_Coop_Estb_2016mapamun)

brasileiropgmun <- merge(shpmun,Rais_Coop_Estb_2016mapamun, by.x = 
"CD_GEOCMU", by.y = "Município")

proj4string(brasileiropgmun) <- CRS("+proj=longlat +datum=WGS84 +no_defs") 
#adicionando coordenadas geográficas

Encoding(brasileiropgmun$NM_MUNICIP) <- "UTF-8"

brasileiropgmun$Score[is.na(brasileiropgmun$Score)] <- 0 

display.brewer.all()

pal <- colorBin("YlGn",domain = NULL,n=20) #cores do mapa

state_popupmun <- paste0("<strong>Município: </strong>", 
                  brasileiropgmun$NM_MUNICIP, 
                  "<br><strong>Pontos: </strong>", 
                  brasileiropgmun$Score)
leaflet(data = brasileiropgmun) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(fillColor = ~pal(brasileiropgmun$Score), 
          fillOpacity = 0.8, 
          color = "#BDBDC3", 
          weight = 1, 
          popup = state_popupmun) %>%
addLegend("bottomright", pal = pal, values = ~brasileiropgmun$Score,
        title = "Pontos Conquistados",
        opacity = 1)

** The caption is not appearing either.

    
asked by anonymous 25.09.2018 / 15:11

1 answer

6

The latest versions of ggplot2 have map-specific geometry. The big advantage is that you do not need to join the data.frame with the data with the spatial object (but for that the spatial object needs to be converted to a format that ggplot understands with fortify ). The important thing is that they both have an identification column (the IBGE municipalities code, in this case) with the same name.

To plot the margins of states, regions, etc., it is best to use a separate object for each. They can be different shapefiles or a new spatial object generated by union of attributes using R itself (with the rgeos package, for example).

I'm using shapefiles that I already own, based on latest version of IBGE FTP .

library(ggplot2)

shpMun <- rgdal::readOGR('./shapefiles', 'municipios')
mapaMun <- fortify(shpMun, region = 'GEOCODE')

shpUFs <- rgdal::readOGR('./shapefiles', 'ufs')

# Simulando as informações de cooperativas
dadosCoop <- data.frame(
  GEOCODE = shpMun@data$GEOCODE,
  cooperativas = sample(1:20, length(shpMun@data$GEOCODE), replace = TRUE) )
#

ggplot(dadosCoop) +
  geom_map(
    map = mapaMun,
    color = 'gray60', size = .1,
    aes(map_id = GEOCODE, fill = cooperativas)
  ) +
  expand_limits(
    x = mapaMun$long,
    y = mapaMun$lat
  ) +
  scale_fill_gradient(
    low = 'lightyellow',
    high = 'darkred'
  ) +
  geom_path(
    data = shpUFs,
    size = .2,
    aes(long, lat, group = group)
  ) +
  coord_map() +
  theme_void() +
  theme(legend.position = c(0.2,0.3))

ggsave('mapa.png', width = 8, height = 8, dpi = 100)

    
27.09.2018 / 00:41