R Markdown

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
delitos <- read.csv("https://cdaj.netlify.app/data/delitos_barrios_comunas.csv")

#Ejercitando con la guia. ##Homcidios

homicidios_por_comuna <- delitos %>% 
    filter(tipo == "Homicidio") %>% 
    group_by(comuna) %>% 
    summarise(homicidios = sum(total))

###Practico con otro delito

hurto_sin_violencia <- delitos %>% 
  filter(tipo == "Hurto (sin violencia)") %>% 
  group_by(comuna) %>% 
  summarise(hurto_sin_violencia = sum (total))

#Integro cantidad de población.

poblacion <- read.csv("https://cdaj.netlify.app/data/comunas_poblacion_2020.csv")
homicidios_por_comuna <- homicidios_por_comuna %>% left_join(poblacion)
## Joining with `by = join_by(comuna)`
hurto_sin_violencia <- hurto_sin_violencia %>% left_join(poblacion)
## Joining with `by = join_by(comuna)`

#Scatter Plot

ggplot(homicidios_por_comuna) + geom_point(aes(x = poblacion, y = homicidios))

ggplot(hurto_sin_violencia) + geom_point(aes(x = poblacion, y = hurto_sin_violencia))
## Warning: Removed 1 rows containing missing values (`geom_point()`).

Agrego color con aes.

ggplot(homicidios_por_comuna) + 
    geom_point(aes(x = poblacion, y = homicidios, color = factor(comuna)))

ggplot (hurto_sin_violencia) + 
  geom_point(aes(x = poblacion, y = hurto_sin_violencia, color = factor(comuna)))
## Warning: Removed 1 rows containing missing values (`geom_point()`).

#Le ponemos etiquetas con el nombre de cada comuna.

ggplot(homicidios_por_comuna) + 
    geom_label(aes(x = poblacion, y = homicidios, label = factor(comuna)))

ggplot(hurto_sin_violencia) + 
    geom_label(aes(x = poblacion, y = hurto_sin_violencia, label = factor(comuna)))
## Warning: Removed 1 rows containing missing values (`geom_label()`).

ggplot(homicidios_por_comuna) + 
    geom_point(aes(x = poblacion, y = homicidios, size = homicidios))

ggplot(homicidios_por_comuna) + 
    geom_point(aes(x = poblacion, y = homicidios, shape = factor(comuna)))
## Warning: The shape palette can deal with a maximum of 6 discrete values because
## more than 6 becomes difficult to discriminate; you have 15. Consider
## specifying shapes manually if you must have them.
## Warning: Removed 9 rows containing missing values (`geom_point()`).

## Ajustado color y tamaño.

ggplot(homicidios_por_comuna) + 
    geom_point(aes(x = poblacion, y = homicidios), color = "darkolivegreen4")

ggplot(hurto_sin_violencia) + 
    geom_point(aes(x = poblacion, y = hurto_sin_violencia), color = "magenta", size = 6, shape = 0)
## Warning: Removed 1 rows containing missing values (`geom_point()`).

#Facetado

homicidios_por_comuna_y_subtipo <- delitos %>% 
    filter(tipo == "Homicidio") %>% 
    group_by(comuna, subtipo) %>% 
    summarise(homicidios = sum(total)) %>% 
    left_join(poblacion)
## `summarise()` has grouped output by 'comuna'. You can override using the
## `.groups` argument.
## Joining with `by = join_by(comuna)`
head(homicidios_por_comuna_y_subtipo)
## # A tibble: 6 × 4
## # Groups:   comuna [3]
##   comuna subtipo        homicidios poblacion
##    <int> <chr>               <int>     <int>
## 1      1 Doloso                 32    256405
## 2      1 Femicidio               1    256405
## 3      1 Siniestro Vial         11    256405
## 4      2 Doloso                  2    149430
## 5      2 Siniestro Vial          4    149430
## 6      3 Doloso                  6    193276

##Practico con mi variable, “hurto sin violencia”.

hurto_sin_violencia_y_subtipo <- delitos %>% 
    filter(tipo == "Hurto (sin violencia)") %>% 
    group_by(comuna, subtipo) %>% 
    summarise(hurto_sin_violencia = sum(total)) %>% 
    left_join(poblacion)
