##CLASE 4: DATOS ESPACIALES - SPPLOT

require(raster)
## Loading required package: raster
## Loading required package: sp
require(rgdal)
## Loading required package: rgdal
## rgdal: version: 1.5-16, (SVN revision 1050)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.0.4, released 2020/01/28
## Path to GDAL shared files: D:/USUARIO/Documents/R/win-library/3.6/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
## Path to PROJ shared files: D:/USUARIO/Documents/R/win-library/3.6/rgdal/proj
## Linking to sp version:1.4-2
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
require(sp)
require(readxl)
## Loading required package: readxl
## Importar el archivo shp

comunas=shapefile("D:/ESPECIALIZACION/SEMESTRE 1/1. Tratamiento de datos/Clase 4 datos espaciales/Comuna.shp")

comuna_pop =read_excel("D:/ESPECIALIZACION/SEMESTRE 1/1. Tratamiento de datos/Clase 4 datos espaciales/comuna_pop.xlsx", 
                         col_types = c("text", "numeric", "numeric", "numeric", "numeric", "numeric"))
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E2 / R2C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E3 / R3C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E4 / R4C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E5 / R5C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E6 / R6C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E7 / R7C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E8 / R8C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E9 / R9C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E10 / R10C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E11 / R11C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E12 / R12C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E13 / R13C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E14 / R14C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E15 / R15C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E16 / R16C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E17 / R17C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E18 / R18C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E19 / R19C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E20 / R20C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E21 / R21C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E22 / R22C5: got a date
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in E23 / R23C5: got a date
## ver la tabla de atributos del shape comunas
comunas@data
##    comuna    nombre zona_recol     area perimetro
## 0       6  Comuna 6       <NA>  5385422 12496.285
## 1       4  Comuna 4       <NA>  4524983 11430.279
## 2       5  Comuna 5       <NA>  4197624  8441.174
## 3       7  Comuna 7       <NA>  5107267 12547.823
## 4       8  Comuna 8       <NA>  5266743 12178.376
## 5       9  Comuna 9       <NA>  2899409  7983.949
## 6      21 Comuna 21       <NA>  4828927 16149.224
## 7      13 Comuna 13       <NA>  4737262 10030.224
## 8       1  Comuna 1       <NA>  3842243 15518.149
## 9       3  Comuna 3       <NA>  3704463 11003.318
## 10     19 Comuna 19       <NA> 11318091 26480.361
## 11     12 Comuna 12       <NA>  2329398  8254.095
## 12     10 Comuna 10       <NA>  4297730  9404.276
## 13     20 Comuna 20       <NA>  2439498  8448.577
## 14     16 Comuna 16       <NA>  4275834 11700.410
## 15     15 Comuna 15       <NA>  4060433 10425.092
## 16     17 Comuna 17       <NA> 12555929 16841.538
## 17     18 Comuna 18       <NA>  5428611 11681.301
## 18     14 Comuna 14       <NA>  4543322  8937.073
## 19     11 Comuna 11       <NA>  3699601  9315.019
## 20      2  Comuna 2       <NA> 11401109 26477.419
## 21     22 Comuna 22       <NA> 10589125 15552.137
## ordenar, encontrar o emparejar (match) entre el orden de nueros del 1 al 22 segun estan en la tabla "comuna_pop$"2005", 
# con el orden de comuna que tiene el shape 
# -------- resultado: en la tabla comuna_pop$"2005" aparecen  63148  99808  45129 etc
# --------           en el shape corresponden a las comunas  9       21    10    etc

match(1:22,comunas@data$comuna)
##  [1]  9 21 10  2  3  1  4  5  6 13 20 12  8 19 16 15 17 18 11 14  7 22
orden = match(1:22,comunas@data$comuna)
comuna_pop$"2005"[orden]
##  [1]  49415  88329 105884  99808  45129  63148  57160 101976 171258 173129
## [11]  65868  68099 100059 102199  96502 127748 106735  97811 100595 156695
## [21]  75752   8276
## otra forma de ordenar: creando un nuevo shape "comunas_ordenadas"
## aqui ordeno LOS POLIGONOS de shape entonces queda por defecto organizada la tabla del shape pq los poligonos estan asociado a esa tabla 

