Curso de gráficas

1.1 Gráficas con una variable

1.1.1 Gráficas con 1 variable con formato

Este va a ser el espacio de trabajo para mi cuarta gráfica.

Los datos fueron tomados de la ENOE

El objetivo es describir el comportamiento de las personas ocupadas por sexo y estado conyugal para tener una idea más clara de las características socioeconómicas de las personas ocupadas según la ENOE.

#1. Cargar librerias
paquetes=c("scales","survey","ggsurvey","forcats","haven","readxl","fastDummies","readr", "dplyr", "foreign", "tidyr", "ggplot2","gridExtra")
for (i in paquetes) {if (!require(i, character.only = TRUE)) {install.packages(i);library(i, character.only = TRUE)} else {library(i, character.only = TRUE)}}

#2.  Directorio de trabajo

setwd("~/Desktop/Cosas en R/Clase1-Visualización")

enoe=read_dta("ENOE_SDEMT423.dta")


# Variable filtro

enoe <- enoe %>%
  mutate(f = r_def == 0 & (c_res == 1 | c_res == 3) & eda >= 15 & eda <= 98)



enoe=enoe %>% filter(f==1)



#3. Preparar datos

#3.1 Subconjuntos de datos

# CLASE2=Condicion de ocupacion
# Codigos: 1=Ocupado, 2=Desocupado, 3=Disponible, 4=No disponible

# Convertir la variable a dato numerico
enoe$clase2=as.numeric(enoe$clase2)

# Filtrar la base, necesitamos a los ocupados CLASE2=1
ocupados=enoe%>%filter(clase2==1)

#3.2 Etiquetado de variables


# Codigos: 1=Hombre, 2=Mujer
ocupados <- ocupados %>%
  mutate(sex = fct_recode(factor(sex),#miutei
                          Hombre = "1",
                          Mujer = "2"))

# pos_ocu=Posicion en la ocupacion
# Codigos: 1=Suborninado, 2=Independiente, 3=Disponible, 0=No aplica
ocupados <- ocupados %>%
  mutate(pos_ocu = fct_recode(factor(pos_ocu), 
                              "No Aplica" = "0",
                              "Suborninado" = "1",
                              "Empleadores" = "2",
                              "Cuenta propia" = "3",
                              "Sin pago" = "4"))

#---------------------------------------
# NOTA:
# Estamos recodificando en la misma
# variable, entonces se pierden los 
# valores originales.
#---------------------------------------

ocupados <- ocupados %>%
  mutate(e_con = fct_recode(factor(e_con),#miutei
                        "Unión libre" = "1",
                          "Separado (a)"= "2",
                          "Divorciado (a)"="3",
                          "Viudo (a)"="4",
                          "Casado (a)"="5",
                          "Soltero (a)"="6",
                          "No sabe"="9"))


#4. Graficas (ggplot)

#4.1 Con 1 variable discreta

