library(readODS)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(sidrar)
library(tidyr)
library(sf)
library(viridis)
library(RColorBrewer)
library(basedosdados)
library(DBI)
library(bigrquery)
set_billing_id("1055074884926")
bq_auth()
con <- dbConnect(
bigrquery::bigquery(),
billing = "1055074884926",
project = "My First Project"
)
# Ler o shapefile dos municípios
shpMun <- st_read("G:/Meu Drive/AGR/BR_Municipios_2021.shp")
## Reading layer `BR_Municipios_2021' from data source
## `G:\Meu Drive\AGR\BR_Municipios_2021.shp' using driver `ESRI Shapefile'
## Simple feature collection with 5572 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -73.99045 ymin: -33.75118 xmax: -28.84764 ymax: 5.271841
## Geodetic CRS: SIRGAS 2000
# Ler o shapefile dos estados
shpUFs <- st_read("G:/Meu Drive/AGR/BR_UF_2021.shp")
## Reading layer `BR_UF_2021' from data source `G:\Meu Drive\AGR\BR_UF_2021.shp' using driver `ESRI Shapefile'
## Simple feature collection with 27 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -73.99045 ymin: -33.75118 xmax: -28.84764 ymax: 5.271841
## Geodetic CRS: SIRGAS 2000
# Ajustar o nome da coluna para os municípios
mapaMun <- shpMun %>%
st_transform(crs = st_crs(shpMun)) %>%
mutate(CD_MUN = as.character(CD_MUN)) # Ajuste conforme o nome real da coluna
# Ajustar o nome da coluna para os estados
mapaUF <- shpUFs %>%
st_transform(crs = st_crs(shpUFs)) %>%
mutate(CD_UF = as.character(CD_UF)) # Ajuste conforme o nome real da coluna
pop<- basedosdados::read_sql('
SELECT * FROM `basedosdados.br_ibge_populacao.municipio`
')
save(pop, file="pop.Rda")
load("pop.Rda")
Bovino e Suíno
#boi
#https://apisidra.ibge.gov.br/values/t/3939/n6/all/v/all/p/1980,1990,2000,2010,2020,2021,2022/c79/2670
boi =
'/t/3939/n6/all/v/all/p/2021/c79/2670' %>%
get_sidra(api=.)
Vou fazer o mapa para 2021
pop2021<-pop %>% filter(ano==2021)
boi2021<-boi %>% select(Valor, `Município (Código)`)
colnames(boi2021)<-c("boi", "id")
colnames(pop2021)<-c("ano", "sigla", "id", "pop")
bmapa<-merge(boi2021, pop2021, by="id")
bmapa$intensidade_boi<-bmapa$boi/bmapa$pop
ggplot(bmapa, aes(x = intensidade_boi)) +
geom_density() + theme_clean()
mapaMun$id <- mapaMun$CD_MUN
basemapa2<-merge(bmapa, mapaMun, by="id")
library(sf)
# Verifique se basemapa2 e shpUFs são objetos sf
class(basemapa2)
## [1] "data.frame"
class(shpUFs)
## [1] "sf" "data.frame"
basemapa2 <- st_as_sf(basemapa2)
mean(basemapa2$intensidade_boi, na.rm = T)
## [1] 3.520204
# Criando a nova variável 'intensidade' com a ordem desejada e removendo NAs
basemapa2 <- basemapa2 %>%
mutate(intensidade = factor(case_when(
intensidade_boi < 1 ~ "Menor que 1",
intensidade_boi >= 1 & intensidade_boi < 3 ~ "Entre 1 e 3",
intensidade_boi >= 3 & intensidade_boi < 10 ~ "Entre 3 e 10",
intensidade_boi >= 10 & intensidade_boi < 25 ~ "Entre 10 e 25",
intensidade_boi >= 25 ~ "Maior que 25"
), levels = c("Maior que 25", "Entre 10 e 25", "Entre 3 e 10", "Entre 1 e 3", "Menor que 1")))
# Fazendo o gráfico com a escala viridis e removendo NAs da legenda
test1 <- ggplot(data = basemapa2) +
geom_sf(aes(fill = intensidade), color = NA) +
geom_sf(data = shpUFs, fill = NA, color = "black", size = 0.5) +
scale_fill_viridis_d(name = "Cabeças de boi / \n número pessoas", option = "viridis", direction = -1, na.translate = FALSE, guide = guide_legend(nrow = 2, byrow = TRUE)) +
labs(title = "", subtitle = "", fill = "Intensidade") +
theme_minimal() +
theme(legend.title = element_text(),
legend.position = "bottom",
legend.box.margin = margin(0, 0, 0, 0)) +
coord_sf(expand = FALSE)
# Exibindo o gráfico
test1