comunas_ordenadas=comunas[orden,]
comunas_ordenadas@data
##    comuna    nombre zona_recol     area perimetro
## 8       1  Comuna 1       <NA>  3842243 15518.149
## 20      2  Comuna 2       <NA> 11401109 26477.419
## 9       3  Comuna 3       <NA>  3704463 11003.318
## 1       4  Comuna 4       <NA>  4524983 11430.279
## 2       5  Comuna 5       <NA>  4197624  8441.174
## 0       6  Comuna 6       <NA>  5385422 12496.285
## 3       7  Comuna 7       <NA>  5107267 12547.823
## 4       8  Comuna 8       <NA>  5266743 12178.376
## 5       9  Comuna 9       <NA>  2899409  7983.949
## 12     10 Comuna 10       <NA>  4297730  9404.276
## 19     11 Comuna 11       <NA>  3699601  9315.019
## 11     12 Comuna 12       <NA>  2329398  8254.095
## 7      13 Comuna 13       <NA>  4737262 10030.224
## 18     14 Comuna 14       <NA>  4543322  8937.073
## 15     15 Comuna 15       <NA>  4060433 10425.092
## 14     16 Comuna 16       <NA>  4275834 11700.410
## 16     17 Comuna 17       <NA> 12555929 16841.538
## 17     18 Comuna 18       <NA>  5428611 11681.301
## 10     19 Comuna 19       <NA> 11318091 26480.361
## 13     20 Comuna 20       <NA>  2439498  8448.577
## 6      21 Comuna 21       <NA>  4828927 16149.224
## 21     22 Comuna 22       <NA> 10589125 15552.137
## join: adicionar una nueva colunma "pop_2005" a "comunas_ordenadas"


comuna_pop$est_mod2019
##  [1] 1 5 3 2 3 2 3 3 3 3 3 3 2 1 1 2 5 3 4 1 1 6
comunas_ordenadas@data$estrato=comuna_pop$est_mod2019
comunas_ordenadas@data
##    comuna    nombre zona_recol     area perimetro estrato
## 8       1  Comuna 1       <NA>  3842243 15518.149       1
## 20      2  Comuna 2       <NA> 11401109 26477.419       5
## 9       3  Comuna 3       <NA>  3704463 11003.318       3
## 1       4  Comuna 4       <NA>  4524983 11430.279       2
## 2       5  Comuna 5       <NA>  4197624  8441.174       3
## 0       6  Comuna 6       <NA>  5385422 12496.285       2
## 3       7  Comuna 7       <NA>  5107267 12547.823       3
## 4       8  Comuna 8       <NA>  5266743 12178.376       3
## 5       9  Comuna 9       <NA>  2899409  7983.949       3
## 12     10 Comuna 10       <NA>  4297730  9404.276       3
## 19     11 Comuna 11       <NA>  3699601  9315.019       3
## 11     12 Comuna 12       <NA>  2329398  8254.095       3
## 7      13 Comuna 13       <NA>  4737262 10030.224       2
## 18     14 Comuna 14       <NA>  4543322  8937.073       1
## 15     15 Comuna 15       <NA>  4060433 10425.092       1
## 14     16 Comuna 16       <NA>  4275834 11700.410       2
## 16     17 Comuna 17       <NA> 12555929 16841.538       5
## 17     18 Comuna 18       <NA>  5428611 11681.301       3
## 10     19 Comuna 19       <NA> 11318091 26480.361       4
## 13     20 Comuna 20       <NA>  2439498  8448.577       1
## 6      21 Comuna 21       <NA>  4828927 16149.224       1
## 21     22 Comuna 22       <NA> 10589125 15552.137       6
comuna_pop$vio_fam_2018
##  [1] 247 138  99 136 102 278 200 216  98 152 157 123 296 262 332 199 122 273  83
## [20] 243 322   9
comunas_ordenadas@data$violencia_fam=comuna_pop$vio_fam_2018
comunas_ordenadas@data
##    comuna    nombre zona_recol     area perimetro estrato violencia_fam
## 8       1  Comuna 1       <NA>  3842243 15518.149       1           247
## 20      2  Comuna 2       <NA> 11401109 26477.419       5           138
## 9       3  Comuna 3       <NA>  3704463 11003.318       3            99
## 1       4  Comuna 4       <NA>  4524983 11430.279       2           136
## 2       5  Comuna 5       <NA>  4197624  8441.174       3           102
## 0       6  Comuna 6       <NA>  5385422 12496.285       2           278
## 3       7  Comuna 7       <NA>  5107267 12547.823       3           200
## 4       8  Comuna 8       <NA>  5266743 12178.376       3           216
## 5       9  Comuna 9       <NA>  2899409  7983.949       3            98
## 12     10 Comuna 10       <NA>  4297730  9404.276       3           152
## 19     11 Comuna 11       <NA>  3699601  9315.019       3           157
## 11     12 Comuna 12       <NA>  2329398  8254.095       3           123
## 7      13 Comuna 13       <NA>  4737262 10030.224       2           296
## 18     14 Comuna 14       <NA>  4543322  8937.073       1           262
## 15     15 Comuna 15       <NA>  4060433 10425.092       1           332
## 14     16 Comuna 16       <NA>  4275834 11700.410       2           199
## 16     17 Comuna 17       <NA> 12555929 16841.538       5           122
## 17     18 Comuna 18       <NA>  5428611 11681.301       3           273
## 10     19 Comuna 19       <NA> 11318091 26480.361       4            83
## 13     20 Comuna 20       <NA>  2439498  8448.577       1           243
## 6      21 Comuna 21       <NA>  4828927 16149.224       1           322
## 21     22 Comuna 22       <NA> 10589125 15552.137       6             9
## grafico de estrato moda por comuna año 2019 