class(ocupados$sex)  
## [1] "factor"
names(enoe)
##   [1] "r_def"      "loc"        "mun"        "est"        "est_d_tri" 
##   [6] "est_d_men"  "ageb"       "t_loc_tri"  "t_loc_men"  "cd_a"      
##  [11] "ent"        "con"        "upm"        "d_sem"      "n_pro_viv" 
##  [16] "v_sel"      "n_hog"      "h_mud"      "n_ent"      "per"       
##  [21] "n_ren"      "c_res"      "par_c"      "sex"        "eda"       
##  [26] "nac_dia"    "nac_mes"    "nac_anio"   "l_nac_c"    "cs_p12"    
##  [31] "cs_p13_1"   "cs_p13_2"   "cs_p14_c"   "cs_p15"     "cs_p16"    
##  [36] "cs_p17"     "n_hij"      "e_con"      "cs_p20a_1"  "cs_p20a_c" 
##  [41] "cs_p20b_1"  "cs_p20b_c"  "cs_p20c_1"  "cs_ad_mot"  "cs_p21_des"
##  [46] "cs_ad_des"  "cs_nr_mot"  "cs_p23_des" "cs_nr_ori"  "ur"        
##  [51] "zona"       "salario"    "fac_tri"    "fac_men"    "clase1"    
##  [56] "clase2"     "clase3"     "pos_ocu"    "seg_soc"    "rama"      
##  [61] "c_ocu11c"   "ing7c"      "dur9c"      "emple7c"    "medica5c"  
##  [66] "buscar5c"   "rama_est1"  "rama_est2"  "dur_est"    "ambito1"   
##  [71] "ambito2"    "tue1"       "tue2"       "tue3"       "busqueda"  
##  [76] "d_ant_lab"  "d_cexp_est" "dur_des"    "sub_o"      "s_clasifi" 
##  [81] "remune2c"   "pre_asa"    "tip_con"    "dispo"      "nodispo"   
##  [86] "c_inac5c"   "pnea_est"   "niv_ins"    "eda5c"      "eda7c"     
##  [91] "eda12c"     "eda19c"     "hij5c"      "domestico"  "anios_esc" 
##  [96] "hrsocup"    "ingocup"    "ing_x_hrs"  "tpg_p8a"    "tcco"      
## [101] "cp_anoc"    "imssissste" "ma48me1sm"  "p14apoyos"  "scian"     
## [106] "t_tra"      "emp_ppal"   "tue_ppal"   "trans_ppal" "mh_fil2"   
## [111] "mh_col"     "sec_ins"    "tipo"       "mes_cal"    "f"
glimpse(enoe)
## Rows: 323,118
## Columns: 115
## $ r_def      <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ loc        <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ mun        <dbl> 10, 13, 5, 7, 5, 14, 13, 7, 7, 7, 2, 14, 14, 11, 11, 13, 12…
## $ est        <dbl> 30, 30, 20, 30, 20, 30, 30, 20, 20, 20, 20, 40, 30, 30, 20,…
## $ est_d_tri  <dbl> 124, 124, 123, 124, 123, 124, 124, 123, 123, 123, 123, 125,…
## $ est_d_men  <dbl> NA, NA, NA, NA, 115, 116, 116, 115, 115, 115, 115, 117, 116…
## $ ageb       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ t_loc_tri  <dbl+lbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1…
## $ t_loc_men  <dbl+lbl> NA, NA, NA, NA,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,…
## $ cd_a       <dbl+lbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ ent        <dbl+lbl> 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9…
## $ con        <dbl> 40002, 40013, 40032, 40035, 40071, 40081, 40085, 40087, 400…
## $ upm        <dbl> 912033, 915488, 902860, 909615, 905080, 916744, 915917, 908…
## $ d_sem      <dbl+lbl> 101, 201, 401, 401, 302, 402, 405, 402, 402, 402, 203, …
## $ n_pro_viv  <dbl> 76, 54, 85, 36, 100, 114, 15, 61, 61, 61, 121, 20, 5, 129, …
## $ v_sel      <dbl+lbl> 4, 3, 5, 4, 3, 2, 5, 5, 5, 5, 4, 4, 1, 5, 2, 3, 4, 4, 1…
## $ n_hog      <dbl+lbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ h_mud      <dbl+lbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ n_ent      <dbl+lbl> 5, 4, 2, 2, 3, 2, 2, 2, 2, 2, 4, 3, 3, 1, 4, 4, 3, 1, 2…
## $ per        <dbl+lbl> 423, 423, 423, 423, 423, 423, 423, 423, 423, 423, 423, …
## $ n_ren      <dbl+lbl> 3, 1, 1, 4, 1, 1, 2, 1, 2, 6, 5, 1, 2, 1, 4, 1, 2, 1, 1…
## $ c_res      <dbl+lbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ par_c      <dbl> 401, 101, 101, 408, 101, 101, 301, 101, 201, 301, 408, 101,…
## $ sex        <dbl+lbl> 2, 2, 1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 1, 2…
## $ eda        <dbl> 93, 67, 43, 27, 53, 70, 33, 34, 35, 15, 17, 45, 65, 61, 34,…
## $ nac_dia    <dbl+lbl> 10,  1, 15,  2,  6, 29, 15, 28, 25, 21, 23, 21, 26, 10,…
## $ nac_mes    <dbl+lbl>  2,  9,  6,  7, 10, 11, 11,  3,  2,  1,  8,  9,  1,  3,…
## $ nac_anio   <dbl> 1930, 1956, 1980, 1996, 1970, 1952, 1989, 1989, 1988, 2008,…
## $ l_nac_c    <dbl+lbl> 21, 13,  9,  9,  9,  9,  9,  9, 21, 21,  9,  9, 15, 12,…
## $ cs_p12     <dbl+lbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ cs_p13_1   <dbl+lbl> 6, 2, 3, 4, 3, 8, 4, 3, 4, 4, 4, 7, 7, 6, 3, 2, 5, 7, 7…
## $ cs_p13_2   <dbl+lbl> 3, 6, 3, 3, 3, 2, 3, 3, 3, 1, 1, 4, 4, 3, 3, 6, 4, 4, 5…
## $ cs_p14_c   <chr> " 21600", "", "", "", "", " 31200", "", "", "", "", "", " 3…
## $ cs_p15     <dbl+lbl>  1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,  3,  3,  2,…
## $ cs_p16     <dbl+lbl>  1, NA, NA, NA, NA,  1, NA, NA, NA, NA, NA,  1,  1,  1,…
## $ cs_p17     <dbl+lbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2…
## $ n_hij      <dbl+lbl>  7,  4, NA,  0, NA, NA, NA, NA,  5, NA,  0, NA,  0,  1,…
## $ e_con      <dbl+lbl> 4, 2, 5, 6, 5, 5, 6, 5, 5, 6, 6, 6, 5, 6, 2, 5, 5, 5, 6…
## $ cs_p20a_1  <dbl> 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ cs_p20a_c  <dbl> 9, 9, 9, 9, 9, 9, 9, 21, 21, 21, 9, 9, 9, 9, 9, 9, 9, 9, 9,…
## $ cs_p20b_1  <dbl> 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ cs_p20b_c  <dbl> 10, 13, 5, 7, 5, 14, 13, 71, 71, 71, 2, 14, 14, 11, 11, 13,…
## $ cs_p20c_1  <dbl> NA, NA, NA, NA, NA, NA, NA, 1, 3, 3, NA, NA, NA, NA, NA, NA…
## $ cs_ad_mot  <dbl+lbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ cs_p21_des <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",…
## $ cs_ad_des  <dbl+lbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ cs_nr_mot  <dbl+lbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ cs_p23_des <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",…
## $ cs_nr_ori  <dbl+lbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ ur         <dbl+lbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ zona       <dbl+lbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2…
## $ salario    <dbl> 6223, 6223, 6223, 6223, 6223, 6223, 6223, 6223, 6223, 6223,…
## $ fac_tri    <dbl> 1058, 1109, 907, 896, 419, 578, 687, 853, 853, 853, 1025, 2…
## $ fac_men    <dbl> 0, 0, 0, 0, 1154, 1945, 2312, 2568, 2568, 2568, 3080, 6477,…
## $ clase1     <dbl+lbl> 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 1, 1…
## $ clase2     <dbl> 4, 1, 1, 1, 1, 4, 1, 1, 4, 4, 4, 1, 4, 1, 1, 1, 4, 1, 1, 4,…
## $ clase3     <dbl+lbl> 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1…
## $ pos_ocu    <dbl+lbl> 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 3, 0, 1, 3, 3, 0, 3, 2…
## $ seg_soc    <dbl+lbl> 0, 1, 2, 1, 1, 0, 1, 2, 0, 0, 0, 2, 0, 1, 2, 2, 0, 2, 2…
## $ rama       <dbl+lbl> 0, 4, 3, 4, 4, 0, 4, 3, 0, 0, 0, 3, 0, 4, 4, 1, 0, 6, 4…
## $ c_ocu11c   <dbl+lbl>  0,  8,  6,  4,  5,  0,  5, 10,  0,  0,  0,  6,  0,  1,…
## $ ing7c      <dbl+lbl> 0, 2, 4, 2, 1, 0, 2, 2, 0, 0, 0, 2, 0, 2, 2, 2, 0, 7, 2…
## $ dur9c      <dbl+lbl> 0, 6, 6, 6, 6, 0, 7, 7, 0, 0, 0, 5, 0, 5, 8, 6, 0, 2, 7…
## $ emple7c    <dbl+lbl> 0, 5, 2, 7, 6, 0, 4, 2, 0, 0, 0, 1, 0, 6, 1, 1, 0, 1, 2…
## $ medica5c   <dbl+lbl> 0, 3, 4, 3, 3, 0, 3, 4, 0, 0, 0, 1, 0, 3, 1, 1, 0, 1, 1…
## $ buscar5c   <dbl+lbl> 0, 4, 4, 4, 4, 0, 4, 4, 0, 0, 0, 4, 0, 4, 4, 4, 0, 2, 4…
## $ rama_est1  <dbl+lbl> 0, 3, 3, 3, 3, 0, 3, 3, 0, 0, 0, 3, 0, 3, 3, 2, 0, 1, 3…
## $ rama_est2  <dbl+lbl>  0, 10,  5,  8, 11,  0,  8,  5,  0,  0,  0,  5,  0, 11,…
## $ dur_est    <dbl+lbl> 0, 4, 4, 4, 4, 0, 5, 5, 0, 0, 0, 4, 0, 4, 5, 4, 0, 2, 5…
## $ ambito1    <dbl+lbl> 0, 2, 2, 3, 2, 0, 2, 2, 0, 0, 0, 2, 0, 2, 2, 2, 0, 1, 2…
## $ ambito2    <dbl+lbl> 0, 4, 3, 0, 7, 0, 4, 3, 0, 0, 0, 2, 0, 7, 2, 2, 0, 0, 3…
## $ tue1       <dbl+lbl> 0, 2, 1, 1, 2, 0, 1, 3, 0, 0, 0, 3, 0, 2, 1, 3, 0, 1, 1…
## $ tue2       <dbl+lbl> 0, 3, 2, 1, 4, 0, 1, 5, 0, 0, 0, 5, 0, 4, 2, 5, 0, 2, 1…
## $ tue3       <dbl+lbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0…
## $ busqueda   <dbl+lbl> 0, 2, 2, 2, 2, 0, 2, 2, 0, 0, 0, 2, 0, 2, 2, 2, 0, 2, 2…
## $ d_ant_lab  <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ d_cexp_est <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ dur_des    <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ sub_o      <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0…
## $ s_clasifi  <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0…
## $ remune2c   <dbl+lbl> 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0…
## $ pre_asa    <dbl+lbl> 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0…
## $ tip_con    <dbl+lbl> 0, 3, 5, 3, 3, 0, 2, 5, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0…
## $ dispo      <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ nodispo    <dbl+lbl> 3, 0, 0, 0, 0, 3, 0, 0, 3, 3, 3, 0, 3, 0, 0, 0, 3, 0, 0…
## $ c_inac5c   <dbl+lbl> 5, 0, 0, 0, 0, 3, 0, 0, 2, 1, 1, 0, 3, 0, 0, 0, 3, 0, 0…
## $ pnea_est   <dbl+lbl> 6, 0, 0, 0, 0, 4, 0, 0, 4, 4, 4, 0, 4, 0, 0, 0, 4, 0, 0…
## $ niv_ins    <dbl+lbl> 2, 2, 3, 4, 3, 4, 4, 3, 4, 3, 3, 4, 4, 3, 3, 2, 3, 4, 4…
## $ eda5c      <dbl+lbl> 4, 4, 2, 2, 3, 4, 2, 2, 2, 1, 1, 3, 4, 3, 2, 3, 4, 3, 2…
## $ eda7c      <dbl+lbl> 6, 6, 4, 2, 5, 6, 3, 3, 3, 1, 1, 4, 6, 6, 3, 4, 6, 4, 3…
## $ eda12c     <dbl+lbl> 11, 11,  6,  3,  8, 11,  4,  4,  5,  1,  1,  7, 11, 10,…
## $ eda19c     <dbl+lbl> 18, 16, 11,  8, 13, 17,  9,  9, 10,  6,  6, 12, 16, 15,…
## $ hij5c      <dbl+lbl> 4, 3, 0, 1, 0, 0, 0, 0, 3, 0, 1, 0, 1, 2, 0, 0, 3, 0, 1…
## $ domestico  <dbl+lbl> 6, 3, 4, 3, 3, 8, 4, 4, 8, 7, 7, 3, 8, 3, 3, 3, 9, 3, 3…
## $ anios_esc  <dbl> 9, 6, 9, 12, 9, 18, 12, 9, 12, 10, 10, 16, 16, 12, 9, 6, 13…
## $ hrsocup    <dbl> 0, 45, 48, 44, 40, 0, 54, 56, 0, 0, 0, 36, 0, 35, 70, 40, 0…
## $ ingocup    <dbl> 0, 0, 20000, 0, 6000, 0, 12000, 12040, 0, 0, 0, 8600, 0, 10…
## $ ing_x_hrs  <dbl> 0.00000, 0.00000, 96.89922, 0.00000, 34.88372, 0.00000, 51.…
## $ tpg_p8a    <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0…
## $ tcco       <dbl+lbl> 0, 0, 0, 0, 2, 0, 3, 3, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 3…
## $ cp_anoc    <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0…
## $ imssissste <dbl+lbl> 0, 1, 4, 1, 2, 0, 1, 4, 0, 0, 0, 4, 0, 2, 4, 4, 0, 4, 4…
## $ ma48me1sm  <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ p14apoyos  <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ scian      <dbl+lbl>  0, 19,  7, 14, 20,  0, 14,  7,  0,  0,  0,  7,  0, 20,…
## $ t_tra      <dbl+lbl> 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ emp_ppal   <dbl+lbl> 0, 2, 1, 2, 2, 0, 2, 1, 0, 0, 0, 1, 0, 2, 2, 1, 0, 1, 2…
## $ tue_ppal   <dbl+lbl> 0, 2, 2, 2, 2, 0, 2, 1, 0, 0, 0, 1, 0, 2, 2, 1, 0, 2, 2…
## $ trans_ppal <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ mh_fil2    <dbl+lbl> 0, 3, 3, 3, 3, 0, 3, 1, 0, 0, 0, 1, 0, 3, 3, 1, 0, 4, 3…
## $ mh_col     <dbl+lbl> 0, 2, 1, 2, 2, 0, 2, 1, 0, 0, 0, 7, 0, 2, 8, 7, 0, 7, 6…
## $ sec_ins    <dbl+lbl> 0, 6, 4, 2, 5, 0, 2, 8, 0, 0, 0, 8, 0, 5, 4, 8, 0, 3, 2…
## $ tipo       <dbl+lbl> 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1…
## $ mes_cal    <dbl+lbl> 99, 99, 99, 99, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,…
## $ f          <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,…
summary(enoe)
##      r_def        loc              mun              est          est_d_tri    
##  Min.   :0   Min.   : NA      Min.   :  1.00   Min.   :10.00   Min.   :  1.0  
##  1st Qu.:0   1st Qu.: NA      1st Qu.:  6.00   1st Qu.:20.00   1st Qu.:111.0  
##  Median :0   Median : NA      Median : 22.00   Median :20.00   Median :237.0  
##  Mean   :0   Mean   :NaN      Mean   : 40.01   Mean   :23.63   Mean   :234.2  
##  3rd Qu.:0   3rd Qu.: NA      3rd Qu.: 49.00   3rd Qu.:30.00   3rd Qu.:359.0  
##  Max.   :0   Max.   : NA      Max.   :570.00   Max.   :40.00   Max.   :476.0  
##              NA's   :323118   NA's   :2515                                    
##    est_d_men          ageb     t_loc_tri       t_loc_men          cd_a      
##  Min.   :  1.0   Min.   :0   Min.   :1.000   Min.   :1.000   Min.   : 1.00  
##  1st Qu.: 99.0   1st Qu.:0   1st Qu.:1.000   1st Qu.:1.000   1st Qu.:15.00  
##  Median :210.0   Median :0   Median :1.000   Median :1.000   Median :33.00  
##  Mean   :210.7   Mean   :0   Mean   :1.818   Mean   :1.852   Mean   :43.71  
##  3rd Qu.:323.0   3rd Qu.:0   3rd Qu.:3.000   3rd Qu.:3.000   3rd Qu.:83.00  
##  Max.   :430.0   Max.   :0   Max.   :4.000   Max.   :4.000   Max.   :86.00  
##  NA's   :16327                               NA's   :16327                  
##       ent             con             upm              d_sem      
##  Min.   : 1.00   Min.   :   13   Min.   : 100005   Min.   :101.0  
##  1st Qu.: 8.00   1st Qu.:40216   1st Qu.: 802719   1st Qu.:202.0  
##  Median :16.00   Median :40576   Median :1605275   Median :304.0  
##  Mean   :16.38   Mean   :45195   Mean   :1650335   Mean   :308.1  
##  3rd Qu.:25.00   3rd Qu.:50037   3rd Qu.:2501131   3rd Qu.:409.0  
##  Max.   :32.00   Max.   :90024   Max.   :3260800   Max.   :513.0  
##                                                                   
##    n_pro_viv          v_sel            n_hog           h_mud       
##  Min.   :   1.0   Min.   : 1.000   Min.   :1.000   Min.   :0.0000  
##  1st Qu.:  38.0   1st Qu.: 2.000   1st Qu.:1.000   1st Qu.:0.0000  
##  Median :  76.0   Median : 4.000   Median :1.000   Median :0.0000  
##  Mean   :  93.3   Mean   : 5.345   Mean   :1.005   Mean   :0.0273  
##  3rd Qu.: 120.0   3rd Qu.: 5.000   3rd Qu.:1.000   3rd Qu.:0.0000  
##  Max.   :1285.0   Max.   :40.000   Max.   :5.000   Max.   :4.0000  
##                                                                    
##      n_ent           per          n_ren            c_res           par_c      
##  Min.   :1.00   Min.   :423   Min.   : 1.000   Min.   :1.000   Min.   :101.0  
##  1st Qu.:2.00   1st Qu.:423   1st Qu.: 1.000   1st Qu.:1.000   1st Qu.:101.0  
##  Median :3.00   Median :423   Median : 2.000   Median :1.000   Median :201.0  
##  Mean   :2.97   Mean   :423   Mean   : 2.184   Mean   :1.022   Mean   :213.8  
##  3rd Qu.:4.00   3rd Qu.:423   3rd Qu.: 3.000   3rd Qu.:1.000   3rd Qu.:301.0  
##  Max.   :5.00   Max.   :423   Max.   :20.000   Max.   :3.000   Max.   :999.0  
##                                                                               
##       sex            eda           nac_dia         nac_mes        nac_anio   
##  Min.   :1.00   Min.   :15.00   Min.   : 1.00   Min.   : 1.0   Min.   :1905  
##  1st Qu.:1.00   1st Qu.:26.00   1st Qu.: 9.00   1st Qu.: 4.0   1st Qu.:1967  
##  Median :2.00   Median :40.00   Median :17.00   Median : 7.0   Median :1983  
##  Mean   :1.53   Mean   :42.15   Mean   :22.91   Mean   :13.5   Mean   :1986  
##  3rd Qu.:2.00   3rd Qu.:55.00   3rd Qu.:25.00   3rd Qu.:10.0   3rd Qu.:1996  
##  Max.   :2.00   Max.   :98.00   Max.   :99.00   Max.   :99.0   Max.   :9999  
##                                                                              
##     l_nac_c           cs_p12         cs_p13_1         cs_p13_2    
##  Min.   :  1.00   Min.   :1.000   Min.   : 0.000   Min.   :0.000  
##  1st Qu.:  9.00   1st Qu.:1.000   1st Qu.: 3.000   1st Qu.:3.000  
##  Median : 16.00   Median :1.000   Median : 4.000   Median :3.000  
##  Mean   : 18.73   Mean   :1.034   Mean   : 4.108   Mean   :3.313  
##  3rd Qu.: 25.00   3rd Qu.:1.000   3rd Qu.: 6.000   3rd Qu.:4.000  
##  Max.   :999.00   Max.   :9.000   Max.   :99.000   Max.   :9.000  
##                                                                   
##    cs_p14_c             cs_p15           cs_p16           cs_p17    
##  Length:323118      Min.   :1.00     Min.   :1.00     Min.   :1.00  
##  Class :character   1st Qu.:3.00     1st Qu.:1.00     1st Qu.:2.00  
##  Mode  :character   Median :3.00     Median :1.00     Median :2.00  
##                     Mean   :2.86     Mean   :1.25     Mean   :1.87  
##                     3rd Qu.:3.00     3rd Qu.:1.00     3rd Qu.:2.00  
##                     Max.   :9.00     Max.   :9.00     Max.   :9.00  
##                     NA's   :240525   NA's   :234353                 
##      n_hij            e_con        cs_p20a_1       cs_p20a_c     
##  Min.   : 0.00    Min.   :1.00   Min.   :1.000   Min.   :  1.00  
##  1st Qu.: 0.00    1st Qu.:4.00   1st Qu.:1.000   1st Qu.:  8.00  
##  Median : 2.00    Median :5.00   Median :1.000   Median : 16.00  
##  Mean   : 2.13    Mean   :4.41   Mean   :1.009   Mean   : 16.65  
##  3rd Qu.: 3.00    3rd Qu.:6.00   3rd Qu.:1.000   3rd Qu.: 25.00  
##  Max.   :99.00    Max.   :9.00   Max.   :4.000   Max.   :999.00  
##  NA's   :151713                                                  
##    cs_p20b_1      cs_p20b_c        cs_p20c_1        cs_ad_mot     
##  Min.   :1.00   Min.   :  1.00   Min.   : 1.0     Min.   : NA     
##  1st Qu.:1.00   1st Qu.:  6.00   1st Qu.: 1.0     1st Qu.: NA     
##  Median :1.00   Median : 22.00   Median : 3.0     Median : NA     
##  Mean   :1.01   Mean   : 42.56   Mean   : 3.7     Mean   :NaN     
##  3rd Qu.:1.00   3rd Qu.: 50.00   3rd Qu.: 4.2     3rd Qu.: NA     
##  Max.   :2.00   Max.   :999.00   Max.   :11.0     Max.   : NA     
##  NA's   :351                     NA's   :319702   NA's   :323118  
##   cs_p21_des          cs_ad_des        cs_nr_mot       cs_p23_des       
##  Length:323118      Min.   : NA      Min.   : 1.0     Length:323118     
##  Class :character   1st Qu.: NA      1st Qu.: 4.0     Class :character  
##  Mode  :character   Median : NA      Median : 6.0     Mode  :character  
##                     Mean   :NaN      Mean   : 6.1                       
##                     3rd Qu.: NA      3rd Qu.: 9.0                       
##                     Max.   : NA      Max.   :99.0                       
##                     NA's   :323118   NA's   :319629                     
##    cs_nr_ori            ur             zona          salario    
##  Min.   :1.0      Min.   :1.000   Min.   :1.000   Min.   :6223  
##  1st Qu.:1.0      1st Qu.:1.000   1st Qu.:2.000   1st Qu.:6223  
##  Median :1.0      Median :1.000   Median :2.000   Median :6223  
##  Mean   :1.3      Mean   :1.362   Mean   :1.918   Mean   :6480  
##  3rd Qu.:1.0      3rd Qu.:2.000   3rd Qu.:2.000   3rd Qu.:6223  
##  Max.   :9.0      Max.   :2.000   Max.   :2.000   Max.   :9372  
##  NA's   :320552                                                 
##     fac_tri           fac_men            clase1          clase2     
##  Min.   :    9.0   Min.   :    0.0   Min.   :1.000   Min.   :1.000  
##  1st Qu.:  103.0   1st Qu.:  292.0   1st Qu.:1.000   1st Qu.:1.000  
##  Median :  171.0   Median :  525.0   Median :1.000   Median :1.000  
##  Mean   :  312.1   Mean   :  930.2   Mean   :1.388   Mean   :2.129  
##  3rd Qu.:  377.0   3rd Qu.: 1117.0   3rd Qu.:2.000   3rd Qu.:4.000  
##  Max.   :13108.0   Max.   :43369.0   Max.   :2.000   Max.   :4.000  
##                                                                     
##      clase3          pos_ocu          seg_soc            rama      
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.000  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.000  
##  Median :1.0000   Median :1.0000   Median :1.0000   Median :2.000  
##  Mean   :0.7389   Mean   :0.9322   Mean   :0.9404   Mean   :2.035  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:2.0000   3rd Qu.:4.000  
##  Max.   :7.0000   Max.   :4.0000   Max.   :3.0000   Max.   :7.000  
##                                                                    
##     c_ocu11c          ing7c           dur9c          emple7c     
##  Min.   : 0.000   Min.   :0.000   Min.   :0.000   Min.   :0.000  
##  1st Qu.: 0.000   1st Qu.:0.000   1st Qu.:0.000   1st Qu.:0.000  
##  Median : 2.000   Median :1.000   Median :3.000   Median :1.000  
##  Mean   : 3.225   Mean   :1.681   Mean   :3.317   Mean   :2.121  
##  3rd Qu.: 6.000   3rd Qu.:2.000   3rd Qu.:6.000   3rd Qu.:4.000  
##  Max.   :11.000   Max.   :7.000   Max.   :9.000   Max.   :7.000  
##                                                                  
##     medica5c        buscar5c       rama_est1       rama_est2     
##  Min.   :0.000   Min.   :0.000   Min.   :0.000   Min.   : 0.000  
##  1st Qu.:0.000   1st Qu.:0.000   1st Qu.:0.000   1st Qu.: 0.000  
##  Median :1.000   Median :4.000   Median :2.000   Median : 3.000  
##  Mean   :1.271   Mean   :2.342   Mean   :1.544   Mean   : 3.462  
##  3rd Qu.:3.000   3rd Qu.:4.000   3rd Qu.:3.000   3rd Qu.: 6.000  
##  Max.   :5.000   Max.   :5.000   Max.   :4.000   Max.   :11.000  
##                                                                  
##     dur_est         ambito1         ambito2           tue1      
##  Min.   :0.000   Min.   :0.000   Min.   :0.000   Min.   :0.000  
##  1st Qu.:0.000   1st Qu.:0.000   1st Qu.:0.000   1st Qu.:0.000  
##  Median :3.000   Median :2.000   Median :2.000   Median :1.000  
##  Mean   :2.331   Mean   :1.187   Mean   :2.036   Mean   :1.066  
##  3rd Qu.:4.000   3rd Qu.:2.000   3rd Qu.:4.000   3rd Qu.:2.000  
##  Max.   :6.000   Max.   :3.000   Max.   :8.000   Max.   :4.000  
##                                                                 
##       tue2            tue3            busqueda       d_ant_lab      
##  Min.   :0.000   Min.   :0.00000   Min.   :0.000   Min.   :0.00000  
##  1st Qu.:0.000   1st Qu.:0.00000   1st Qu.:0.000   1st Qu.:0.00000  
##  Median :1.000   Median :0.00000   Median :2.000   Median :0.00000  
##  Mean   :1.768   Mean   :0.07513   Mean   :1.187   Mean   :0.01836  
##  3rd Qu.:4.000   3rd Qu.:0.00000   3rd Qu.:2.000   3rd Qu.:0.00000  
##  Max.   :7.000   Max.   :2.00000   Max.   :2.000   Max.   :2.00000  
##                                                                     
##    d_cexp_est         dur_des            sub_o          s_clasifi    
##  Min.   :0.00000   Min.   :0.00000   Min.   :0.0000   Min.   :0.000  
##  1st Qu.:0.00000   1st Qu.:0.00000   1st Qu.:0.0000   1st Qu.:0.000  
##  Median :0.00000   Median :0.00000   Median :0.0000   Median :0.000  
##  Mean   :0.02477   Mean   :0.03342   Mean   :0.0447   Mean   :0.141  
##  3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.0000   3rd Qu.:0.000  
##  Max.   :4.00000   Max.   :6.00000   Max.   :1.0000   Max.   :6.000  
##                                                                      
##     remune2c         pre_asa          tip_con          dispo       
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.000   1st Qu.:0.0000  
##  Median :0.0000   Median :0.0000   Median :0.000   Median :0.0000  
##  Mean   :0.4392   Mean   :0.5518   Mean   :1.578   Mean   :0.2052  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:3.000   3rd Qu.:0.0000  
##  Max.   :2.0000   Max.   :3.0000   Max.   :6.000   Max.   :7.0000  
##                                                                    
##     nodispo         c_inac5c         pnea_est       niv_ins         eda5c      
##  Min.   :0.000   Min.   :0.0000   Min.   :0.00   Min.   :1.00   Min.   :1.000  
##  1st Qu.:0.000   1st Qu.:0.0000   1st Qu.:0.00   1st Qu.:2.00   1st Qu.:2.000  
##  Median :0.000   Median :0.0000   Median :0.00   Median :3.00   Median :2.000  
##  Mean   :1.192   Mean   :0.8664   Mean   :1.49   Mean   :3.06   Mean   :2.351  
##  3rd Qu.:3.000   3rd Qu.:2.0000   3rd Qu.:4.00   3rd Qu.:4.00   3rd Qu.:3.000  
##  Max.   :9.000   Max.   :5.0000   Max.   :6.00   Max.   :5.00   Max.   :5.000  
##                                                                                
##      eda7c           eda12c           eda19c          hij5c      
##  Min.   :1.000   Min.   : 1.000   Min.   : 6.00   Min.   :0.000  
##  1st Qu.:2.000   1st Qu.: 3.000   1st Qu.: 8.00   1st Qu.:0.000  
##  Median :4.000   Median : 6.000   Median :11.00   Median :1.000  
##  Mean   :3.643   Mean   : 5.859   Mean   :10.99   Mean   :1.136  
##  3rd Qu.:5.000   3rd Qu.: 9.000   3rd Qu.:14.00   3rd Qu.:2.000  
##  Max.   :7.000   Max.   :12.000   Max.   :19.00   Max.   :5.000  
##                                                                  
##    domestico        anios_esc        hrsocup          ingocup      
##  Min.   : 1.000   Min.   : 0.00   Min.   :  0.00   Min.   :     0  
##  1st Qu.: 3.000   1st Qu.: 8.00   1st Qu.:  0.00   1st Qu.:     0  
##  Median : 3.000   Median :10.00   Median : 24.00   Median :     0  
##  Mean   : 4.706   Mean   :10.44   Mean   : 24.47   Mean   :  3760  
##  3rd Qu.: 8.000   3rd Qu.:12.00   3rd Qu.: 48.00   3rd Qu.:  6450  
##  Max.   :10.000   Max.   :99.00   Max.   :168.00   Max.   :208000  
##                                                                    
##    ing_x_hrs          tpg_p8a             tcco           cp_anoc     
##  Min.   :   0.00   Min.   :0.00000   Min.   :0.0000   Min.   :0.000  
##  1st Qu.:   0.00   1st Qu.:0.00000   1st Qu.:0.0000   1st Qu.:0.000  
##  Median :   0.00   Median :0.00000   Median :0.0000   Median :0.000  
##  Mean   :  22.97   Mean   :0.02001   Mean   :0.3915   Mean   :0.107  
##  3rd Qu.:  35.56   3rd Qu.:0.00000   3rd Qu.:0.0000   3rd Qu.:0.000  
##  Max.   :6976.74   Max.   :1.00000   Max.   :3.0000   Max.   :1.000  
##                                                                      
##    imssissste      ma48me1sm         p14apoyos     scian            t_tra      
##  Min.   :0.000   Min.   :0.00000   Min.   :0   Min.   : 0.000   Min.   :1.000  
##  1st Qu.:0.000   1st Qu.:0.00000   1st Qu.:0   1st Qu.: 0.000   1st Qu.:1.000  
##  Median :1.000   Median :0.00000   Median :0   Median : 4.000   Median :1.000  
##  Mean   :1.666   Mean   :0.02761   Mean   :0   Mean   : 5.988   Mean   :1.037  
##  3rd Qu.:4.000   3rd Qu.:0.00000   3rd Qu.:0   3rd Qu.: 9.000   3rd Qu.:1.000  
##  Max.   :5.000   Max.   :1.00000   Max.   :0   Max.   :21.000   Max.   :2.000  
##                                                                                
##     emp_ppal         tue_ppal       trans_ppal          mh_fil2     
##  Min.   :0.0000   Min.   :0.000   Min.   :0.000000   Min.   :0.000  
##  1st Qu.:0.0000   1st Qu.:0.000   1st Qu.:0.000000   1st Qu.:0.000  
##  Median :1.0000   Median :1.000   Median :0.000000   Median :1.000  
##  Mean   :0.8892   Mean   :1.035   Mean   :0.002182   Mean   :1.497  
##  3rd Qu.:2.0000   3rd Qu.:2.000   3rd Qu.:0.000000   3rd Qu.:3.000  
##  Max.   :2.0000   Max.   :2.000   Max.   :1.000000   Max.   :4.000  
##                                                                     
##      mh_col         sec_ins            tipo         mes_cal         f          
##  Min.   :0.000   Min.   : 0.000   Min.   :1.00   Min.   :10.00   Mode:logical  
##  1st Qu.:0.000   1st Qu.: 0.000   1st Qu.:1.00   1st Qu.:10.00   TRUE:323118   
##  Median :1.000   Median : 2.000   Median :1.00   Median :11.00                 
##  Mean   :1.946   Mean   : 2.989   Mean   :1.09   Mean   :15.45                 
##  3rd Qu.:2.000   3rd Qu.: 5.000   3rd Qu.:1.00   3rd Qu.:12.00                 
##  Max.   :9.000   Max.   :11.000   Max.   :2.00   Max.   :99.00                 
## 
# Gráfico = Datos + Estética + Geometría.

