A choropleth map showing the spatial distribution of Brazilian GDP by city. Colors represent the economic sector with the highest share of contribution to total city GDP. Data comes from the most recent National Accounts data from IBGE (2023).
data-visualization
brazil
maps
ggplot2
english
Author
Vinicius Oike
Published
April 20, 2024
Code
# Librarieslibrary(geobr)library(sf)library(ggplot2)library(dplyr)library(tidyr)library(stringr)library(MetBrewer)library(showtext)# Color palettecores =met.brewer("Hiroshige", 20)[c(1, 8, 15, 20)]# Import Gill Sans locallyfont_add("Gill Sans", "GillSans.ttc")showtext_auto()# Data ------------------------------------------------------------------# Shapes# Import city shapefilemunis <-read_municipality(year =2022, showProgress =FALSE)# Import state shapefilegeostate <-read_state(showProgress =FALSE)# Import city data from Brazil (see data/raw/R/painel_municipios.R)panel <- readr::read_csv(here::here("static/data", "cities_brazil.csv"))## Data cleaning --------------------------------------------------------# Vector to select GDP columnspib_cols =c("pib_agriculture", "pib_industrial", "pib_services","pib_govmt_services")# Compute GDP share by citypib_share = panel |>select(all_of(c("code_muni", "pib", pib_cols))) |>pivot_longer(cols =3:last_col(), names_to ="sector", values_to ="total") |>mutate(share = total / pib *100,# Improve text labelssector_label =str_remove(sector, "pib_"),sector_label =str_to_title(sector_label),sector_label =str_replace(sector_label, "Govmt_services", "Government"),sector_label =str_replace(sector_label, "Industrial", "Industry") )# Convert to widertab_pib_share = pib_share |>pivot_wider(id_cols ="code_muni",names_from ="sector",values_from ="share" )# Find the most relevant economic sector by citytop_pib_share = pib_share |>filter(share ==max(share), .by ="code_muni") |>select(code_muni, top_sector = sector_label)# Join tabular data with city shapefilemap_share = munis |>left_join(tab_pib_share, by ="code_muni") |>left_join(top_pib_share, by ="code_muni")# Map -------------------------------------------------------------------m1 =ggplot() +# State bordersgeom_sf(data = geostate, lwd =0.5, color ="gray30", fill ="white") +# City colorsgeom_sf(data =na.omit(map_share),aes(fill = top_sector, color = top_sector),lwd =0.01 ) +# Remove junk from the east coastcoord_sf(xlim =c(NA, -35)) +# Use Hiroshige colorsscale_fill_manual(name ="", values = cores) +scale_color_manual(name ="", values = cores) +labs(title ="Top GDP contribution by City (2021)",subtitle ="Colors represent the most relevant economic contributing group in each city",caption ="Source: National Accounts, IBGE (2023)") + ggthemes::theme_map(base_family ="Gill Sans") +# Thematic elementstheme(# Backgroundpanel.background =element_rect(fill ="#ffffff", color =NA),plot.background =element_rect(fill ="#ffffff", color =NA),# Legendlegend.position ="left",legend.justification =0.5,legend.key.size =unit(1, "cm"),legend.key.width =unit(1.5, "cm"),legend.text =element_text(size =12),legend.margin =margin(),# Labels (title, subtitle)plot.title =element_text(size =38, hjust =0.5),plot.subtitle =element_text(size =14, hjust =0.5),# Plot marginsplot.margin =margin(10, 5, 5, 10) )# Histogram -------------------------------------------------------------pib_bars = top_pib_share |>count(top_sector) |>mutate(top_sector = forcats::fct_rev(top_sector))p_hist =ggplot(pib_bars, aes(top_sector, n)) +geom_col(aes(fill = top_sector)) +# Number labelsgeom_text(aes(y = n, label = n, color =factor(c(0, 0, 0, 1))),size =4,family ="Gill Sans",nudge_y =c(-200, -200, 200, -200) ) +# Text labelsgeom_text(aes(y =50, label = top_sector, color =factor(c(0, 0, 0, 1))),family ="Gill Sans",size =4,hjust =0 ) +# Flip axiscoord_flip() +scale_fill_manual(values =rev(cores)) +scale_color_manual(values =c("black", "white")) +labs(x =NULL, y =NULL, title ="Number of cities") +guides(fill ="none", color ="none") +theme_minimal(base_family ="Gill Sans") +theme(plot.title =element_text(size =14, hjust =0.5),panel.grid.major =element_blank(),panel.grid.minor =element_blank(),axis.text =element_blank() )
GDP Contribution by city
The number of cities where Agriculture is the main driver of the local economy rose from 1049 in 2020 to 1272 in 2021. Agriculture is most prevalent in the Midwest cities.
Public Administration is the most prevalent economic activity among cities in the North and Northeast of the country. General services are most prevalent in the Southeast.
Industrial activities constitute the primary economic driver for fewer than 8% of cities. The few industrial cities are spatially disperse across the country, exhibiting no clear pattern.
Dados: IBGE, Contas Nacionais, Produto Interno Bruto dos Municípios (2023)