library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── 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(ggplot2)
library(dplyr)
library(readxl)

library(dplyr)
 
 
BD1 <- read_excel("C:/Users/daiby/Desktop/R/bases_diccionario/BD1.xlsx")

BD2 <- read_excel("C:/Users/daiby/Desktop/R/bases_diccionario/BD2.xlsx")
 
 
head(BD1)
## # A tibble: 6 × 14
##   REGISTRO COMUNA    PI    PJ    PA    PB    PC PC_EDAD EDAD_RANGO GENERO
##      <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>      <dbl>  <dbl>
## 1       25      3     1     1     1     1     1      69         10      1
## 2       26      3     1     1     1     1     1      24          2      2
## 3       27      1     1     1     3     1     1      48          7      2
## 4       30      1     1     1     3     1     1      19          2      2
## 5       36      7     1     1     2     1     1      18          2      1
## 6       38      7     1     1     1     1     1      23          2      2
## # ℹ 4 more variables: ESTRATO_DEC <dbl>, ESTRATO <lgl>, PD <dbl>, D1 <dbl>
names(BD1)
##  [1] "REGISTRO"    "COMUNA"      "PI"          "PJ"          "PA"         
##  [6] "PB"          "PC"          "PC_EDAD"     "EDAD_RANGO"  "GENERO"     
## [11] "ESTRATO_DEC" "ESTRATO"     "PD"          "D1"
head(BD2)
## # A tibble: 6 × 409
##   REGISTRO  D4_2    D5  P106  P107    P1    P2    P3    P4    P5    P6 P7A_1
##      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1       25    NA     2     4     2     3     3     2     2     2     3     8
## 2       26    NA     1     2     2     2     3     2     3     2     3    88
## 3       27    NA     2     4     4     2     3     2     3     3     3     1
## 4       30    NA     2     2     1     2    NA     2     3     2     1    88
## 5       36    NA     2     4     4     2     3     2     3     2     3    88
## 6       38    NA     1     4     4     2     3     2     3     2     3    88
## # ℹ 397 more variables: P7A_2 <dbl>, P7A_3 <dbl>, P7_OTRO <chr>, P7B <dbl>,
## #   T9_1 <dbl>, T9_2 <dbl>, T9_3 <dbl>, T9_4 <dbl>, T9_5 <dbl>, T9_6 <dbl>,
## #   T9_7 <dbl>, P118 <dbl>, CP10_1 <dbl>, CP10_2 <dbl>, CP10_3 <dbl>,
## #   P10_1 <chr>, P10_2 <chr>, P10_3 <chr>, T11_12 <dbl>, T11_13 <dbl>,
## #   P12_1 <dbl>, P12_2 <dbl>, P12_3 <dbl>, P12_4 <dbl>, P12_5 <lgl>,
## #   P12_6 <lgl>, P12_7 <lgl>, P12_OTRO <chr>, P13 <dbl>, HORA_CAP_3 <chr>,
## #   TIEMPO_CAP_2_SEG <dbl>, TIEMPO_CAP_2_MIN <dbl>, P14 <dbl>, P15_1 <dbl>, …
names(BD2)
##   [1] "REGISTRO"          "D4_2"              "D5"               
##   [4] "P106"              "P107"              "P1"               
##   [7] "P2"                "P3"                "P4"               
##  [10] "P5"                "P6"                "P7A_1"            
##  [13] "P7A_2"             "P7A_3"             "P7_OTRO"          
##  [16] "P7B"               "T9_1"              "T9_2"             
##  [19] "T9_3"              "T9_4"              "T9_5"             
##  [22] "T9_6"              "T9_7"              "P118"             
##  [25] "CP10_1"            "CP10_2"            "CP10_3"           
##  [28] "P10_1"             "P10_2"             "P10_3"            
##  [31] "T11_12"            "T11_13"            "P12_1"            
##  [34] "P12_2"             "P12_3"             "P12_4"            
##  [37] "P12_5"             "P12_6"             "P12_7"            
##  [40] "P12_OTRO"          "P13"               "HORA_CAP_3"       
##  [43] "TIEMPO_CAP_2_SEG"  "TIEMPO_CAP_2_MIN"  "P14"              
##  [46] "P15_1"             "P15_2"             "P15_3"            
##  [49] "P15_4"             "P15_5"             "P15_6"            
##  [52] "P15_7"             "P15_8"             "P15_9"            
##  [55] "P15_10"            "P15_11"            "P15_12"           
##  [58] "P15_13"            "P15_OTRO"          "P20A"             
##  [61] "T21_1"             "T21_2"             "T21_3"            
##  [64] "T21_4"             "T21_5"             "T21_6"            
##  [67] "T21_7"             "T21_8"             "T21_9"            
##  [70] "T21_10"            "T21_11"            "T21_12"           
##  [73] "T21_13"            "T21_14"            "P22N"             
##  [76] "P22"               "P22_OTRO"          "P23"              
##  [79] "P24"               "P25"               "P26"              
##  [82] "P27_1"             "P27_2"             "P27_3"            
##  [85] "P27_4"             "P27_OTRO"          "T28_1"            
##  [88] "T28_2"             "T28_3"             "T28_4"            
##  [91] "T28_5"             "T28_6"             "P29"              
##  [94] "P30"               "P31"               "P32_1"            
##  [97] "P32_2"             "P32_3"             "HORA_CAP_7"       
## [100] "TIEMPO_CAP_6_SEG"  "TIEMPO_CAP_6_MIN"  "P33N"             
## [103] "P33"               "P33_OTRO_1"        "P34"              
## [106] "P35_1"             "P35_2"             "P35_3"            
## [109] "P35_4"             "P35_5"             "P35_6"            
## [112] "P35_7"             "P35_8"             "P35_9"            
## [115] "P35_10"            "P36"               "P39"              
## [118] "P40_1"             "P40_2"             "P40_3"            
## [121] "T41_1"             "T41_2"             "T41_3"            
## [124] "T41_4"             "T41_5"             "T41_6"            
## [127] "T41_7"             "P42"               "P43"              
## [130] "P44"               "P37"               "P38_1"            
## [133] "P38_2"             "P38_3"             "P45"              
## [136] "P46_1"             "P46_2"             "P46_OTRO"         
## [139] "P47N"              "P47"               "P47_OTRO"         
## [142] "P48"               "P49"               "P52"              
## [145] "P53_1"             "P53_2"             "P53_3"            
## [148] "P53_OTRO"          "T54_1"             "T54_2"            
## [151] "T54_3"             "T54_4"             "T54_5"            
## [154] "T54_6"             "T54_7"             "P55"              
## [157] "P56"               "P50"               "P51_1"            
## [160] "P51_2"             "P51_3"             "HORA_CAP_9"       
## [163] "TIEMPO_CAP_8_SEG"  "TIEMPO_CAP_8_MIN"  "CONTINUAR_1"      
## [166] "P57N"              "P57"               "P57_CUAL"         
## [169] "P58"               "P59"               "P59_OTRO"         
## [172] "P60"               "P61"               "P64"              
## [175] "P65_1"             "P65_2"             "P65_3"            
## [178] "T66_1"             "T66_2"             "T66_3"            
## [181] "T66_4"             "T66_5"             "T66_6"            
## [184] "T66_7"             "P67"               "P68"              
## [187] "P62"               "P63"               "HORA_CAP_10"      
## [190] "TIEMPO_CAP_9_SEG"  "TIEMPO_CAP_9_MIN"  "CONTINUAR_2"      
## [193] "P69"               "P70"               "P71_COD"          
## [196] "P71"               "P71N"              "P71L"             
## [199] "P72"               "P73"               "P74"              
## [202] "P75_1"             "P75_2"             "P75_3"            
## [205] "P75_OTRO"          "T76_1"             "T76_2"            
## [208] "T76_3"             "T76_4"             "T76_5"            
## [211] "T76_6"             "T76_7"             "T76_8"            
## [214] "P77"               "P78"               "P78A"             
## [217] "P78B"              "HORA_CAP_11"       "TIEMPO_CAP_10_SEG"
## [220] "TIEMPO_CAP_10_MIN" "CONTINUAR_4"       "PFAM"             
## [223] "PFM"               "T79_1"             "T79_2"            
## [226] "T79_3"             "T79_4"             "T79_5"            
## [229] "MENCIONES_79"      "P80A"              "P80B"             
## [232] "P81"               "P81_CONOC"         "P81_OTRO_CONOC"   
## [235] "P81_DESC"          "P81_OTRO_DESC"     "P82"              
## [238] "P83_1_1"           "P83_1_2"           "P83_1_3"          
## [241] "P83_1_4"           "P83_1_5"           "P83_1_6"          
## [244] "P83_1_7"           "P84"               "T85_1"            
## [247] "T85_2"             "T85_3"             "T85_4"            
## [250] "T85_5"             "T85_6"             "P86_1"            
## [253] "P86_2"             "P86_3"             "P86A"             
## [256] "HORA_CAP_12"       "TIEMPO_CAP_11_SEG" "TIEMPO_CAP_11_MIN"
## [259] "T87_1"             "T87_2"             "T87_3"            
## [262] "T87_4"             "T87_5"             "T87_6"            
## [265] "T87_7"             "T87_8"             "T87_9"            
## [268] "T87_10"            "T87_11"            "T87_12"           
## [271] "T87_13"            "T87_14"            "T87_15"           
## [274] "T87_16"            "T87_17"            "T87_18"           
## [277] "T87_19"            "T87_20"            "T87_21"           
## [280] "T87_22"            "T87_23"            "P90"              
## [283] "CONTINUAR_3"       "HORA_CAP_13"       "TIEMPO_CAP_12_SEG"
## [286] "TIEMPO_CAP_12_MIN" "T92_1"             "T92_2"            
## [289] "T92_3"             "T92_4"             "T92_5"            
## [292] "T92_6"             "T92_7"             "T92_8"            
## [295] "T92_9"             "T92_10"            "T92_11"           
## [298] "T93_1"             "T93_2"             "T93_3"            
## [301] "T93_4"             "T93_5"             "P94_1"            
## [304] "P94_2"             "P94_3"             "P94_4"            
## [307] "P94_5"             "P94_6"             "P94_7"            
## [310] "P94_8"             "T96_1"             "T96_2"            
## [313] "T96_3"             "T96_4"             "T96_5"            
## [316] "T96_6"             "T96_7"             "T96_8"            
## [319] "T96_9"             "T96_10"            "T96_11"           
## [322] "T96_12"            "T96_13"            "T96_14"           
## [325] "T96_15"            "T96_16"            "T96_17"           
## [328] "T96_18"            "T96_19"            "T96_20"           
## [331] "T96_21"            "P97_1"             "P97_2"            
## [334] "P97_3"             "P98"               "P99_1"            
## [337] "P99_2"             "P99_3"             "P99_4"            
## [340] "P99_5"             "P99_6"             "P99_7"            
## [343] "P99_8"             "P100_1"            "P100_2"           
## [346] "P100_3"            "P100_4"            "P100_5"           
## [349] "P100_6"            "P100_7"            "P100_8"           
## [352] "P100_9"            "P100_10"           "P101"             
## [355] "T102_1"            "T102_2"            "T102_3"           
## [358] "T102_4"            "T102_5"            "T102_6"           
## [361] "T102_7"            "T102_8"            "T102_9"           
## [364] "T102_10"           "T102_11"           "T102_12"          
## [367] "T102_13"           "T102_14"           "T102_15"          
## [370] "P103_1"            "P103_2"            "P103_3"           
## [373] "P103_4"            "P103_5"            "P103_6"           
## [376] "P103_7"            "P103_8"            "P103_9"           
## [379] "P103_10"           "P103_11"           "T104_1"           
## [382] "T104_2"            "T104_3"            "T104_4"           
## [385] "T104_5"            "T104_6"            "HORA_CAP_14"      
## [388] "TIEMPO_CAP_13_SEG" "TIEMPO_CAP_13_MIN" "P105_1"           
## [391] "P105_2"            "P105_3"            "P105_4"           
## [394] "P105_5"            "P105_6"            "P108_1"           
## [397] "P108_2"            "CP108_PAIS"        "P108_PAIS"        
## [400] "P108_DEP"          "P108_OTRO_DEP"     "P108_MUNI"        
## [403] "P108_MUNI_OTRO"    "P109"              "P109_OTRO"        
## [406] "P110"              "P111"              "P113"             
## [409] "FACTOR"

