delitos <- read.csv("https://bitsandbricks.github.io/data/crimenydelito.csv")
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.1
## Registered S3 method overwritten by 'rvest':
##   method            from
##   read_xml.response xml2
## -- Attaching packages -------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.0       v purrr   0.3.2  
## v tibble  2.1.1       v dplyr   0.8.0.1
## v tidyr   0.8.3       v stringr 1.4.0  
## v readr   1.3.1       v forcats 0.4.0
## -- Conflicts ----------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lubridate)
## Warning: package 'lubridate' was built under R version 3.6.1
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
delitos <- delitos %>% mutate(fecha = ymd(fecha))
delitos %>% 
    filter(year(fecha) == 2017) %>% 
    ggplot() +
        geom_bar(aes(x = month(fecha, label = TRUE)))

delitos_frecuentes <- delitos %>% 
    count(tipo_delito) %>% 
    top_n(5) %>% 
    pull(tipo_delito)
## Selecting by n
delitos %>% 
    filter(year(fecha) == 2017,
           tipo_delito %in% delitos_frecuentes) %>% 
    ggplot() +
        geom_bar(aes(x = month(fecha, label = TRUE), fill = tipo_delito))

delitos %>% 
    filter(year(fecha) == 2017,
           tipo_delito %in% delitos_frecuentes) %>% 
    ggplot() +
        geom_bar(aes(x = month(fecha, label = TRUE), fill = tipo_delito),
                 position = "dodge")

conteo_Mes <-  delitos %>% 
    filter(year(fecha) == 2017,
           tipo_delito %in% delitos_frecuentes) %>% 
    count(tipo_delito, mes = month(fecha, label = TRUE))
ggplot(conteo_Mes) +
    geom_line(aes(x = mes, y = n, group = tipo_delito, color = tipo_delito))

- Mediante el grƔfico anterior podemos verificar lo comentado de manera previa.

conteo_dia <-  delitos %>% 
    filter(year(fecha) == 2017,
           tipo_delito %in% delitos_frecuentes) %>% 
    count(tipo_delito, diasemana = wday(fecha, label = TRUE))%>% 
    group_by(tipo_delito) %>% 
    mutate(pct = n / sum(n) * 100)
ggplot(conteo_dia) +
    geom_line(aes(x = diasemana, y = pct, group = tipo_delito, color = tipo_delito))

Los sƔbados pero sobre todo los domingos se reducen la cantidad de delitos cometidos Los martes existen mƔs hurtos de rueda

library(ggmap)
## Warning: package 'ggmap' was built under R version 3.6.1
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
delitos <- delitos %>% 
    filter(latitud <0, longitud <0)

bbox <- c(min(delitos$longitud, na.rm = TRUE),
          min(delitos$latitud, na.rm = TRUE),
          max(delitos$longitud, na.rm = TRUE),
          max(delitos$latitud, na.rm = TRUE))

CABA <- get_stamenmap(bbox = bbox, 
                      maptype = "toner-lite")
## Source : http://tile.stamen.com/toner-lite/10/345/616.png
## Source : http://tile.stamen.com/toner-lite/10/346/616.png
## Source : http://tile.stamen.com/toner-lite/10/345/617.png
## Source : http://tile.stamen.com/toner-lite/10/346/617.png
ggmap(CABA) +
    geom_bin2d(data = delitos, aes(x = longitud, y = latitud), bins = 100) +
    scale_fill_viridis_c()
## Warning: Removed 2 rows containing missing values (geom_tile).

- Es visible que la concetración del delito se brinda sobre la linea A/Eje Rivadavia, Linea D/Eje Santa Fe Cabildo y el centro neuralgico de la ciudad.

ggmap(CABA) +
    geom_density2d(data = filter(delitos, tipo_delito %in% delitos_frecuentes), aes(x = longitud, y = latitud, color = stat(level))) +
    scale_color_viridis_c() +
    facet_wrap(~tipo_delito)

delitos <- delitos %>% 
    mutate(hora_base = hour(hms(hora)))

ggmap(CABA) +
    geom_density2d(data = filter(delitos, 
                                 tipo_delito == "Hurto De Rueda",
                                 !(wday(fecha) %in% 2:5) ),
                   aes(x = longitud, 
                       y = latitud, 
                       color = stat(level))) +
    scale_color_viridis_c() +
    facet_wrap(~hora_base, nrow = 4) +
    labs(title = "Concentración espacial de hurtos de rueda",
         subtitle = "SegĆŗn hora del dĆ­a")