#Cargando

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.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(readxl)
datos <- read_excel("base_datos.xlsx")
datos<-mutate(datos,HC13=factor(HC13,levels = c("Medido", "No presente","Rechazó",
                                                "Parcialmente medido","Otro")))
datos<-mutate(datos,HC55=factor(HC55,levels = c("Medido","No presente","Rechazo",
                                                "Otro")))
datos<-mutate(datos,HC57=factor(HC57,levels = c("Grave", "Moderado", "Leve",
                                                "Sin anemia")))
datos<-mutate(datos,HC61=factor(HC61,levels = c("Sin educación","Primaria","Secundaria",
                                               "Superior","No sabe")))

#Pregunta 1

ggplot(datos,aes(x=HC57)) + geom_bar(fill="skyblue",color="black") +
  geom_text(stat="count",aes(label = ..count..),vjust = -0.5)+
  labs(title="Nivel de anemia de los niños menores de 5 años",
       x="Nivel de anemia",y="Porcentaje")
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#Pregunta 2

frecuen <- count(datos,HC57)
frecuen <- mutate(frecuen,porcentaje=n/sum(n))
ggplot(frecuen,aes(x=HC57,y=porcentaje,fill=HC57))+
  geom_col()+theme_bw()+guides(fill=F)+
  geom_label(aes(label=scales::percent(porcentaje)),size=3.5,
             position = position_stack(vjust=1.0))+
  labs(title="Nivel de anemia de los niños menores de 5 años",
       x="Nivel de anemia",y="Porcentaje")
## 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.

# Pregunta 3

frecuenc <- count(datos,HC57,HC27)
frecuenc <- mutate(frecuenc,Porcentaje=n/sum(n)*100)
ggplot(frecuenc, aes(x = HC57, y = Porcentaje, fill = HC27)) +
  geom_bar(stat = "identity", position = "dodge")+ 
  geom_text(
    aes(label = paste0(round(Porcentaje, 1), "%")), 
    position = position_dodge(width = 0.9), 
    vjust = -0.5
  ) +
  labs(
    title = "Nivel de anemia según sexo en niños menores de 5 años",
    x = "Nivel de anemia",
    y = "Porcentaje",
    fill = "Sexo"
  ) +
  scale_y_continuous(limits=c(0,40),
                     breaks=seq(0,40,10),
                     labels = scales::percent_format(scale = 1)) + theme_bw()

#Pregunta 4

ggplot(frecuenc, aes(x = HC57, y = Porcentaje, fill = HC27)) +
  geom_bar(stat = "identity", position = "dodge") + 
  geom_text(
    aes(label = paste0(round(Porcentaje, 1), "%")), 
    position = position_dodge(width = 0.9), 
    vjust = -0.5 
  ) +
  labs(
    title = "Nivel de anemia según sexo en niños menores de 5 años",
    x = "Nivel de anemia",
    y = "Porcentaje",
    fill = "Sexo"
  ) +
  scale_y_continuous(limits=c(0,40),
                     breaks=seq(0,40,10),
                     labels = scales::percent_format(scale = 1))+
  theme_bw() + facet_wrap(~HC27)

#Pregunta 5

ggplot(datos,aes(x=HC53))+
  geom_histogram(bins = 42,
                 colour = "black",fill="yellow"
  )+ theme_bw()+
  labs(title = "Distribución de nivel de hemoglobina en niños menores de 5 años",
       x = "Nivel de hemoglobina",
       y = "Frecuencia")

#Pregunta 6

ggplot(datos,aes(x=HC53))+
  geom_histogram(binwidth = 4,color="black",fill="blue")+
  geom_freqpoly(binwidth=4,color="red",size=1.0)+
  theme_bw(base_size=10)+
  labs(x="Nivel de hemoglobina",
       y="Frecuencia")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#Pregunta 7

ggplot(datos,aes(x=HC53))+
  geom_histogram(bins = 42,
                 colour = "black",fill="blue"
  )+ theme_bw()+
  labs(title = "Distribución de nivel de hemoglobina en niños menores de 5 años",
       x = "Nivel de hemoglobina",
       y = "Frecuencia")+ facet_wrap(~HC57)

ggplot(datos,aes(x=HC53))+
  geom_histogram(bins = 40,
                 colour = "black",fill="blue"
  )+ theme_bw()+
  labs(title = "Distribución de nivel de hemoglobina en niños menores de 5 años",
       x = "Nivel de hemoglobina",
       y = "Frecuencia")+ facet_wrap(~HC27)

#Pregunta 8

ggplot(datos,aes(x=HC53,fill=HC27))+geom_density(
  size = 1.0, 
  kernel = "gaussian",
  color = "red", 
  bw = 1)+
  theme_bw(base_size = 15)+
  labs(x="Nivel de hemoglobina",
       y="Frecuencia",fill="Sexo")

ggplot(datos,aes(x=HC53,fill=HC57))+geom_density(alpha = 0.5,
  size = 0.5, 
  kernel = "gaussian",
  color = "red", 
  bw = 1)+
  theme_bw(base_size = 15)+
  labs(x="Nivel de hemoglobina",
       y="Frecuencia",fill="Nivel")

#Pregunta 9

ggplot(datos,aes(x=HC57,
                     y=HC56,
                     fill=HC57))+
  geom_boxplot()+
  guides(fill=F)+
  theme_bw()+
  labs(
    title = "Diagrama de cajas y bigotes de nivel de hemoglobina",
    x = "Nivel de anemia",
    y = "")

#Pregunta 10

ggplot(datos,aes(x=HC57,y=HC56,fill=HC57))+
  geom_violin(adjust=1.0)+ # Menor valor muestra la distribución de la variable
  theme_bw(base_size=10)+
  scale_fill_brewer(palette="Set1")+
  theme(legend.position = "none")+
  labs(
    x = "Nivel de anemia",
    y = "") 

#Pregunta 11

circular <- count(datos,HC57)
circular <- mutate(circular,porcentaje=n/sum(n)*100)
circular
## # A tibble: 4 × 3
##   HC57           n porcentaje
##   <fct>      <int>      <dbl>
## 1 Grave         12     0.0586
## 2 Moderado    1776     8.68  
## 3 Leve        4780    23.4   
## 4 Sin anemia 13898    67.9
ggplot(circular,aes(x="",y=HC57,fill=HC57))+
  geom_col(color="black")+
  coord_polar("y",direction = 1)+
  geom_text(
    aes(x=1.5, 
        label=paste(porcentaje,"%")),
    position=position_stack(vjust = 0.5),
    size=5)+
  theme_bw(base_size=14)+theme(line=element_blank(),
                                axis.text = element_blank(), 
                                legend.position = "right") +
  labs(fill="Nivel de anemia",x=" ")

ggplot(datos,aes(x=HC56,y=HC1))+geom_point(aes(colour=HC57))