#En este proyecto analizaremos cuales son las delegaciones , los meses, y dias donde mas incidentes viales se han presentado en el 2020 en la CDMX

#Primero estableceremos las librerias que utilizaremos en este proyecto

#ggplot

#ploty

#ggmap

library(ggplot2) 
library(plotly) 
## 
## 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(ggmap) 
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
## 
##     wind

#Observaremos las principales delegaciones donde se producen los incidentes

Iztapalapa<-c(29.062)
GustavoAMadero<-c(16.588)
Cuauhtemoc<-c(15.971)
Tlalpan<-c(12.562)
Dele<-rbind(Iztapalapa,GustavoAMadero,Cuauhtemoc,Tlalpan)
colnames(Dele)<-c("Incidentes")
Dele
##                Incidentes
## Iztapalapa         29.062
## GustavoAMadero     16.588
## Cuauhtemoc         15.971
## Tlalpan            12.562

#Con ayuda de una grafica de pastel observaremos la diferencia

pie(Dele, main="Dele", col= "purple")

#Ubicaremos las delegaciones en la CDMX para observar la lejania o cercania entre cada una de ellas

Iztapalapa<-c(-99.06224,19.35529)
GustavoAMadero<-c(-99.11075,19.49392 )
Cuauhtemoc<-c(-99.14612,19.44506)
Tlalpan<-c(-99.16206,19.29513)
Deleg<-rbind(Iztapalapa,GustavoAMadero,Cuauhtemoc,Tlalpan)
colnames(Deleg)<-c("Longitud","Latitud")
Deleg
##                 Longitud  Latitud
## Iztapalapa     -99.06224 19.35529
## GustavoAMadero -99.11075 19.49392
## Cuauhtemoc     -99.14612 19.44506
## Tlalpan        -99.16206 19.29513

#Incluiremos un dataframe

Deleg<-data.frame(Deleg)
Deleg
##                 Longitud  Latitud
## Iztapalapa     -99.06224 19.35529
## GustavoAMadero -99.11075 19.49392
## Cuauhtemoc     -99.14612 19.44506
## Tlalpan        -99.16206 19.29513

#Con la funcion qmplot graficaremos los puntos

qmplot(Longitud, Latitud, data=Deleg)
## Using zoom = 13...
## Source : http://tile.stamen.com/terrain/13/1839/3643.png
## Source : http://tile.stamen.com/terrain/13/1840/3643.png
## Source : http://tile.stamen.com/terrain/13/1841/3643.png
## Source : http://tile.stamen.com/terrain/13/1839/3644.png
## Source : http://tile.stamen.com/terrain/13/1840/3644.png
## Source : http://tile.stamen.com/terrain/13/1841/3644.png
## Source : http://tile.stamen.com/terrain/13/1839/3645.png
## Source : http://tile.stamen.com/terrain/13/1840/3645.png
## Source : http://tile.stamen.com/terrain/13/1841/3645.png
## Source : http://tile.stamen.com/terrain/13/1839/3646.png
## Source : http://tile.stamen.com/terrain/13/1840/3646.png
## Source : http://tile.stamen.com/terrain/13/1841/3646.png
## Source : http://tile.stamen.com/terrain/13/1839/3647.png
## Source : http://tile.stamen.com/terrain/13/1840/3647.png
## Source : http://tile.stamen.com/terrain/13/1841/3647.png
## Source : http://tile.stamen.com/terrain/13/1839/3648.png
## Source : http://tile.stamen.com/terrain/13/1840/3648.png
## Source : http://tile.stamen.com/terrain/13/1841/3648.png

#Ahora lo observaremos en un mapa de densidad

qmplot(Longitud, Latitud, data=Deleg, geom=c("point","density2d"))
## Using zoom = 13...

#Ahora ubicaremos las delegaciones donde se dieron menos de 5000 incidentes en 2020

Tlahuac<-c(-99.00507,19.28689)
Cuajimalpa<-c(-99.29388,19.35993 )
MagdalenaContreras<-c(-99.21118,19.33212)
MilpaAlta<-c(-99.02317,19.19251)
Deleg2<-rbind(Tlahuac,Cuajimalpa,MagdalenaContreras,MilpaAlta)
colnames(Deleg2)<-c("Longitud","Latitud")
Deleg2
##                     Longitud  Latitud
## Tlahuac            -99.00507 19.28689
## Cuajimalpa         -99.29388 19.35993
## MagdalenaContreras -99.21118 19.33212
## MilpaAlta          -99.02317 19.19251

#Lo pondremos en un dataframe

Deleg2<-data.frame(Deleg2)
Deleg2
##                     Longitud  Latitud
## Tlahuac            -99.00507 19.28689
## Cuajimalpa         -99.29388 19.35993
## MagdalenaContreras -99.21118 19.33212
## MilpaAlta          -99.02317 19.19251

#Ahora lo ubicaremos en un mapa de densidad

qmplot(Longitud, Latitud, data=Deleg2, geom=c("point","density2d"))
## Using zoom = 12...
## Source : http://tile.stamen.com/terrain/12/918/1823.png
## Source : http://tile.stamen.com/terrain/12/919/1823.png
## Source : http://tile.stamen.com/terrain/12/920/1823.png
## Source : http://tile.stamen.com/terrain/12/921/1823.png
## Source : http://tile.stamen.com/terrain/12/918/1824.png
## Source : http://tile.stamen.com/terrain/12/919/1824.png
## Source : http://tile.stamen.com/terrain/12/920/1824.png
## Source : http://tile.stamen.com/terrain/12/921/1824.png
## Source : http://tile.stamen.com/terrain/12/918/1825.png
## Source : http://tile.stamen.com/terrain/12/919/1825.png
## Source : http://tile.stamen.com/terrain/12/920/1825.png
## Source : http://tile.stamen.com/terrain/12/921/1825.png

#Concluiremos con los meses en donde se dieron mas incidentes

febrero<-(19.313)
enero<-(18.113)
marzo<-(16.705)
noviemb<-(14.854)
mayo<-(7.064)
abril<-(8.001)
junio<-(10.414)
am<-matrix(c(febrero,enero,marzo,noviemb,mayo,abril,junio),nrow=1)
colnames(am)=c("Febrero","Enero","Marzo","Noviemb","Mayo","Abril","Junio")
rownames(am)=c("Meses")
am
##       Febrero  Enero  Marzo Noviemb  Mayo Abril  Junio
## Meses  19.313 18.113 16.705  14.854 7.064 8.001 10.414

#Lo ejemplificaremos en una grafica de barras

barplot(am, main="Meses", col= "pink")

#Para finalizar veremos el dia de la semana en la que mas incidentes hay en la CDMX

sabado<-(26.937)
viernes<-(26.660)
domingo<-(22.965)
jueves<-(22.810)
miercoles<-(21.915)
martes<-(20.882)
lunes<-(20.517)
am<-matrix(c(sabado,viernes,domingo,jueves,miercoles,martes,lunes),nrow=1)
colnames(am)=c("Sabado","Viernes","Domingo","Jueves","Miercoles","Martes","Lunes")
rownames(am)=c("Semana")
am
##        Sabado Viernes Domingo Jueves Miercoles Martes  Lunes
## Semana 26.937   26.66  22.965  22.81    21.915 20.882 20.517

#Con la grafica de pastel veremos la diferencia

pie(am, main="Semana", col= "blue")

#Para finalizar les dejare un video explicando a fondo el proyecto