R - brazilmaps (border of states)

Asked

Viewed 251 times

1

I created a map of SP and MG states using brazilmaps().

On this map I filled some cities with values.

However, I would like to make the border of states appear on Plot.

How do I do that?

Follow what I’m trying to.

library(brazilmaps)
library(maptools)
library(RColorBrewer)
library(tidyverse)

    # Definindo paleta de cores ----
    createColors  <- colorRampPalette(c("yellow","red","darkgreen"))

    # Definindo cidades e estados ----
    cidades_sp_mg <- get_brmap(geo = "City", geo.filter = list(State = c(31,35))
                               ,class = "SpatialPolygonsDataFrame")

    estados_sp_mg <- get_brmap(geo = "State", geo.filter = list(State = c(31,35))
                               ,class = "SpatialPolygonsDataFrame")


    # Criando Mapa ----
    load(file="dados/data.Rdata")

    # Agrupa por municipio, conta quantas coop. cada municipio tem
    dScore_data <- data %>% group_by(cod_municipio_ibge) %>% count()


    # Gera mapa das cidades
    mapa_data <- plot_brmap(cidades_sp_mg, 
                                       data_to_join = dScore_data, 
                                       join_by = c("City" = "cod_municipio_ibge"),
                                       var = "n"
                                       )


    mapa2_data <- plot_brmap(estados_sp_mg, 
                                       data_to_join = dScore_data, 
                                       join_by = c("State" ="cod_uf"),
                                       var = "n"
    )


    MinhasCores_data  <- createColors(4)

    png("dados/Cities.png", width = 800, height = 600) 

    print(mapa_data) +
      scale_fill_gradientn(colours=MinhasCores_data,name="Qtd.", na.value = "lightgray") + 
      labs(title="Cities",caption = "Font") + 
      theme(legend.title = element_text(face="bold"),title = element_text(face="bold"),
            plot.title = element_text(hjust = 0.5,size=20),
            plot.caption = element_text(hjust = 0.5,size = 10)) 
+ plot(mapa2_data)

    dev.off()

With the + plot(mapa2_data) gives error. But without this command, states are generated and cities filled normally, without state borders.

2 answers

1

A viable option is to use the ggplot2 package and the geom_polygon function to read the data, adopting "color" as in the example below. It is possible to change the color and thickness to your taste.

ggplot(mapa_data) + 
  geom_polygon(..., 
               color = "white", size = 0.1)

0


I solved the problem as follows:

library(geobr)

# Definindo bordas de SP e MG     
mapa_SP <- read_state(code_state="SP", year=2018)
mapa_MG <- read_state(code_state="MG", year=2018)

After that, I added these files to the map Plot with the geom_sf()

print(mapa_data) +
      scale_fill_gradientn(colours=MinhasCores_data,name="Qtd.", na.value = "lightgray") + 
      labs(title="Cities",caption = "Font") + 
      theme(legend.title = element_text(face="bold"),title = element_text(face="bold"),
            plot.title = element_text(hjust = 0.5,size=20),
            plot.caption = element_text(hjust = 0.5,size = 10)) +
  geom_sf(data=mapa_SP, fill="transparent", color="black", size=0.8, show.legend = FALSE) + 
  geom_sf(data=mapa_MG, fill="transparent", color="black", size=0.8, show.legend = FALSE)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.