Caracterización social

A continuación encontreremos una tabla con las variables mas importantes para analizar la problematica sobre seguridad, Rango de edad, Genero, estrato.

CR <- BD1 %>%
select(EDAD_RANGO,GENERO,ESTRATO_DEC)%>%
mutate(
  GENERO1=ifelse(GENERO =="1","Mujer","Hombre")
)
head(CR)
## # A tibble: 6 × 4
##   EDAD_RANGO GENERO ESTRATO_DEC GENERO1
##        <dbl>  <dbl>       <dbl> <chr>  
## 1         10      1           2 Mujer  
## 2          2      2           2 Hombre 
## 3          7      2           2 Hombre 
## 4          2      2           7 Hombre 
## 5          2      1           2 Mujer  
## 6          2      2           2 Hombre

Caracterización social por genero

CR %>%
  count(GENERO1) %>%
  mutate(Porcentaje = n / sum(n) * 100,
         Etiqueta = paste0(round(Porcentaje, 1), "%")) %>% 
  ggplot(aes(x = "", y = Porcentaje, fill = GENERO1)) +
  geom_col(width = 1, color = "white") +
  scale_fill_manual(values = c("Hombre" = "light blue", "Mujer" = "pink"))+
  coord_polar(theta = "y", start = 0) +
  geom_text(aes(label = Etiqueta), 
            position = position_stack(vjust = 0.5), 
            color = "white",                        
            size = 5) +                             
  theme_minimal() +
  labs(title = "Distribución por Género", 
       x = NULL, 
       y = NULL,
       caption = "Fuente: Estudio de seguridad Ciudad Castellana") 

