Datos

Homicidios 2017.

#datos <- read.csv("https://www.datos.gov.co/api/views/mkw6-468s/rows.csv?accessType=DOWNLOAD")
#save(datos, file = "Homicidios2017.Rdata")
load("Homicidios2017.Rdata")
# Editando datos
datos2 <- datos %>% 
  select(-c(Código.DANE, Cantidad)) %>% 
  mutate(Fecha2 = dmy(Fecha), Hora2 = hms(Hora))
str(datos2)
## 'data.frame':    11965 obs. of  20 variables:
##  $ Fecha             : Factor w/ 365 levels "01/01/2017","01/02/2017",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Departamento      : Factor w/ 32 levels "AMAZONAS","ANTIOQUIA",..: 1 2 2 2 2 2 2 2 2 2 ...
##  $ Municipio         : Factor w/ 764 levels "ABEJORRAL","ABREGO",..: 348 77 166 194 196 310 382 382 382 382 ...
##  $ Día               : Factor w/ 7 levels "Domingo","Jueves",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Hora              : Factor w/ 1154 levels "0:00:00","0:01:00",..: 328 714 939 949 1012 1021 29 29 760 260 ...
##  $ Barrio            : Factor w/ 5569 levels "      BARRIO LAS AMERICAS",..: 2035 2163 2679 3299 2429 3023 1369 1640 2337 1640 ...
##  $ Zona              : Factor w/ 2 levels "RURAL","URBANA": 1 2 2 1 2 2 2 2 2 2 ...
##  $ Clase.de.sitio    : Factor w/ 141 levels "AEROPUERTO","ALMACENES",..: 22 140 41 55 140 140 62 140 26 140 ...
##  $ Arma.empleada     : Factor w/ 20 levels "ACIDO","ALMOHADA",..: 3 4 3 4 3 3 4 4 10 3 ...
##  $ Móvil.Agresor     : Factor w/ 11 levels "-","A PIE","BICICLETA",..: 9 2 2 2 2 2 2 2 2 2 ...
##  $ Móvil.Victima     : Factor w/ 12 levels "-","A PIE","BICICLETA",..: 10 2 2 2 2 2 2 2 2 2 ...
##  $ Edad              : int  20 21 22 46 37 41 24 22 4 27 ...
##  $ Sexo              : Factor w/ 2 levels "FEMENINO","MASCULINO": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Estado.civil      : Factor w/ 7 levels "-","CASADO","DIVORCIADO",..: 5 5 6 5 6 3 5 5 5 5 ...
##  $ País.de.nacimiento: Factor w/ 17 levels "-","AUSTRALIA",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ Clase.de.empleado : Factor w/ 29 levels "AFRODESCENDIENTE",..: 14 22 22 22 22 2 18 14 7 7 ...
##  $ Profesión         : Factor w/ 34 levels "-","ADMINISTRACION  DE EMPRESAS",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Escolaridad       : Factor w/ 8 levels "-","ANALFABETA",..: 5 4 5 5 4 4 5 5 2 5 ...
##  $ Fecha2            : Date, format: "2017-01-01" "2017-01-01" ...
##  $ Hora2             :Formal class 'Period' [package "lubridate"] with 6 slots
##   .. ..@ .Data : num  0 0 0 0 0 0 0 0 0 0 ...
##   .. ..@ year  : num  0 0 0 0 0 0 0 0 0 0 ...
##   .. ..@ month : num  0 0 0 0 0 0 0 0 0 0 ...
##   .. ..@ day   : num  0 0 0 0 0 0 0 0 0 0 ...
##   .. ..@ hour  : num  15 21 4 5 6 6 0 0 22 13 ...
##   .. ..@ minute: num  5 20 48 0 30 40 30 30 10 40 ...

Series temporales

