install.packages("data.table")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
install.packages("rgdal")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
install.packages("ggplot2")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
install.packages("treemap")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)

Actividad 5

P1

Instale y cargue los siguientes paquetes: data.table,rgdal,ggplot2 y treemap.

library("data.table")
library("rgdal")
## Loading required package: sp
## rgdal: version: 1.4-8, (SVN revision 845)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.2, released 2017/09/15
##  Path to GDAL shared files: /usr/share/gdal/2.2
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: (autodetected)
##  Linking to sp version: 1.4-1
library("ggplot2")
library("treemap")

P2

bicidatos<- readOGR("Bici_personaGS2016_2/Bici_personaGS2016_2.shp")
## OGR data source with driver: ESRI Shapefile 
## Source: "/cloud/project/Bici_personaGS2016_2/Bici_personaGS2016_2.shp", layer: "Bici_personaGS2016_2"
## with 1135 features
## It has 33 fields
bicidatos<- bicidatos@data

bicidatos<- as.data.table(bicidatos)

P3

La variable ESTADO_CAL describe el estado de la calzada por la cual circulaba el/la ciclista. De esta forma, las categorías que debe considerar esta variable son 3: BUENO, REGULAR y MALO. Por lo tanto, lo que debe hacer, es considerar todas aquellas observaciones que tengan alguna de esas 3 categorías en la variable ESTADO_CAL.

bicidatos<- bicidatos[ESTADO_CAL=="BUENO" | ESTADO_CAL=="REGULAR" | ESTADO_CAL=="MALO",]

P4

¿Cuántos accidentes hay por comuna?

bicidatos[,.N,by=COMUNA1]
##              COMUNA1   N
##  1:      PROVIDENCIA 135
##  2:      CERRO NAVIA  14
##  3:            NUNOA  79
##  4:            RENCA  34
##  5:         SANTIAGO 150
##  6:            MAIPU  62
##  7:       LA FLORIDA  31
##  8: P. AGUIRRE CERDA  11
##  9:         PUDAHUEL  35
## 10:        PENALOLEN  41
## 11:            LAMPA  11
## 12:           COLINA   6
## 13:       LAS CONDES  52
## 14:        QUILICURA  48
## 15:      SAN JOAQUIN  18
## 16:           PIRQUE   1
## 17:      PUENTE ALTO  72
## 18:            MACUL  28
## 19:      LA CISTERNA  16
## 20:     LO BARNECHEA   9
## 21:        EL BOSQUE  34
## 22:        CERRILLOS  14
## 23:        SAN RAMON  14
## 24:         VITACURA   6
## 25:         RECOLETA  24
## 26:         LA REINA  29
## 27:         LO PRADO  18
## 28: ESTACION CENTRAL  32
## 29:       SAN MIGUEL  19
## 30:    QUINTA NORMAL  27
## 31:        LA GRANJA  15
## 32:       HUECHURABA   6
## 33:    INDEPENDENCIA  16
## 34:       LA PINTANA  12
## 35:         CONCHALI  11
##              COMUNA1   N

P5

¿Cuántos accidentes con personas fallecidas hay por comuna?

bicidatos[Fallecidos>0, .N, by = COMUNA1]
##         COMUNA1 N
##  1:   QUILICURA 2
##  2:   LA GRANJA 1
##  3: CERRO NAVIA 1
##  4:       MAIPU 1
##  5:  LA FLORIDA 2
##  6:       MACUL 1
##  7:    SANTIAGO 1
##  8:  LA PINTANA 1
##  9:    LA REINA 1
## 10: LA CISTERNA 1
## 11:       NUNOA 1
## 12:    RECOLETA 1

P6

¿Cuántos accidentes con personas graves hay por comuna?