##Caracterización de Estrato

CR %>%
  mutate(ESTRATO_DEC = as.factor(ESTRATO_DEC)) %>%
  count(ESTRATO_DEC) %>%
  mutate(Porcentaje = n / sum(n) * 100,
         Etiqueta = paste0(round(Porcentaje, 1), "%")) %>% 

  ggplot(aes(x = reorder(ESTRATO_DEC, -Porcentaje), y = Porcentaje, fill = ESTRATO_DEC)) +
  geom_col(color = "white") +
  geom_text(aes(label = Etiqueta), 
            vjust = -0.5, 
            color = "black",                        
            size = 4) +                             
  theme_minimal() +
  
  theme(legend.position = "none") +
  
  labs(title = "Estrato Socioeconómico", 
       x = "Estrato", 
       y = "Porcentaje (%)",
       caption = "Fuente: Estudio de seguridad Ciudad Castellana")

## Caracterización de Rango de edad

CR %>%
  mutate(EDAD_RANGO = as.factor(EDAD_RANGO)) %>%
  count(EDAD_RANGO) %>%
  mutate(Porcentaje = n / sum(n) * 100,
         Etiqueta = paste0(round(Porcentaje, 1), "%")) %>% 
  
  ggplot(aes(x = Porcentaje, y = reorder(EDAD_RANGO, -Porcentaje), fill = EDAD_RANGO)) +
  geom_col(color = "white") +
  
  geom_text(aes(label = Etiqueta), 
            hjust = -0.2, 
            color = "black",                        
            size = 4) +                             
  theme_minimal() +
  theme(legend.position = "none") +
  
  scale_x_continuous(expand = expansion(mult = c(0, 0.15))) +
  
  labs(title = "Distribución por Rango de Edad", 
       x = "Porcentaje (%)", 
       y = "Rango de Edad",
       caption = "Fuente: Estudio de seguridad Ciudad Castellana")