## `summarise()` has grouped output by 'comuna'. You can override using the
## `.groups` argument.
## Joining with `by = join_by(comuna)`
head(hurto_sin_violencia_y_subtipo)
## # A tibble: 6 × 4
## # Groups:   comuna [3]
##   comuna subtipo           hurto_sin_violencia poblacion
##    <int> <chr>                           <int>     <int>
## 1      1 ""                               2966    256405
## 2      1 "Hurto Automotor"                 112    256405
## 3      2 ""                               1124    149430
## 4      2 "Hurto Automotor"                  54    149430
## 5      3 ""                               2258    193276
## 6      3 "Hurto Automotor"                 163    193276

##Hago el facetado.

ggplot(homicidios_por_comuna_y_subtipo) + 
    geom_point(aes(x = poblacion, y = homicidios)) +
    facet_wrap(~subtipo)

##Con mi variable.

ggplot(hurto_sin_violencia_y_subtipo) + 
    geom_point(aes(x = poblacion, y = hurto_sin_violencia)) +
    facet_wrap(~subtipo)
## Warning: Removed 1 rows containing missing values (`geom_point()`).

#Gráficos de barras, “Geom_bar”.

ggplot(delitos) +
    geom_bar(aes(x = barrio, weight = total))

ggplot(delitos) +
    geom_bar(aes(x = barrio, weight = total)) +
    coord_flip()

ggplot(delitos) +
    geom_bar(aes(x = barrio, weight = total, fill = tipo)) +
    coord_flip()

ggplot(delitos) +
    geom_bar(aes(x = tipo, weight = total)) 

ggplot(delitos) +
    geom_bar(aes(x = tipo, weight = total, fill = barrio)) 

ggplot(delitos) +
    geom_bar(aes(x = tipo, weight = total)) +
    facet_wrap(~barrio)

#Histogramas.

delitos_anuales <- delitos %>% 
    group_by(barrio) %>% 
    summarise(gran_total = sum(total))
head(delitos_anuales)
## # A tibble: 6 × 2
##   barrio      gran_total
##   <chr>            <int>
## 1 ""                 411
## 2 "Agronomía"        208
## 3 "Almagro"         2748
## 4 "Balvanera"       4503
## 5 "Barracas"        2422
## 6 "Belgrano"        2238
ggplot(delitos_anuales) + 
    geom_histogram(aes(x = gran_total))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#{r} ggplot(delitos_anuales_por_tipo) + geom_histogram(aes(x = gran_total)) + facet_wrap(~tipo) ## Poniendo titulos.

ggplot(delitos) +
    geom_bar(aes(x = barrio, weight = total, fill = tipo)) +
    coord_flip() +
    labs(title = "Delitos registrados",
         subtitle = "Ciudad Autónoma de Buenos Aires, 2020",
         caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar",
         x = "barrio",
         y = "cantidad",
         fill = "Tipo")

ggplot(delitos) +
    geom_bar(aes(x = barrio, weight = total, fill = tipo)) +
    coord_flip() +
    labs(title = "Delitos registrados",
         subtitle = "Ciudad Autónoma de Buenos Aires, 2020",
         caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar",
         x = "barrio",
         y = "cantidad",
         fill = "Tipo") +
  theme_minimal()

ggplot(delitos) +
    geom_bar(aes(x = barrio, weight = total, fill = tipo)) +
    coord_flip() +
    labs(title = "Delitos registrados",
         subtitle = "Ciudad Autónoma de Buenos Aires, 2020",
         caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar",
         x = "barrio",
         y = "cantidad",
         fill = "Tipo") +
  theme_dark()

ggplot(delitos) +
    geom_bar(aes(x = barrio, weight = total, fill = tipo)) +
    coord_flip() +
    labs(title = "Delitos registrados",
         subtitle = "Ciudad Autónoma de Buenos Aires, 2020",
         caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar",
         x = "barrio",
         y = "cantidad",
         fill = "Tipo") +
  theme_classic()

#ESTE EL QUE MAS ME GUSTA (CLASSIC)!!