ggplot(ocupados) + aes(sex) + geom_bar(fill="yellowgreen")

ggplot(ocupados) + aes(e_con) + geom_bar(fill="orange")

# Gráfico = Datos + Estética + Geometría + Etiquetas

ggplot(ocupados) + aes(sex) + geom_bar(fill="yellowgreen") + 
  xlab("Sexo") + ylab("Personas") + ggtitle("Total de mujeres y hombres ocupados")

ggplot(ocupados) + aes(e_con) + geom_bar(fill="orange") + 
  xlab("Estado conyugal") + ylab("Personas") + ggtitle("Estado conyugal de las personas ocupadas")

# Gráfico = Datos + Estética + Geometría + Etiquetas + Ajustes


ggplot(ocupados) + aes(sex) + geom_bar(fill="yellowgreen") + 
  xlab("Sexo") + ylab("Personas") + ggtitle("Total de mujeres y hombres")+ 
  geom_text(aes(label = scales::comma(..count..)), stat = 'count',                 # Añade el número con comas
            position = position_dodge(0.5), vjust = -0.5, size = 3.5) +            # Ajusta la posición del texto moviéndolo ligeramente y ajustando su posición vertical
  theme_minimal(base_size = 14) +                                                  # Ajusta la configuración del gráfico
  theme(plot.title = element_text(hjust = 0.5))                                    # Centrar el título horizontalmente

