1

I made a map of Brazilian cities using plot_brmap() and I want to show the state border.

But I don't know how to do it.

What I have been trying:

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

    # Define colors ----
    createColors  <- colorRampPalette(c("yellow","red","darkgreen"))

    # Define city and states (shapefiles) ----
    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")


    # Create the city map ----
    load(file="dados/data.Rdata")


    dScore_data <- data %>% group_by(cod_municipio_ibge) %>% count()


    # Merge city and shapefile
    mapa_data <- plot_brmap(cidades_sp_mg, 
                                       data_to_join = dScore_data, 
                                       join_by = c("City" = "cod_municipio_ibge"),
                                       var = "n"
                                       )

    # Create state map
    mapa2_data <- plot_brmap(estados_sp_mg, 
                                       data_to_join = dScore_data, 
                                       join_by = c("State" = "code_state"),

    )


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

    dev.off()

Any suggestions ?

1 Answer 1

0

library(geobr)

# Define borders SP and MG     
mapa_SP <- read_state(code_state="SP", year=2018)
mapa_MG <- read_state(code_state="MG", year=2018)

Add to the map plot with 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.