PERCEPCION DE SEGURIDAD

PS <- BD2 %>%
select(P1,P3,P5) %>%
rename( Barrio =P1,Comuna =P3,Ciudad=P5)
head(PS)
## # A tibble: 6 × 3
##   Barrio Comuna Ciudad
##    <dbl>  <dbl>  <dbl>
## 1      3      2      2
## 2      2      2      2
## 3      2      2      3
## 4      2      2      2
## 5      2      2      2
## 6      2      2      2

Percepcion de seguridad en la ciudad

PS %>% 
  mutate(across(c(Barrio, Comuna, Ciudad), as.character)) %>% 
  pivot_longer(cols = c(Barrio, Comuna, Ciudad), 
               names_to = "Variable", 
               values_to = "Categoria") %>% 
  group_by(Variable, Categoria) %>% 
  summarise(n = n(), .groups = "drop_last") %>% 
  mutate(Porcentaje = n / sum(n) * 100,
         Etiqueta = paste0(round(Porcentaje, 1), "%")) %>% 
  ungroup() %>% 
  ggplot(aes(x = Categoria, y = Porcentaje, fill = Categoria)) +
  geom_col(color = "white") +
  geom_text(aes(label = Etiqueta),
            vjust = -0.5,
            color = "black",
            size = 3.5) +
  facet_wrap(~ Variable, scales = "free_x") +
  scale_x_discrete(labels = c("1" = "Muy Seguro", 
                              "2" = "Seguro", 
                              "3" = "Inseguro",
                              "4" = "Muy Inseguro")) +
  theme_minimal() +
  
  theme(legend.position = "none",
        strip.text = element_text(face = "bold", size = 13, hjust = 0),
        axis.text.x = element_text(angle = 45, hjust = 1)) +            
  scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
  labs(title = "Percepción de Seguridad por Barrio, Comuna y Ciudad",
       x = "Nivel de Seguridad",
       y = "Porcentaje (%)",
       caption = "Fuente: Estudio de seguridad Ciudad Castellana")