tabla1 <- ocupados %>%count(e_con)

ggplot(tabla1, 
       aes(x = reorder(e_con, -n), y = n)) + 
  geom_bar(fill="orange",stat="identity") +
  geom_text(aes(label =n, vjust=-0.5))+
  labs(x = "Estado conyugal", 
       y = "Personas", 
       title  = "Total de personas ocupadas por estado conyugal")+
  theme_minimal()+theme(plot.title = element_text(hjust = 0.5))

ggplot(ocupados, aes(x = sex, y = ..prop.. * 100, group = 1)) +
  geom_bar(aes(fill = sex), position = "dodge", stat = "count") +                            # Crea barras con porcentajes
  geom_text(aes(label = scales::percent(..prop..)), stat = 'count',                          # Ajusta el número mostrado arriba de las barras
            vjust = -0.5, position = position_dodge(0.5), size = 3.5) +
  labs(x = "Sexo", y = "Porcentaje", title = "Porcentaje de mujeres y hombres", 
       caption = "Nota: población de 15 años y más\nFuente: ENOE cuarto trimestre 2023") +  # Añade la fuente y nota como pie de página
  theme_minimal(base_size = 14) +
  theme(plot.title = element_text(hjust = 0.5),                                              # Centrar el título horizontalmente
        plot.caption = element_text(hjust = 0, size = 9)) +                                  # Ajustes de estilo para la nota de fuente y la nota explicativa
  scale_y_continuous(labels = scales::percent_format(scale = 1))                             # Formatear el eje y como porcentaje

