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()