De la grafica anterior se puede observar que el barro y comuna las personas sienten mayor seguridad pero en la ciudad esta percepción disminuye diviendose las opiniones entre seguro e inseguro.

PERCEPCION POR VARIBLES

pv <- left_join(BD1, BD2)%>%
mutate(
  GENERO1=ifelse(GENERO =="1","Mujer","Hombre")
)
## Joining with `by = join_by(REGISTRO)`
head(pv)
## # A tibble: 6 × 423
##   REGISTRO COMUNA    PI    PJ    PA    PB    PC PC_EDAD EDAD_RANGO GENERO
##      <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>      <dbl>  <dbl>
## 1       25      3     1     1     1     1     1      69         10      1
## 2       26      3     1     1     1     1     1      24          2      2
## 3       27      1     1     1     3     1     1      48          7      2
## 4       30      1     1     1     3     1     1      19          2      2
## 5       36      7     1     1     2     1     1      18          2      1
## 6       38      7     1     1     1     1     1      23          2      2
## # ℹ 413 more variables: ESTRATO_DEC <dbl>, ESTRATO <lgl>, PD <dbl>, D1 <dbl>,
## #   D4_2 <dbl>, D5 <dbl>, P106 <dbl>, P107 <dbl>, P1 <dbl>, P2 <dbl>, P3 <dbl>,
## #   P4 <dbl>, P5 <dbl>, P6 <dbl>, P7A_1 <dbl>, P7A_2 <dbl>, P7A_3 <dbl>,
## #   P7_OTRO <chr>, P7B <dbl>, T9_1 <dbl>, T9_2 <dbl>, T9_3 <dbl>, T9_4 <dbl>,
## #   T9_5 <dbl>, T9_6 <dbl>, T9_7 <dbl>, P118 <dbl>, CP10_1 <dbl>, CP10_2 <dbl>,
## #   CP10_3 <dbl>, P10_1 <chr>, P10_2 <chr>, P10_3 <chr>, T11_12 <dbl>,
## #   T11_13 <dbl>, P12_1 <dbl>, P12_2 <dbl>, P12_3 <dbl>, P12_4 <dbl>, …
pv %>%
  rename(Barrio = P1, Comuna = P3, Ciudad = P5) %>% 
  mutate(across(c(GENERO1, Barrio, Comuna, Ciudad), as.character)) %>% 
  pivot_longer(cols = c(Barrio, Comuna, Ciudad), 
               names_to = "Variable", 
               values_to = "Categoria") %>% 
  group_by(Variable, GENERO1, Categoria) %>% 
  summarise(n = n(), .groups = "drop_last") %>% 
  mutate(Porcentaje = n / sum(n) * 100,
         Etiqueta = paste0(round(Porcentaje, 1), "%")) %>% 
  ungroup() %>% 
  filter(!is.na(GENERO1), !is.na(Categoria)) %>% 
  ggplot(aes(x = Categoria, y = Porcentaje, fill = GENERO1)) +
  geom_col(position = "dodge", color = "white") +
  scale_fill_manual(values = c("Hombre" = "light blue", "Mujer" = "pink"))+
  geom_text(aes(label = Etiqueta),
            position = position_dodge(width = 0.9),
            hjust = -0.1,
            color = "black",
            size = 3) +
  facet_wrap(~ Variable, ncol = 1) +
  scale_x_discrete(labels = c("1" = "Muy Seguro", 
                              "2" = "Seguro", 
                              "3" = "Inseguro",
                              "4" = "Muy Inseguro")) +
  coord_flip() +
  theme_minimal() +
  theme(legend.position = "bottom",
        strip.text = element_text(face = "bold", size = 12, hjust = 0)) + 
  scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
  labs(title = "Percepción de Seguridad por Barrio, Comuna y Ciudad según Género",
       x = NULL,
       y = "Porcentaje (%)",
       fill = "Género:",
       caption = "Fuente: Estudio de seguridad ciudad castellana")