#4.2 Con 2 variables discretas


# Ahora vamos a guardar la definición de la base de datos y la variable en un elemento llamado g1
g1<-ggplot(ocupados) + aes(pos_ocu)            # Base de datos y variable


# Ya no bamos a repetir la definición, solo vamos a usar g1


# Opción 1: Pantalla dividida
g1+geom_bar(fill="yellowgreen")+
  facet_wrap(~ sex)+                          # facet_wrap divide a la pantalla
  ggtitle("Ocupacion por sexo")+
  xlab("Posicion de la ocupacion")+
  ylab("Personas")

# agregar más detalles

g1+geom_bar(fill="yellowgreen")+
  facet_wrap(~ sex)+                          # facet_wrap divide a la pantalla
  ggtitle("Ocupacion por sexo")+
  xlab("Posicion de la ocupacion")+
  ylab("Personas")+
  geom_text(aes(label=..count..), stat='count',  
            position=position_dodge(0.5), 
            vjust=-0.5, 
            size=3.5) 

# Opción 2: Separa las barras

g2<-ggplot(ocupados,aes(x=pos_ocu,fill=sex))   # Base de datos y variable

g2+geom_bar(position = "dodge")+
  ggtitle("Ocupacion por sexo")+              # position ajusta la posicion de las barras
  xlab("Posicion de la ocupacion")+
  ylab("Personas")+
  scale_fill_brewer(palette = "Blues")+
  scale_y_continuous(labels = scales::comma)