datos2 %>% 
  group_by(Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  geom_point() +
  geom_line() +
  geom_smooth(se = FALSE, lwd = 1.2, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Departamento

datos2 %>% 
  group_by(Departamento,Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_wrap(~ Departamento, ncol = 5) +
  geom_line(lwd = 0.1) +
  geom_smooth(se = FALSE, lwd = 1, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por zona") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Zona

datos2 %>% 
  group_by(Zona,Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_wrap(~ Zona) +
  geom_line(lwd = 0.2) +
  geom_smooth(se = FALSE, lwd = 1.2, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por zona") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Género

datos2 %>% 
  group_by(Sexo,Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_wrap(~ Sexo) +
  geom_line(lwd = 0.5) +
  geom_smooth(se = FALSE, lwd = 1.2, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por género") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Género - Zona

datos2 %>% 
  group_by(Sexo, Zona,Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_grid(Zona ~ Sexo) +
  geom_line(lwd = 0.3) +
  geom_smooth(se = FALSE, lwd = 0.9, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por zona y género") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Arma empleada

datos2 %>% 
  group_by(Arma.empleada, Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_wrap(~ Arma.empleada) +
  geom_line(lwd = 0.2) +
  geom_smooth(se = FALSE, lwd = 0.9, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por arma empleada") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Móvil del agresor

datos2 %>% 
  group_by(Móvil.Agresor, Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_wrap(~ Móvil.Agresor) +
  geom_line(lwd = 0.2) +
  geom_smooth(se = FALSE, lwd = 0.6, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por móvil del agresor") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Móvil de la victima

datos2 %>% 
  group_by(Móvil.Victima, Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_wrap(~ Móvil.Victima) +
  geom_line(lwd = 0.2) +
  geom_smooth(se = FALSE, lwd = 0.15, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por móvil de la victima") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Estado civil

datos2 %>% 
  group_by(Estado.civil, Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_wrap(~ Estado.civil) +
  geom_line(lwd = 0.2) +
  geom_smooth(se = FALSE, lwd = 0.25, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por estado civil") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Escolaridad

datos2 %>% 
  group_by(Escolaridad, Fecha2) %>% 
  summarise(n = n()) %>% 
  ggplot(data = ., mapping = aes(x = Fecha2, y = n)) +
  facet_wrap(~ Escolaridad) +
  geom_line(lwd = 0.2) +
  geom_smooth(se = FALSE, lwd = 0.35, color = "firebrick2") +
  labs(x = "Fecha", y = "Número de homicidios", title = "Homicidios por día en el año 2017",
       subtitle = "Serie temporal por nivel de escolaridad") +
  theme_linedraw() +
  theme(axis.text.x = element_text(angle = 90))

Distribución de edad

# biblitoeca gridExtra
library(gridExtra)
g1 <- ggplot(data = datos2, mapping = aes(x = Edad)) +
  geom_histogram(color = "black", bins = 30, aes(y = ..density..),
                 fill = "gray60") +
  geom_density(fill = "blue",  alpha = 0.3,
               show.legend = FALSE) +
  geom_vline(xintercept = mean(datos2$Edad), color = "red", linetype = 2) +
  labs(x = "Edad (años)") +
  theme_linedraw() 

g2 <- ggplot(data = datos2, mapping = aes(x = "", y = Edad)) +
  geom_boxplot() +
  geom_hline(yintercept = mean(datos2$Edad), color = "red", linetype = 2) +
  labs(y = "Edad (años)", x = "") +
  theme_linedraw()

grid.arrange(arrangeGrob(g1, g2, ncol = 2, top = "Distribución de edad",
                         bottom = "Líneas rojas punteadas indican la media general de edad"))

Relación edad vs número de asesinatos

# Obteniendo conteos
df_disp <- datos2 %>% 
  group_by(Edad) %>% 
  summarise(Total = n())

# Gráfico
ggplot(data = df_disp, mapping = aes(x = Edad, y = Total)) +
  geom_point(size = 1, stroke = 0.7) +
  geom_smooth(method = "lm", lwd = 0.5, se = FALSE, aes(colour = "black")) +
  geom_smooth(method = "loess", lwd = 0.5, se = FALSE, aes(colour = "blue")) +
  geom_smooth(method = "lm", formula = y ~ poly(x, 2),
              lwd = 0.5, se = FALSE, aes(colour = "magenta4")) +
  geom_smooth(method = "lm", formula = y ~ poly(x, 3),
              lwd = 0.5, se = FALSE, aes(colour = "red")) +
  geom_smooth(method = "lm", formula = y ~ poly(x, 4),
              lwd = 0.5, se = FALSE, aes(colour = "green4")) +
  labs(x = "Edad (años)", y = "Número de muertos",
       title = "Edad vs número de muertos") +
  scale_colour_identity(name = "Modelo ajustado",
                        breaks = c("black", "blue", "magenta4", "red", "green4"),
                        labels = c("Lineal","Loess", "Cuadrático", "Cúbico",
                                   "4to grado"),
                        guide = "legend") +
  theme_linedraw()

ggplot(data = df_disp, mapping = aes(x = Edad, y = Total)) +
  geom_density_2d(size = 0.8) +
  labs(x = "Edad (años)", y = "Número de muertos",
       title = "Edad vs número de muertos", subtitle = "Densidad 2D") +
  theme_linedraw()

Género

# Obteniendo conteos
df_disp2 <- datos2 %>% 
  group_by(Sexo, Edad) %>% 
  summarise(Total = n())

# Gráfico
ggplot(data = df_disp2, mapping = aes(x = Edad, y = Total, color = Sexo)) +
  geom_point(size = 1, stroke = 0.7) +
  labs(x = "Edad (años)", y = "Número de muertos",
       title = "Edad vs número de muertos") +
  scale_color_manual(values = c("blue", "red")) +
  theme_linedraw()

ggplot(data = df_disp2, mapping = aes(x = Edad, y = Total, color = Sexo)) +
  geom_density_2d(size = 0.8) +
  scale_color_manual(values = c("blue", "red")) +
  labs(x = "Edad (años)", y = "Número de muertos",
       title = "Edad vs número de muertos", subtitle = "Densidad 2D") +
  theme_linedraw()

Otros

# Obteniendo conteos
df_disp3 <- datos2 %>% 
  group_by(Zona, Edad) %>% 
  summarise(Total = n())

ggplot(data = df_disp3, mapping = aes(x = Edad, y = Total, color = Zona)) +
  geom_density_2d(size = 0.8) +
  scale_color_manual(values = c("blue", "red")) +
  labs(x = "Edad (años)", y = "Número de muertos",
       title = "Edad vs número de muertos", subtitle = "Densidad 2D") +
  theme_linedraw()

ggplot(data = df_disp3, mapping = aes(x = Edad, y = Total, color = Zona)) +
  geom_point(size = 1, stroke = 0.7) +
  labs(x = "Edad (años)", y = "Número de muertos",
       title = "Edad vs número de muertos") +
  scale_color_manual(values = c("blue", "red")) +
  theme_linedraw()

# Obteniendo conteos
df_disp4 <- datos2 %>% 
  group_by(Escolaridad, Edad) %>% 
  summarise(Total = n())

ggplot(data = df_disp4, mapping = aes(x = Edad, y = Total, color = Escolaridad)) +
  geom_point(size = 1, stroke = 0.7) +
  labs(x = "Edad (años)", y = "Número de muertos",
       title = "Edad vs número de muertos") +
  scale_color_manual(values = RColorBrewer::brewer.pal(n = 8, name = "Set1")) +
  theme_linedraw()

# Obteniendo conteos
df_disp5 <- datos2 %>%
  group_by(Estado.civil, Edad) %>% 
  summarise(Total = n())

ggplot(data = df_disp5, mapping = aes(x = Edad, y = Total, color = Estado.civil)) +
  geom_point(size = 1, stroke = 0.7, stroke = 1.4) +
  labs(x = "Edad (años)", y = "Número de muertos",
       title = "Edad vs número de muertos") +
  scale_color_manual(values = RColorBrewer::brewer.pal(n = 8, name = "Set2")) +
  theme_linedraw()

Tablas resumen

Departamentos

datos2 %>% 
  group_by(Departamento) %>% 
  summarise(Total = n()) %>%
  arrange(desc(Total)) %>% 
  slice(1:10) %>% 
  knitr::kable(caption = "10 departamentos con mayor número de homicidos en 2017.")
10 departamentos con mayor número de homicidos en 2017.
Departamento Total
VALLE 2369
ANTIOQUIA 1915
CUNDINAMARCA 1555
CAUCA 561
ATLÁNTICO 555
NARIÑO 530
NORTE DE SANTANDER 506
BOLÍVAR 402
TOLIMA 315
META 281

Municipios

datos2 %>% 
  group_by(Municipio) %>% 
  summarise(Total = n()) %>%
  arrange(desc(Total)) %>% 
  slice(1:30) %>% 
  knitr::kable(caption = "30 municipios con mayor número de homicidos en 2017.")
30 municipios con mayor número de homicidos en 2017.
Municipio Total
CALI (CT) 1234
BOGOTÁ D.C. (CT) 1130
MEDELLÍN (CT) 573
BARRANQUILLA (CT) 346
CÚCUTA (CT) 243
CARTAGENA (CT) 233
SAN ANDRES DE TUMACO 208
SOACHA 160
PALMIRA 154
PEREIRA (CT) 135
SOLEDAD 130
VILLAVICENCIO (CT) 112
MAICAO 102
SANTA MARTA (CT) 96
TULUÁ 96
QUIBDÓ (CT) 93
BUCARAMANGA (CT) 91
IBAGUÉ (CT) 91
ARMENIA (CT) 86
JAMUNDÍ 85
TURBO 84
NEIVA (CT) 82
BELLO 80
GUADALAJARA DE BUGA 78
YUMBO 74
CARTAGO 72
VALLEDUPAR (CT) 71
TIBÚ 68
BUENAVENTURA 67
MANIZALES (CT) 67

Días de la semana

datos2 %>% 
  group_by(Día) %>% 
  summarise(Total = n()) %>%
  arrange(desc(Total)) %>% 
  slice(1:10) %>% 
  knitr::kable(caption = "Número de homicidios por día de la semana.")
Número de homicidios por día de la semana.
Día Total
Domingo 2736
Sábado 1859
Lunes 1716
Viernes 1515
Jueves 1393
Martes 1377
Miércoles 1369

Sitios

datos2 %>% 
  group_by(Clase.de.sitio) %>% 
  summarise(Total = n()) %>%
  arrange(desc(Total)) %>% 
  slice(1:20) %>% 
  knitr::kable(caption = "20 sitios de mayor homicidio en 2017")
20 sitios de mayor homicidio en 2017
Clase.de.sitio Total
VIAS PUBLICAS 7472
FINCAS Y SIMILARES 1251
CASAS DE HABITACION 639
CARRETERA 361
FRENTE A RESIDENCIAS - VIA PUBLICA 257
BARES, CANTINAS Y SIMILARES 166
DENTRO DE LA VIVIENDA 164
RIOS 138
LOTE BALDIO 135
LOCAL COMERCIAL 77
BILLARES 71
INTERIOR VEHICULO PARTICULAR 65
PARQUES 62
TROCHA 61
TIENDA 52
CANCHA DE FUTBOL 45
DISCOTECAS 45
INTERIOR VEHICULO SERVICIO PUBLICO 43
HOTELES, RESIDENCIAS, Y SIMILARES. 42
TRAMO DE VIA 37

Profesión

datos2 %>% 
  mutate(Profesión = gsub("-", "Sin información", Profesión)) %>% 
  group_by(Profesión) %>% 
  summarise(Total = n()) %>%
  arrange(desc(Total)) %>% 
  slice(1:10) %>% 
  knitr::kable(caption = "10 Profesiones de mayor homicidio en 2017.")
10 Profesiones de mayor homicidio en 2017.
Profesión Total
Sin información 11785
POLICIAS 67
DERECHO 22
LICENCIADO 15
ADMINISTRACION DE EMPRESAS 11
TECNICO EMPRESARIAL 11
CONTADURIA PUBLICA 5
TECNOLOGO ELECTRICA 5
TECNOLOGO EN SISTEMAS 5
INGENIERO 4

Nacionalidad

datos2 %>% 
  mutate(País.de.nacimiento = gsub("-", "Sin información", País.de.nacimiento)) %>% 
  group_by(País.de.nacimiento) %>% 
  summarise(Total = n()) %>%
  arrange(desc(Total)) %>% 
  knitr::kable(caption = "Homicidios por nacionalidad en 2017.")
Homicidios por nacionalidad en 2017.
País.de.nacimiento Total
COLOMBIA 11745
Sin información 121
VENEZUELA 79
ECUADOR 4
ESTADOS UNIDOS 3
FRANCIA 2
AUSTRALIA 1
CHINA 1
CUBA 1
ESPAÑA 1
ITALIA 1
MEXICO 1
PERU 1
REPUBLICA DOMINICANA 1
SALVADOR 1
TANZANIA 1
TURQUIA 1