De acuerdo con la grafica anterior podemos observar que en la ciudad las mujeres perciben mayor seguridad frente a los hombres.

Percepción por estrato en el barrio

pv %>%   rename(Barrio = P1) %>%   mutate(across(c(ESTRATO_DEC, Barrio), as.character)) %>%   group_by(ESTRATO_DEC, Barrio) %>%   summarise(n = n(), .groups = "drop_last") %>%   mutate(Porcentaje = n / sum(n) * 100,          Etiqueta = paste0(round(Porcentaje, 1), "%")) %>%   ungroup() %>%   filter(!is.na(ESTRATO_DEC), !is.na(Barrio)) %>%   ggplot(aes(x = Barrio, y = Porcentaje, fill = ESTRATO_DEC)) +   geom_col(position = "dodge", color = "white") +   geom_text(aes(label = Etiqueta),             position = position_dodge(width = 0.9),             vjust = -0.5,             color = "black",             size = 2.5) +   scale_x_discrete(labels = c("1" = "Muy Seguro",                               "2" = "Seguro",                               "3" = "Inseguro",                               "4" = "Muy Inseguro")) +   theme_minimal() +   theme(legend.position = "bottom") +   scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +   labs(title = "Percepción de Seguridad en el Barrio según Estrato",        x = "Nivel de Seguridad",        y = "Porcentaje (%)",        fill = "Estrato:",        caption = "Fuente: Estudio de seguridad ciudad castellana")

De acuerdo con lo atenrior se puede observar que las personas de estrato 6 son la que perciben mayor seguridad en el barrio, y las estrato 5 menor seguridad

Percepción por estrato en la comuna

pv %>%   rename(Comuna = P3) %>%   mutate(across(c(ESTRATO_DEC, Comuna), as.character)) %>%   group_by(ESTRATO_DEC, Comuna) %>%   summarise(n = n(), .groups = "drop_last") %>%   mutate(Porcentaje = n / sum(n) * 100,          
  Etiqueta = paste0(round(Porcentaje, 1), "%")) %>%   
  ungroup() %>%   
  filter(!is.na(ESTRATO_DEC), !is.na(Comuna)) %>%   
  ggplot(aes(x = Comuna, y = Porcentaje, fill = ESTRATO_DEC)) +   
  geom_col(position = "dodge", color = "white") +   
  geom_text(aes(label = Etiqueta),             
            position = position_dodge(width = 0.9),             
            vjust = -0.5,             
            color = "black",             
            size = 2.5) +   scale_x_discrete(labels = c("1" = "Muy Seguro",                               "2" = "Seguro",                               "3" = "Inseguro",                               "4" = "Muy Inseguro")) +   
theme_minimal() +   
theme(legend.position = "bottom") +   
scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +   
  labs(title = "Percepción de Seguridad en la comuna según Estrato",        
       x = "Nivel de Seguridad",        
       y = "Porcentaje (%)",        
       fill = "Estrato:",        
       caption = "Fuente: Estudio de seguridad ciudad castellana")

De acuerdo con lo atenrior se puede observar que las personas de estrato 1 y 2 son los que perciben mayor seguridad en la comuna y los de estrato 6 se sienten muy inseguros

Percepción por estrato en la ciudad

pv %>%   rename(Ciudad = P5) %>%   mutate(across(c(ESTRATO_DEC, Ciudad), as.character)) %>%   group_by(ESTRATO_DEC, Ciudad) %>%   summarise(n = n(), .groups = "drop_last") %>%   mutate(Porcentaje = n / sum(n) * 100,          
  Etiqueta = paste0(round(Porcentaje, 1), "%")) %>%   
  ungroup() %>%   
  filter(!is.na(ESTRATO_DEC), !is.na(Ciudad)) %>%   
  ggplot(aes(x = Ciudad, y = Porcentaje, fill = ESTRATO_DEC)) +   
  geom_col(position = "dodge", color = "white") +   
  geom_text(aes(label = Etiqueta),             
            position = position_dodge(width = 0.9),             
            vjust = -0.5,             
            color = "black",             
            size = 2.5) +   scale_x_discrete(labels = c("1" = "Muy Seguro",                               "2" = "Seguro",                               "3" = "Inseguro",                               "4" = "Muy Inseguro")) +   
