# 1. Librerías -------------------------------------------------------------
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.2
library(extrafont)   # sólo la primera vez: font_import(pattern="Times")
## Warning: package 'extrafont' was built under R version 4.2.2
## Registering fonts with R
# loadfonts(device="win")  # en Windows, o loadfonts() en Linux/Mac

# 2. Importar datos --------------------------------------------------------
df <- read_excel("C:/Users/PC/Desktop/sinai/variables_recodificadas.xlsx")

# 3. Convertir a factor ---------------------------------------------------
df$Exposure <- factor(df$Exposure,
  levels = c(0, 1),
  labels = c("No", "Sí")
)
df$Concern <- factor(df$Concern,
  levels = c(1, 2, 3, 4),
  labels = c("Nada", "Poco", "Algo", "Mucho")
)
df$Experience <- factor(df$Experience,
  levels = c(0, 1),
  labels = c("No", "Sí")
)
df$Knowledge <- factor(df$Knowledge,
  levels = c(0, 1),
  labels = c("No", "Sí")
)
df$Prepared <- factor(df$Prepared,
  levels = c(0, 1),
  labels = c("No preparada/o", "Preparada/o")
)
df$Education <- factor(df$Education,
  levels = c(0, 1, 2),
  labels = c("Sin escolaridad", "Primaria", "Secundaria/Técnico")
)

# 4. Tema común con Times 10 ----------------------------------------------
tema_times <- theme_classic() +
  theme(
    text         = element_text(family = "Times", size = 10),
    axis.title   = element_text(size = 10),
    axis.text    = element_text(size = 10),
    legend.title = element_text(size = 10),
    legend.text  = element_text(size = 10)
  )

# 5. Gráficos --------------------------------------------------------------

## a) Exposición vs Preocupación
p1 <- ggplot(df, aes(x = Exposure, fill = Concern)) +
  geom_bar(position = "fill") +
  scale_y_continuous(labels = scales::percent) +
  labs(
    title = "Exposición comunitaria vs Nivel de Preocupación",
    x     = "Comunidad expuesta",
    y     = "Proporción",
    fill  = "Preocupación"
  ) +
  tema_times

p1 
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

# ggsave("plot_exposure_concern.png", p1, dpi = 700, width = 6, height = 4)

## b) Conocimiento vs Preparación
p2 <- ggplot(df, aes(x = Knowledge, fill = Prepared)) +
  geom_bar(position = "fill") +
  scale_y_continuous(labels = scales::percent) +
  labs(
    title = "Conocimiento de medidas vs Sensación de Preparación",
    x     = "Conoce medidas preventivas",
    y     = "Proporción",
    fill  = "Se siente preparado/a"
  ) +
  tema_times

p2
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

#ggsave("plot_knowledge_prepared.png", p2, dpi = 700, width = 6, height = 4)

## c) Experiencia vs Preocupación
p3 <- ggplot(df, aes(x = Experience, fill = Concern)) +
  geom_bar(position = "fill") +
  scale_y_continuous(labels = scales::percent) +
  labs(
    title = "Experiencia directa vs Nivel de Preocupación",
    x     = "Ha presenciado deslizamientos",
    y     = "Proporción",
    fill  = "Preocupación"
  ) +
  tema_times
p3
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

##ggsave("plot_experience_concern.png", p3, dpi = 700, width = 6, height = 4)

## d) Escolaridad vs Conocimiento
p4 <- ggplot(df, aes(x = Education, fill = Knowledge)) +
  geom_bar(position = "fill") +
  scale_y_continuous(labels = scales::percent) +
  labs(
    title = "Escolaridad vs Conocimiento de Medidas Preventivas",
    x     = "Nivel de escolaridad",
    y     = "Proporción",
    fill  = "Conoce medidas"
  ) +
  tema_times

p4
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

##ggsave("plot_education_knowledge.png", p4, dpi = 700, width = 6, height = 4)