Pregunta 1
rm(list=ls())
library(tidyverse)
library(data.table)
library(ggplot2)
library(leaflet)
library(sf)
library(chilemapas)
egresos<- data.table(readRDS("egresos.rds"))
establecimientos<-data.table(fread("Establecimientos.csv"))
class(egresos)
## [1] "data.table" "data.frame"
class(establecimientos)
## [1] "data.table" "data.frame"
Pregunta 2
egresos[,.N, by=.(egresos$EDAD)]
ggplot(data=egresos,aes(x=`EDAD`))+geom_bar(color="green") + labs(x="Age", y="Peolple", title = "Outflows", subtitle = "By Age") + theme(axis.text.x = element_text(angle=0, vjust=0.5))
Pregunta 3
P3<-merge(x=egresos, y=establecimientos, by.x = "ESTAB", by.y = "Código Vigente", all.x = T)
Pregunta 4
P4 <- P3[!duplicated(`Nombre Oficial`), .(`Nombre Oficial`, `LONGITUD [Grados decimales]`, `LATITUD [Grados decimales]`,`Nombre Comuna`)]
Pregunta 5
texto1 <-paste("Comuna:",P4$`Nombre Comuna`, "<br/>", "Establecimiento:", P4$`Nombre Oficial`)
mapa1<- leaflet() %>%
addTiles() %>%
addMarkers(lat = P4$`LATITUD [Grados decimales]`, lng = P4$`LONGITUD [Grados decimales]`, popup = texto1)
mapa1
Pregunta 6
P6<- P3[,.N, by=.(P3$`Nombre Región`,P3$`Código Región`)]
Region<- data.table(chilemapas::generar_regiones())
Region[,codigo_region:=as.double(Region$codigo_region)]
mapa2<-merge(x=Region, y=P6, by.x = "codigo_region", by.y = "P3.1", all.x = T)
Pregunta 7 + Bonus
mapa2<-st_sf(mapa2)
bins<-seq(0,600000,30000)
paleta<-colorBin("Blues" ,domain = c(mapa2$N), bins = bins)
labels <- sprintf(
"<strong>Region: %s</strong><br/> Numero:%s</strong><br/>",
mapa2$P3, mapa2$N
) %>% lapply(htmltools::HTML)
leaflet(mapa2)%>%
addProviderTiles(provider = providers$OpenStreetMap.BZH)%>%
addPolygons(color = ~paleta(mapa2$N),
weight = 0.5,
fillOpacity = 1,
label = labels)%>%
addLegend(pal = paleta, values = c(mapa2$N), opacity = 2,position = "bottomright",title ="Establecimientos")
Pregunta Bonus