theme_minimal() +   
theme(legend.position = "bottom") +   
scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +   
  labs(title = "Percepción de Seguridad en la ciudad según Estrato",        
       x = "Nivel de Seguridad",        
       y = "Porcentaje (%)",        
       fill = "Estrato:",        
       caption = "Fuente: Estudio de seguridad ciudad castellana")

De acuerdo con o atenrior se ve que las personas que en mayor parte se sienten segura en la ciudad son las de estrato 6 pero así mismo el restante se siente muy inseguro que equivale al 33%, falta validar si los que no mencionan estrato ( número 7) en que estrato se identifican

INDICE DE VICTIMIZACIÓN

pv_con_indice <- pv%>%
  select(COMUNA,P20A,FACTOR) %>% 
  mutate(
 Condicion_Victima = case_when(P20A == 1 | P20A == 2 | P20A == 3 | P20A == 4 | P20A == 6 ~ 1, TRUE ~ 0))
print(pv_con_indice)
## # A tibble: 4,673 × 4
##    COMUNA  P20A FACTOR Condicion_Victima
##     <dbl> <dbl>  <dbl>             <dbl>
##  1      3     6  176.                  1
##  2      3    10  280.                  0
##  3      1    10  180.                  0
##  4      1    10  245.                  0
##  5      7    10  128.                  0
##  6      7    10   63.7                 0
##  7      7    10  169.                  0
##  8      6    10  220.                  0
##  9      6    10  266.                  0
## 10      3    10  345.                  0
## # ℹ 4,663 more rows
indice_ciudad <- pv_con_indice %>%
  summarise(
    Indice_ciudad = (sum(Condicion_Victima * FACTOR, na.rm = TRUE) / sum(FACTOR, na.rm = TRUE)) * 100
  )
print("ÍNDICE DE VICTIMIZACIÓN GENERAL (PONDERADO)")
## [1] "ÍNDICE DE VICTIMIZACIÓN GENERAL (PONDERADO)"
print(indice_ciudad)
## # A tibble: 1 × 1
##   Indice_ciudad
##           <dbl>
## 1          20.0
indice_comunas <- pv_con_indice %>%
  group_by(COMUNA) %>%
  summarise(
    Indice = (sum(Condicion_Victima * FACTOR, na.rm = TRUE) / sum(FACTOR, na.rm = TRUE))*100
  )%>%
  arrange(desc(Indice))
print(indice_comunas)
## # A tibble: 20 × 2
##    COMUNA Indice
##     <dbl>  <dbl>
##  1      5   27.6
##  2      7   23.4
##  3     60   22.9
##  4      4   22.5
##  5      8   21.6
##  6     13   20.8
##  7     16   20.8
##  8      3   20.5
##  9      2   19.7
## 10      9   18.9
## 11      1   18.2
## 12     10   18.0
## 13     80   17.0
## 14     12   15.1
## 15     90   14.3
## 16      6   13.8
## 17     11   13.1
## 18     70   12.7
## 19     15   11.3
## 20     50    0

¿ Cuál es el índice de victimización para la icudad castellana?

Respuesta: El indice es de 20

¿ cuál es el índice de victimización por comunas?

library(knitr)
kable(indice_comunas, digits = 4, 
      col.names = c("Comuna", "Índice de Victimización"),
      caption = "Índice de Victimización por Comuna")
Índice de Victimización por Comuna
Comuna Índice de Victimización
5 27.5735
7 23.4415
60 22.9498
4 22.5247
8 21.5679
13 20.7877
16 20.7770
3 20.5160
2 19.6810
9 18.8964
1 18.1827
10 18.0033
80 16.9776
12 15.1208
90 14.2799
6 13.7626
11 13.1406
70 12.7091
15 11.2756
50 0.0000

¿ En que comuna es mayor este índice?

Respuesta: la comuna con mayor indice es la comuna 5 con 27,5735

¿ En que comuna es menor indice ?

Respuesta: la comuna con menor indice es la comuna 15 con 11,2756