This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
#rm(list_ls())
library(data.table)
library(ggplot2)
library(janitor)
## Warning: package 'janitor' was built under R version 4.1.1
##
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
library(plotly)
## Warning: package 'plotly' was built under R version 4.1.1
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(RColorBrewer)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.1
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble 3.1.4 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## v purrr 0.3.4
## Warning: package 'tibble' was built under R version 4.1.1
## Warning: package 'tidyr' was built under R version 4.1.1
## Warning: package 'readr' was built under R version 4.1.1
## Warning: package 'purrr' was built under R version 4.1.1
## Warning: package 'forcats' was built under R version 4.1.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::between() masks data.table::between()
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::first() masks data.table::first()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks data.table::last()
## x purrr::transpose() masks data.table::transpose()
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.1.1
## No renderer backend detected. gganimate will default to writing frames to separate files
## Consider installing:
## - the `gifski` package for gif output
## - the `av` package for video output
## and restarting the R session
library(ggcorrplot)
## Warning: package 'ggcorrplot' was built under R version 4.1.1
library(maps)
## Warning: package 'maps' was built under R version 4.1.1
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.1.1
library(chilemapas)
## Warning: package 'chilemapas' was built under R version 4.1.1
## Loading required package: sf
## Warning: package 'sf' was built under R version 4.1.1
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
## Registered S3 method overwritten by 'geojsonlint':
## method from
## print.location dplyr
Pregunta 1 Cargue la base “mapa_zonas”, la cual proviene del paquete chilemapas. Transformelo a data.table y guarde este objeto como “mapa” y filtre sus filas para quedarse con la region “13” (character) y provincia “131” (character). Cargue también la base “censo_2017_zonas” (también de chilemapas), transformela a data.table y guardela como censo.
mapas <- data.table(mapa_zonas)
mapa1 <- mapas[codigo_region=="13"& codigo_provincia=="131"]
censo <- data.table(censo_2017_zonas)
mapa1<- merge(mapas,censo,by="geocodigo")
Pregunta 2 Realice un merge entre mapa y codigos_territoriales (la cual proviene del paquete chilemapas). ¿Qué comuna tiene una mayor población de edad == ‘0 a 5’?. Responda fuera del chunk.
#mapa2 <- merge((mapa & codigos_territoriales) by.=(codigo_comuna, edad))
#mapa2[Edad>5]
#La comuna con mayor población entre 0 y 5 años es…No sé, no lo pude sacar
Pregunta 3 Realice un gráfico de barras que muestre el número de personas por tramo de edad (“edad”). Para esto, cree un nuevo objeto llamado mapa2 (ya en el chuck), elimine la columna “geometry” y después proceda a sumar personas por tramo de edad.
mapa3 <- mapa1 [!is.na(geometry)]
#ggplot(mapa3, mapping = aes(x=mapas$`edad`)) + geom_bar()
Pregunta 4 Volviendo al objeto “mapa”, haga un gráfico de densidad del número de personas. Haga un color y facet_wrap por tramo de edad. Libere el eje X e Y en el facet.
#ggplot(mapa1, mapping = aes(x=mapas$`poblacion`)) + geom_bar()
#geom_density(color = 4, bins=100) +
#facet_wrap(~location,scale="free_y", scale="free_x",ncol=3) +
# theme(legend.position = "none")
Pregunta 5 Filtre la base “mapa” para quedarse con aquellas filas que cumplen que edad == ‘0 a 5’. Luego utilice la función de classIntervals para crear 6 intervalos de la población. Utilice el style visto en clases . Por último, haga un mapa de ggplot2 con el fill de la variable categórica creada (la de los intervalos).
#ggplot(mapa1[edad >= as.edad("5")],
#aes(x=edad,y=poblacion,group=location,color=location)) +
# geom_line() +
#facet_wrap(~location,scale="free_y", scale="free_x",ncol=3) +
#theme(legend.position = "none")
Pregunta 6 Repita el gráfico anterior pero con el paquete de leaflet. Se incluyen los bins y paleta, pero usted debe crear los labels y el mapa. No se olvide de transformar la base a spatial.frame e incluir el zoom a Santiago.
ggplot() +
geom_sf(mapa1,mapping=aes(geometry=geometry,fill=poblacion))+
coord_sf(xlim = c(-71.8,-70), ylim = c(-34,-32))
## old-style crs object detected; please recreate object with a recent sf::st_crs()
#bins <- seq(min(mapa$poblacion),max(mapa$poblacion),200) +
# paleta <- colorBin(palette = ‘Reds’,domain = mapa$poblacion,bins = bins)