bicidatos[Graves>0, .N, by = COMUNA1]
##              COMUNA1  N
##  1:      CERRO NAVIA  2
##  2:            NUNOA 16
##  3:            RENCA  8
##  4:           COLINA  2
##  5:      PROVIDENCIA 26
##  6:            LAMPA  2
##  7:      LA CISTERNA  5
##  8: P. AGUIRRE CERDA  2
##  9:         VITACURA  2
## 10:     LO BARNECHEA  2
## 11:            MAIPU 11
## 12:         LA REINA  7
## 13:      SAN JOAQUIN  6
## 14:        EL BOSQUE  9
## 15:        CERRILLOS  1
## 16: ESTACION CENTRAL  5
## 17:         SANTIAGO 19
## 18:         RECOLETA  5
## 19:        LA GRANJA  3
## 20:    QUINTA NORMAL 10
## 21:         LO PRADO  4
## 22:            MACUL  5
## 23:         PUDAHUEL 13
## 24:      PUENTE ALTO  7
## 25:       LA PINTANA  3
## 26:       HUECHURABA  1
## 27:       LA FLORIDA  3
## 28:    INDEPENDENCIA  4
## 29:        PENALOLEN  6
## 30:         CONCHALI  2
## 31:       SAN MIGUEL  3
## 32:       LAS CONDES  9
## 33:        QUILICURA  4
## 34:        SAN RAMON  1
##              COMUNA1  N

P7

¿Cómo podría mostrar el resultado de la pregunta 6 en un gráfico?. Muestre un gráfico legible.

ggplot(data = bicidatos[Graves>0], aes(x= COMUNA1)) + geom_bar() + theme(axis.text.x = element_text(angle = 90, vjust = 0.6), axis.title.x.top = element_text(size = 0.05), plot.title = element_text(size = 8.5) )

P8

¿Cuál es la principal causa (CAUSA__CON) de accidentes?

bicidatos[,.N, by= CAUSA__CON]
##                         CAUSA__CON   N
##  1:       PERDIDA CONTROL VEHICULO  19
##  2:         CAUSAS NO DETERMINADAS 246
##  3:      IMPRUDENCIA DEL CONDUCTOR 507
##  4:                   OTRAS CAUSAS 195
##  5:   DESOBEDIENCIA A SENALIZACION 121
##  6:           ALCOHOL EN CONDUCTOR  21
##  7: DROGAS Y/O FATIGA EN CONDUCTOR   3
##  8:         IMPRUDENCIA DEL PEATON  13
##  9:               FALLAS MECANICAS   2
## 10:           VELOCIDAD IMPRUDENTE   2
## 11:            DEFICIENCIAS VIALES   1

Principal causa de Accidentes es la imprudencia del conductor

P9

¿Cómo podríamos mostrar,gráficamente, las diferentes causas para la comuna de Providencia y Santiago? Muestre un gráfico legible.

ggplot(data = bicidatos[COMUNA1 == "PROVIDENCIA" | COMUNA1 == "SANTIAGO"], aes(x= CAUSA__CON)) + geom_bar() + facet_wrap("COMUNA1")+ theme(axis.text.x = element_text(angle = 90) )

P10

¿Podría establecer que existe alguna relación entre la cantidad de accidentes leves por comuna y la proporción de accidentes que ocurren en un buen estado de calzada (porc_bueno)? Argumente gráficamente, de la mejor manera posible.

ggplot(data = bicidatos, aes(x= Leves, y = porc_bueno)) + geom_point() 

P11

¿Podría establecer que existe alguna relación entre la cantidad de accidentes graves por comuna y la proporción de accidentes que ocurren en un buen estado de calzada (porc_bueno)? Argumente gráficamente, de la mejor manera posible.

ggplot(data = bicidatos, aes(x= Graves, y = porc_bueno)) + geom_point()

P12

Muestre gráficamente la cantidad de accidentes que hay para las distintas causas de accidentes.

ggplot(data = bicidatos, aes(x= CAUSA__CON)) + geom_bar()+ theme(axis.text.x = element_text(angle = 90) ) + labs(x="Causa", y = "Accidentes")