首页 > 解决方案 > R - plot_brmap(州界)

问题描述

我制作了一张巴西城市的地图,plot_brmap()我想显示州界。

但我不知道该怎么做。我一直在尝试mapa2_data用透明颜色填充(状态图)并用mapa_data(城市地图)绘制。但它不起作用。

我一直在尝试:

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

    # 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 - Cooxupé e Cocapec ----
    load(file="dados/data.Rdata")

    # Agrupa por razao social e 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"
                                       )

    dScore_data$code_state <- as.numeric(dScore_datac$code_state)
    mapa2_data <- plot_brmap(estados_sp_mg, 
                                       data_to_join = dScore_data, 
                                       join_by = c("State" = "code_state"),
                                       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()


标签: rmaps

解决方案


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)

添加到地图情节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)

推荐阅读