# agregar más detalles

g2+geom_bar(position = "dodge", stat = "count") +  # Barras con posición dodge y contando las ocurrencias
  geom_text(aes(label = scales::comma(..count..)), stat = "count", position = position_dodge(width = 0.9), vjust = -0.5, size = 2.5) +  # Añade etiquetas numeradas con comas
  labs(x="Posición de la Ocupación",y="Personas",title="Ocupación por Sexo",caption = "Nota: población de 15 años y más\nFuente: ENOE cuarto trimestre 2023")+theme_minimal(base_size = 12) +
  theme(plot.title = element_text(hjust = 0.5),                                              # Centrar el título horizontalmente
        plot.caption = element_text(hjust = 0, size = 9)) + 
  scale_fill_brewer(palette = "Reds",name = "Sexo") +  # Define la paleta de colores y titula la leyenda como "Sexo"
  scale_y_continuous(labels = scales::comma)

ggplot(ocupados, aes(x = sex, y = ..prop.. * 100, group = 1)) +
  geom_bar(aes(fill = sex), position = "dodge", stat = "count") +                            # Crea barras con porcentajes
  geom_text(aes(label = scales::percent(..prop..)), stat = 'count',                          # Ajusta el número mostrado arriba de las barras
            vjust = -0.5, position = position_dodge(0.5), size = 3.5) +
  labs(x = "Sexo", y = "Porcentaje", title = "Porcentaje de mujeres y hombres", 
       caption = "Nota: población de 15 años y más\nFuente: ENOE cuarto trimestre 2023") +  # Añade la fuente y nota como pie de página
  theme_minimal(base_size = 14) +
  theme(plot.title = element_text(hjust = 0.5),                                              # Centrar el título horizontalmente
        plot.caption = element_text(hjust = 0, size = 9)) +                                  # Ajustes de estilo para la nota de fuente y la nota explicativa
  scale_y_continuous(labels = scales::percent_format(scale = 1))     