library(RColorBrewer)

spplot(comunas_ordenadas[,6],main = "ESTRATO MODA POR COMUNA", cuts = 5,col.regions= brewer.pal(6, "Greens"))

comunas_ordenadas@data
##    comuna    nombre zona_recol     area perimetro estrato violencia_fam
## 8       1  Comuna 1       <NA>  3842243 15518.149       1           247
## 20      2  Comuna 2       <NA> 11401109 26477.419       5           138
## 9       3  Comuna 3       <NA>  3704463 11003.318       3            99
## 1       4  Comuna 4       <NA>  4524983 11430.279       2           136
## 2       5  Comuna 5       <NA>  4197624  8441.174       3           102
## 0       6  Comuna 6       <NA>  5385422 12496.285       2           278
## 3       7  Comuna 7       <NA>  5107267 12547.823       3           200
## 4       8  Comuna 8       <NA>  5266743 12178.376       3           216
## 5       9  Comuna 9       <NA>  2899409  7983.949       3            98
## 12     10 Comuna 10       <NA>  4297730  9404.276       3           152
## 19     11 Comuna 11       <NA>  3699601  9315.019       3           157
## 11     12 Comuna 12       <NA>  2329398  8254.095       3           123
## 7      13 Comuna 13       <NA>  4737262 10030.224       2           296
## 18     14 Comuna 14       <NA>  4543322  8937.073       1           262
## 15     15 Comuna 15       <NA>  4060433 10425.092       1           332
## 14     16 Comuna 16       <NA>  4275834 11700.410       2           199
## 16     17 Comuna 17       <NA> 12555929 16841.538       5           122
## 17     18 Comuna 18       <NA>  5428611 11681.301       3           273
## 10     19 Comuna 19       <NA> 11318091 26480.361       4            83
## 13     20 Comuna 20       <NA>  2439498  8448.577       1           243
## 6      21 Comuna 21       <NA>  4828927 16149.224       1           322
## 21     22 Comuna 22       <NA> 10589125 15552.137       6             9
## grafico de violencia familiar año 2018 
#.... my palette n está el número de cortes que desea usar y name el nombre de la rampa de color.

my.palette =brewer.pal(n = 8, name = "OrRd")

spplot(comunas_ordenadas[,7],main = " VIOLENCIA FAMILAR POR COMUNA DE HECHO", 
       col.regions= my.palette, cuts = 7, col = "transparent")

comunas_ordenadas@data
##    comuna    nombre zona_recol     area perimetro estrato violencia_fam
## 8       1  Comuna 1       <NA>  3842243 15518.149       1           247
## 20      2  Comuna 2       <NA> 11401109 26477.419       5           138
## 9       3  Comuna 3       <NA>  3704463 11003.318       3            99
## 1       4  Comuna 4       <NA>  4524983 11430.279       2           136
## 2       5  Comuna 5       <NA>  4197624  8441.174       3           102
## 0       6  Comuna 6       <NA>  5385422 12496.285       2           278
## 3       7  Comuna 7       <NA>  5107267 12547.823       3           200
## 4       8  Comuna 8       <NA>  5266743 12178.376       3           216
## 5       9  Comuna 9       <NA>  2899409  7983.949       3            98
## 12     10 Comuna 10       <NA>  4297730  9404.276       3           152
## 19     11 Comuna 11       <NA>  3699601  9315.019       3           157
## 11     12 Comuna 12       <NA>  2329398  8254.095       3           123
## 7      13 Comuna 13       <NA>  4737262 10030.224       2           296
## 18     14 Comuna 14       <NA>  4543322  8937.073       1           262
## 15     15 Comuna 15       <NA>  4060433 10425.092       1           332
## 14     16 Comuna 16       <NA>  4275834 11700.410       2           199
## 16     17 Comuna 17       <NA> 12555929 16841.538       5           122
## 17     18 Comuna 18       <NA>  5428611 11681.301       3           273
## 10     19 Comuna 19       <NA> 11318091 26480.361       4            83
## 13     20 Comuna 20       <NA>  2439498  8448.577       1           243
## 6      21 Comuna 21       <NA>  4828927 16149.224       1           322
## 21     22 Comuna 22       <NA> 10589125 15552.137       6             9