This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
library (tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library (lubridate)
delitos <- read.csv("https://cdaj.netlify.app/data/delitos_fecha_lugar.csv")
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
## : embedded nul(s) found in input
delitos$fecha <- dmy(delitos$fecha)
muestra_de_fechas <- sample(delitos$fecha, 5)
muestra_de_fechas
## [1] "2020-10-11" "2020-10-13" "2020-02-29" "2020-11-16" "2020-08-27"
wday(muestra_de_fechas)
## [1] 1 3 7 2 5
#El dia
wday(muestra_de_fechas, label = TRUE)
## [1] dom\\. mar\\. sáb\\. lun\\. jue\\.
## Levels: dom\\. < lun\\. < mar\\. < mié\\. < jue\\. < vie\\. < sáb\\.
#El mes
month(muestra_de_fechas)
## [1] 10 10 2 11 8
#El año.
year(muestra_de_fechas)
## [1] 2020 2020 2020 2020 2020
ggplot(delitos) +
geom_bar(aes(x = month(fecha, label = TRUE)))
ggplot(delitos) +
geom_bar(aes(x = month(fecha, label = TRUE), fill = tipo))
#Facetas.
ggplot(delitos) +
geom_bar(aes(x = wday(fecha, label = TRUE), fill = tipo)) +
facet_wrap(vars(tipo))
#Escala libre.
ggplot(delitos) +
geom_bar(aes(x = wday(fecha, label = TRUE), fill = tipo)) +
facet_wrap(vars(tipo), scales = "free")
##Mirando el espacio.
library(ggmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning this package
## will retire shortly. Please refer to R-spatial evolution reports on
## https://r-spatial.org/r/2023/05/15/evolution4.html for details.
## This package is now running under evolution status 0
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
#Obteniendo un mapa base.
bbox <- make_bbox(delitos$longitud, delitos$latitud)
bbox
## left bottom right top
## -58.54046 -34.71247 -58.33403 -34.52172
CABA <- get_stamenmap(bbox = bbox, maptype = "toner", zoom = 12)
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
ggmap(CABA)
CABA <- get_stamenmap(bbox = bbox, maptype = "watercolor", zoom = 12)
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
ggmap(CABA)
#De coordenadas al mapa.
ggmap(CABA) +
geom_point(data = delitos, aes(x = longitud, y = latitud))
## Warning: Removed 411 rows containing missing values (`geom_point()`).
#Ajustando la visualizacion de los puntos.
ggmap(CABA) +
geom_point(data = delitos, aes(x = longitud, y = latitud),
color = "blue", size = 0.1, alpha = 0.1)
## Warning: Removed 411 rows containing missing values (`geom_point()`).
#Mapas de densidad, lo soluciono con la funcion “geom_bind2d”.
ggmap(CABA) +
geom_bin2d(data = delitos, aes(x = longitud, y = latitud))
## Warning: Removed 411 rows containing non-finite values (`stat_bin2d()`).
#Los Viridis.
ggmap(CABA) +
geom_bin2d(data = delitos, aes(x = longitud, y = latitud), bins = 100) +
scale_fill_viridis_c()
## Warning: Removed 411 rows containing non-finite values (`stat_bin2d()`).
#Kernel density estimation con la funcion “geom_density2d_filled”
ggmap(CABA) +
geom_density2d_filled(data = delitos, aes(x = longitud, y = latitud), alpha = 0.5)
## Warning: Removed 411 rows containing non-finite values
## (`stat_density2d_filled()`).
ggmap(CABA) +
geom_density2d_filled(data = delitos, aes(x = longitud, y = latitud))
## Warning: Removed 411 rows containing non-finite values
## (`stat_density2d_filled()`).
#Visualizo multiples categorías.
ggmap(CABA) +
geom_point(data = delitos,
aes(x = longitud, y = latitud, color = tipo),
size = 0.1, alpha = 0.1) +
guides(color = guide_legend(override.aes = list(size = 1, alpha = 1)))
## Warning: Removed 411 rows containing missing values (`geom_point()`).
#Facetado.
ggmap(CABA) +
geom_point(data = delitos,
aes(x = longitud, y = latitud, color = tipo),
size = 0.1, alpha = 0.1) +
guides(color = guide_legend(override.aes = list(size = 1, alpha = 1))) +
facet_wrap(vars(tipo))
## Warning: Removed 411 rows containing missing values (`geom_point()`).
ggmap(CABA) +
geom_density2d_filled(data = delitos, aes(x = longitud, y = latitud), alpha = 0.5) +
facet_wrap(vars(tipo))
## Warning: Removed 411 rows containing non-finite values
## (`stat_density2d_filled()`).
#Combinando espacio y tiempo.
ggmap(CABA) +
geom_density2d_filled(data = filter(delitos, tipo == "Hurto (sin violencia)"),
aes(x = longitud, y = latitud), alpha = .5) +
facet_wrap(vars(franja))
## Warning: Removed 48 rows containing non-finite values
## (`stat_density2d_filled()`).
#Titulos y subtitulos para hurtos sin violencia.
ggmap(CABA) +
geom_density2d_filled(data = filter(delitos, !is.na(franja), tipo == "Hurto (sin violencia)"),
aes(x = longitud, y = latitud), alpha = .5) +
guides(fill = FALSE) +
facet_wrap(vars(franja), nrow = 4) +
labs(title = "Ciudad de Buenos Aires: concentración espacial de hurtos",
subtitle = "según hora del día, durante el año 2020",
caption = "fuente: https://mapa.seguridadciudad.gob.ar") +
theme_void()
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 42 rows containing non-finite values
## (`stat_density2d_filled()`).
#Homicidios.
ggmap(CABA) +
geom_density2d_filled(data = filter(delitos, !is.na(franja), tipo == "Homicidio"),
aes(x = longitud, y = latitud), alpha = .5) +
guides(fill = FALSE) +
facet_wrap(vars(franja), nrow = 4) +
labs(title = "Ciudad de Buenos Aires: concentración espacial de homicidios",
subtitle = "según hora del día, durante el año 2020",
caption = "fuente: https://mapa.seguridadciudad.gob.ar") +
theme_void()
#EJERCICIO I. NO PUEDO RESOLVERLO, SOLO LOGRO UN GRAFICO PERO
LINEAL.
ggplot(delitos, aes(x = franja, y = tipo)) +
geom_line() +
labs(x = "Hora", y = "Delitos") +
ggtitle("Delitos por hora")
## Warning: Removed 496 rows containing missing values (`geom_line()`).
#Aca hago otros intentos.
ggplot(delitos, aes(x = franja, y = tipo)) +
geom_line() +
labs(x = "Hora", y = "Delitos") +
ggtitle("Delitos por hora") +
facet_wrap(vars(franja), nrow = 4)
## Warning: Removed 496 rows containing missing values (`geom_line()`).
#EJERCICIO II
ggmap(CABA) +
geom_count(data = delitos,
aes(x = longitud, y = latitud, color = tipo),
size = 0.1, alpha = 0.1) +
guides(color = guide_legend(override.aes = list(size = 1, alpha = 1)))
## Warning: Removed 411 rows containing non-finite values (`stat_sum()`).
#facetado
ggmap(CABA) +
geom_count(data = delitos,
aes(x = longitud, y = latitud, color = tipo),
size = 0.1, alpha = 0.1) +
guides(color = guide_legend(override.aes = list(size = 1, alpha = 1))) +
facet_wrap(vars(tipo))
## Warning: Removed 411 rows containing non-finite values (`stat_sum()`).