# Opción 3: Apila los elementos uno encima del otro


# Preparación de los datos para calcular los porcentajes correctos
ocupados_r <- ocupados %>%
  group_by(pos_ocu, sex) %>%
  summarise(count = n(), .groups = 'drop') %>%
  group_by(pos_ocu) %>%
  mutate(percent = count / sum(count) * 100)

# Creación del gráfico con los datos preparados
g3 <- ggplot(ocupados_r, aes(x = pos_ocu, y = percent, fill = sex)) 

g3+geom_bar(stat = "identity", position = "dodge") +  # Usar barras con posición dodge
  geom_text(aes(label = scales::percent(percent / 100)), position = position_dodge(width = 0.9), vjust = -0.5, size = 2.5) +
  ggtitle("Ocupación por Sexo") +
  xlab("Posición de la Ocupación") +
  ylab("Porcentaje") +
  scale_fill_brewer(palette = "Blues", name = "Sexo") +
  scale_y_continuous(labels = percent_format(scale = 1))

# Creación del gráfico con las cifras divididas entre 100
g3+geom_bar(stat = "identity", position = "fill") +  # Usar barras con posición fill para apilar los porcentajes en una barra
  geom_text(aes(label = scales::percent(percent / 100, accuracy = 0.1)), position = position_fill(vjust = 0.5), size = 3.5) +
  ggtitle("Ocupación por Sexo") +
  xlab("Posición de la Ocupación") +
  ylab("Porcentaje de Personas") +
  scale_fill_brewer(palette = "Reds", name = "Sexo") +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +  # Formatear el eje Y como porcentaje
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))

