Objetivo: Mapa de Pernambuco com percentual de pessoas sem instrução em cada Município.
library(leaflet)
library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 2.2.1 ✔ purrr 0.2.4
## ✔ tibble 1.4.2 ✔ dplyr 0.7.4
## ✔ tidyr 0.8.0 ✔ stringr 1.3.0
## ✔ readr 1.1.1 ✔ forcats 0.3.0
## ── Conflicts ──────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(stringr)
library(dplyr)
library(readr)
library(readxl)
pessoas <- read_csv("/home/ana/Mapa/Amostra_Pessoas_26.txt", col_names = FALSE)
## Parsed with column specification:
## cols(
## X1 = col_character()
## )
municipios <- read_excel("/home/ana/Unidades da Federação, Mesorregiões, microrregiões e municípios 2010.xls",skip=2)
#pessoas_5perc <- pessoas %>% sample_frac(size=0.05)
instrucao <- pessoas %>% transmute(COD_Municipio = str_sub(string = X1, start = 1, end = 7),
COD_Instrucao = str_sub(string = X1, start = 158, end = 158),
Instrucao = if_else(COD_Instrucao == "1", "Sem instrução e fundamental incompleto",
if_else(COD_Instrucao == "2","Fundamental completo e médio incompleto",
if_else(COD_Instrucao == "3", "Médio completo e superior incompleto",
if_else(COD_Instrucao == "4", "Superior completo", "Não determinado")))))
dados <- instrucao %>% left_join(municipios, by = c("COD_Municipio" = "Município"))
#"Mapa de PE com percentual de gente sem instruçao em cada municipio"
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.2-18, (SVN revision 718)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.1.2, released 2016/10/24
## Path to GDAL shared files: /usr/share/gdal/2.1
## GDAL binary built with GEOS: TRUE
## Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
## Path to PROJ.4 shared files: (autodetected)
## Linking to sp version: 1.2-7
shp <- readOGR("/home/ana/Mapa", "26MUE250GC_SIR", stringsAsFactors=FALSE, encoding="UTF-8")
## OGR data source with driver: ESRI Shapefile
## Source: "/home/ana/Mapa", layer: "26MUE250GC_SIR"
## with 185 features
## It has 3 fields
## Integer64 fields read as strings: ID
dados_aux = dados %>% group_by(COD_Municipio) %>% summarise(count = sum(COD_Instrucao==1)/(sum(COD_Instrucao==1)+sum(COD_Instrucao!=1)))
dados_aux
## # A tibble: 185 x 2
## COD_Municipio count
## <chr> <dbl>
## 1 2600054 0.585
## 2 2600104 0.685
## 3 2600203 0.779
## 4 2600302 0.779
## 5 2600401 0.795
## 6 2600500 0.838
## 7 2600609 0.780
## 8 2600708 0.756
## 9 2600807 0.781
## 10 2600906 0.782
## # ... with 175 more rows
dados_completos <- merge(shp,dados_aux, by.x = c("CD_GEOCODM"), by.y = c("COD_Municipio"))
qpal <- colorQuantile("Blues", dados_completos$count, n = 4)
map <- leaflet() %>% addTiles() %>% addPolygons(data=dados_completos,stroke = FALSE, fillColor = ~qpal(dados_completos$count), smoothFactor = 0.2,fillOpacity = 1) %>% addLegend("bottomright", pal = qpal, values = dados_completos$count,
title = "% sem instrução",
opacity = 1)
map