ocupados_r <- ocupados %>%
  group_by(e_con, sex) %>%
  summarise(count = n(), .groups = 'drop') %>%
  group_by(e_con) %>%
  mutate(percent = count / sum(count) * 100)

g4 <- ggplot(ocupados_r, aes(x = e_con, y = percent, fill = sex)) 

g4+geom_bar(stat = "identity", position = "dodge") +  # Usar barras con posición dodge
  geom_text(aes(label = scales::percent(percent / 100)), position = position_dodge(width = 0.9), vjust = -0.5, size = 2.5) +
  ggtitle("Ocupación por Sexo") +
  xlab("Posición de la Ocupación") +
  ylab("Porcentaje") +
  scale_fill_brewer(palette = "Blues", name = "Sexo") +
  scale_y_continuous(labels = percent_format(scale = 1))

# Creación del gráfico con las cifras divididas entre 100
g4+geom_bar(stat = "identity", position = "fill") +  # Usar barras con posición fill para apilar los porcentajes en una barra
  geom_text(aes(label = scales::percent(percent / 100, accuracy = 0.1)), position = position_fill(vjust = 0.5), size = 3.5) +
  ggtitle("Estado conyugal por sexo") +
  xlab("Estado conyugal") +
  ylab("Porcentaje de personas") +
  scale_fill_brewer(palette = "Reds", name = "Sexo") +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +  # Formatear el eje Y como porcentaje
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))

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

Including Plots

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.