Atividade: Análise de Dados PNAD COVID-19

  1. Aprendemos, através de vários exemplos, como podemos explorar o conjunto de dados da PNAD COVID-19 disponibilizado pelo IBGE. Agora é a sua vez. Nessa atividade, você deverá usar o mesmo banco de dados para avaliar questões distintas das que foram propostas na aula. Busque analisar outras variáveis, fazer uma análise por região, estado, grupo de municı́pios, etc. Você também pode melhorar a apresentação dos gráficos disponibilizados na aula. As possibilidades são imensas e você deverá explorá-las. Coloque em prática todas as ferramentas que aprendemos anteriormente. Quanto maior o número de elementos utilizados, maior será a sua nota. Utilize toda a sua criatividade e conhecimento. Bom trabalho!

Resp:

Nesta atividade utilizaremos o conjunto de dados da PNAD COVID-19, disponibilizado pelo IBGE em novembro de 2020.

Descrição da base de dados

Iniciamos carregando a base de dados (.csv) e o dicionário (.xlxs) que a descreve.

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
library(ggplot2)
library(readxl)
library(readr)
library(srvyr) # trabalha com amostras complexas
## 
## Attaching package: 'srvyr'
## 
## The following object is masked from 'package:stats':
## 
##     filter
#?srvyr

db_dict <- read_excel("Dicionario_PNAD_COVID_112020.xls",
                      sheet = "dicionário pnad covid")
## New names:
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
db_pnad <- read.csv("PNAD_COVID_112020.csv") # obs: ~115MB

head(db_pnad)
##    Ano UF CAPITAL RM_RIDE V1008 V1012 V1013 V1016 Estrato       UPA V1022 V1023
## 1 2020 11      11      NA     1     4    11     7 1110011 110015970     1     1
## 2 2020 11      11      NA     1     4    11     7 1110011 110015970     1     1
## 3 2020 11      11      NA     1     4    11     7 1110011 110015970     1     1
## 4 2020 11      11      NA     1     4    11     7 1110011 110015970     1     1
## 5 2020 11      11      NA     2     1    11     7 1110011 110015970     1     1
## 6 2020 11      11      NA     3     2    11     7 1110011 110015970     1     1
##    V1030    V1031    V1032 posest A001 A001A A001B1 A001B2 A001B3 A002 A003
## 1 152902 101.0117 117.4637   1114    1     1      5      8   1984   36    1
## 2 150835 101.0117 125.7512   1124    2     2     18      8   1990   30    2
## 3 144737 101.0117 131.3610   1112    3     4     25     12   2006   13    1
## 4 144737 101.0117 131.3610   1112    4     4     24      8   2009   11    1
## 5  92377 101.0117 100.7335   1126    1     1     18      5   1963   57    2
## 6  92377 101.0117 100.7335   1126    1     1     25     12   1961   58    2
##   A004 A005 A006 A006A A006B A007 A007A A008 A009 B0011 B0012 B0013 B0014 B0015
## 1    4    5   NA    NA    NA   NA    NA   NA   NA     2     2     2     2     2
## 2    4    7   NA    NA    NA   NA    NA   NA   NA     2     2     2     2     2
## 3    4    2    1     1     3    1    NA    5    3     2     2     2     2     2
## 4    4    2    1     1     3    1    NA    5    3     2     2     2     2     2
## 5    1    2   NA    NA    NA   NA    NA   NA   NA     2     2     2     2     2
## 6    2    1   NA    NA    NA   NA    NA   NA   NA     2     2     2     2     2
##   B0016 B0017 B0018 B0019 B00110 B00111 B00112 B00113 B002 B0031 B0032 B0033
## 1     2     2     2     2      2      2      2      2   NA    NA    NA    NA
## 2     2     2     2     2      2      2      2      2   NA    NA    NA    NA
## 3     2     2     2     2      2      2      2      2   NA    NA    NA    NA
## 4     2     2     2     2      2      2      2      2   NA    NA    NA    NA
## 5     2     2     2     2      2      2      2      2   NA    NA    NA    NA
## 6     2     2     2     2      2      2      2      2   NA    NA    NA    NA
##   B0034 B0035 B0036 B0037 B0041 B0042 B0043 B0044 B0045 B0046 B005 B006 B007
## 1    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA   NA   NA    1
## 2    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA   NA   NA    2
## 3    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA   NA   NA    1
## 4    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA   NA   NA    1
## 5    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA   NA   NA    2
## 6    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA   NA   NA    2
##   B008 B009A B009B B009C B009D B009E B009F B0101 B0102 B0103 B0104 B0105 B0106
## 1    2    NA    NA    NA    NA    NA    NA     2     2     2     2     2     2
## 2    2    NA    NA    NA    NA    NA    NA     2     2     2     2     2     2
## 3    2    NA    NA    NA    NA    NA    NA     2     2     2     2     2     2
## 4    2    NA    NA    NA    NA    NA    NA     2     2     2     2     2     2
## 5    2    NA    NA    NA    NA    NA    NA     2     2     2     2     2     2
## 6    2    NA    NA    NA    NA    NA    NA     1     1     2     2     2     2
##   B011 C001 C002 C003 C004 C005 C0051 C0052 C0053 C006 C007 C007A C007B C007C
## 1    2    1   NA   NA   NA   NA    NA    NA    NA    2    4    NA     1    35
## 2    2    1   NA   NA   NA   NA    NA    NA    NA    2    7    NA    NA    27
## 3    4   NA   NA   NA   NA   NA    NA    NA    NA   NA   NA    NA    NA    NA
## 4    4   NA   NA   NA   NA   NA    NA    NA    NA   NA   NA    NA    NA    NA
## 5    2    2    2   NA   NA   NA    NA    NA    NA   NA   NA    NA    NA    NA
## 6    3    1   NA   NA   NA   NA    NA    NA    NA    2    7    NA    NA    21
##   C007D C007E C007E1 C007E2 C007F C008 C009 C009A C010 C0101 C01011 C01012
## 1     6    NA     NA     NA    NA   48   48    NA    1     1      4   1045
## 2    20    NA     NA     NA    NA   36   36     2    1     1      4   1045
## 3    NA    NA     NA     NA    NA   NA   NA    NA   NA    NA     NA     NA
## 4    NA    NA     NA     NA    NA   NA   NA    NA   NA    NA     NA     NA
## 5    NA    NA     NA     NA    NA   NA   NA    NA   NA    NA     NA     NA
## 6    25    NA     NA     NA    NA   30   30     2    1     1      1    200
##   C0102 C01021 C01022 C0103 C0104 C011A C011A1 C011A11 C011A12 C011A2 C011A21
## 1    NA     NA     NA    NA    NA     1      1       4    1045     NA      NA
## 2    NA     NA     NA    NA    NA     1      1       4    1045     NA      NA
## 3    NA     NA     NA    NA    NA    NA     NA      NA      NA     NA      NA
## 4    NA     NA     NA    NA    NA    NA     NA      NA      NA     NA      NA
## 5    NA     NA     NA    NA    NA    NA     NA      NA      NA     NA      NA
## 6    NA     NA     NA    NA    NA     1      1       0     100     NA      NA
##   C011A22 C012 C013 C014 C015 C016 C017A D0011 D0013 D0021 D0023 D0031 D0033
## 1      NA    1   NA   NA   NA   NA    NA     2    NA     2    NA     2    NA
## 2      NA    1   NA    2   NA   NA    NA     2    NA     2    NA     2    NA
## 3      NA   NA   NA   NA   NA   NA    NA     2    NA     2    NA     2    NA
## 4      NA   NA   NA   NA   NA   NA    NA     2    NA     2    NA     2    NA
## 5      NA   NA   NA    2    2    1     1     2    NA     2    NA     2    NA
## 6      NA    1   NA    2   NA   NA    NA     1  1045     2    NA     2    NA
##   D0041 D0043 D0051 D0053 D0061 D0063 D0071 D0073 E001 E0021 E0022 E0023 E0024
## 1     2    NA     1   600     2    NA     2    NA    3    NA    NA    NA    NA
## 2     2    NA     1   600     2    NA     2    NA    3    NA    NA    NA    NA
## 3     2    NA     1   600     2    NA     2    NA    3    NA    NA    NA    NA
## 4     2    NA     1   600     2    NA     2    NA    3    NA    NA    NA    NA
## 5     1  1045     2    NA     2    NA     2    NA    3    NA    NA    NA    NA
## 6     2    NA     2    NA     2    NA     2    NA    3    NA    NA    NA    NA
##   F001 F0021 F0022 F002A1 F002A2 F002A3 F002A4 F002A5 F0061 F006
## 1    1    NA    NA      1      1      1      2      1     1    1
## 2    1    NA    NA      1      1      1      2      1     1    1
## 3    1    NA    NA      1      1      1      2      1     1    1
## 4    1    NA    NA      1      1      1      2      1     1    1
## 5    1    NA    NA      1      1      1      2      1     1    1
## 6    1    NA    NA      1      1      1      2      1     1    1

Vejamos a estrutura desta base de dados:

dim(db_pnad)
## [1] 381438    148
str(db_pnad)
## 'data.frame':    381438 obs. of  148 variables:
##  $ Ano    : int  2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 ...
##  $ UF     : int  11 11 11 11 11 11 11 11 11 11 ...
##  $ CAPITAL: int  11 11 11 11 11 11 11 11 11 11 ...
##  $ RM_RIDE: int  NA NA NA NA NA NA NA NA NA NA ...
##  $ V1008  : int  1 1 1 1 2 3 4 4 4 4 ...
##  $ V1012  : int  4 4 4 4 1 2 4 4 4 4 ...
##  $ V1013  : int  11 11 11 11 11 11 11 11 11 11 ...
##  $ V1016  : int  7 7 7 7 7 7 7 7 7 7 ...
##  $ Estrato: int  1110011 1110011 1110011 1110011 1110011 1110011 1110011 1110011 1110011 1110011 ...
##  $ UPA    : int  110015970 110015970 110015970 110015970 110015970 110015970 110015970 110015970 110015970 110015970 ...
##  $ V1022  : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ V1023  : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ V1030  : int  152902 150835 144737 144737 92377 92377 123734 138562 144737 134209 ...
##  $ V1031  : num  101 101 101 101 101 ...
##  $ V1032  : num  117 126 131 131 101 ...
##  $ posest : int  1114 1124 1112 1112 1126 1126 1115 1122 1112 1121 ...
##  $ A001   : int  1 2 3 4 1 1 1 4 7 9 ...
##  $ A001A  : int  1 2 4 4 1 1 1 5 5 5 ...
##  $ A001B1 : int  5 18 25 24 18 25 18 99 99 99 ...
##  $ A001B2 : int  8 8 12 8 5 12 2 99 99 99 ...
##  $ A001B3 : int  1984 1990 2006 2009 1963 1961 1973 9999 9999 9999 ...
##  $ A002   : int  36 30 13 11 57 58 47 18 11 9 ...
##  $ A003   : int  1 2 1 1 2 2 1 2 1 2 ...
##  $ A004   : int  4 4 4 4 1 2 4 4 4 4 ...
##  $ A005   : int  5 7 2 2 2 1 2 2 2 2 ...
##  $ A006   : int  NA NA 1 1 NA NA NA 2 1 1 ...
##  $ A006A  : int  NA NA 1 1 NA NA NA NA 1 1 ...
##  $ A006B  : int  NA NA 3 3 NA NA NA NA 3 3 ...
##  $ A007   : int  NA NA 1 1 NA NA NA NA 3 3 ...
##  $ A007A  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ A008   : int  NA NA 5 5 NA NA NA NA NA NA ...
##  $ A009   : int  NA NA 3 3 NA NA NA NA NA NA ...
##  $ B0011  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0012  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0013  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0014  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0015  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0016  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0017  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0018  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0019  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B00110 : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B00111 : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B00112 : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B00113 : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B002   : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0031  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0032  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0033  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0034  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0035  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0036  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0037  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0041  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0042  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0043  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0044  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0045  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0046  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B005   : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B006   : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B007   : int  1 2 1 1 2 2 2 2 2 2 ...
##  $ B008   : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B009A  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B009B  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B009C  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B009D  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B009E  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B009F  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ B0101  : int  2 2 2 2 2 1 2 2 2 2 ...
##  $ B0102  : int  2 2 2 2 2 1 2 2 2 2 ...
##  $ B0103  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0104  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0105  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B0106  : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ B011   : int  2 2 4 4 2 3 2 3 4 4 ...
##  $ C001   : int  1 1 NA NA 2 1 1 2 NA NA ...
##  $ C002   : int  NA NA NA NA 2 NA NA 2 NA NA ...
##  $ C003   : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C004   : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C005   : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C0051  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C0052  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C0053  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C006   : int  2 2 NA NA NA 2 2 NA NA NA ...
##  $ C007   : int  4 7 NA NA NA 7 7 NA NA NA ...
##  $ C007A  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C007B  : int  1 NA NA NA NA NA NA NA NA NA ...
##  $ C007C  : int  35 27 NA NA NA 21 18 NA NA NA ...
##  $ C007D  : int  6 20 NA NA NA 25 5 NA NA NA ...
##  $ C007E  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C007E1 : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C007E2 : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C007F  : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ C008   : int  48 36 NA NA NA 30 40 NA NA NA ...
##  $ C009   : int  48 36 NA NA NA 30 20 NA NA NA ...
##  $ C009A  : int  NA 2 NA NA NA 2 1 NA NA NA ...
##  $ C010   : int  1 1 NA NA NA 1 1 NA NA NA ...
##  $ C0101  : int  1 1 NA NA NA 1 1 NA NA NA ...
##   [list output truncated]

Selecionando algumas informações de interesse

Agora iremos colocar os pesos e filtrar pela região de interesse, que é a capital de MG, a cidade de Belo Horizonte.

# Sobre srvyr:
# "There are three stages to using srvyr functions, creating a survey object, manipulating the data, and calculating survey statistics."
#?as_survey_design
#?as_survey_rep
#?as_survey_twophase


# coloca pesos e filtra pelo município de belo horizonte/mg
db_pesos <- db_pnad %>%
  as_survey_design(ids=UPA, strata=Estrato, weights=V1032, nest=TRUE) %>%
  filter(UF == "31")

db_pesos$variables
## # A tibble: 34,339 × 148
##      Ano    UF CAPITAL RM_RIDE V1008 V1012 V1013 V1016 Estrato       UPA V1022
##    <int> <int>   <int>   <int> <int> <int> <int> <int>   <int>     <int> <int>
##  1  2020    31      31      31     1     3    11     7 3110111 310140585     1
##  2  2020    31      31      31     1     3    11     7 3110111 310140585     1
##  3  2020    31      31      31     1     3    11     7 3110111 310140585     1
##  4  2020    31      31      31     2     1    11     7 3110111 310140585     1
##  5  2020    31      31      31     5     2    11     7 3110111 310140585     1
##  6  2020    31      31      31     5     2    11     7 3110111 310140585     1
##  7  2020    31      31      31     6     4    11     7 3110111 310140585     1
##  8  2020    31      31      31     6     4    11     7 3110111 310140585     1
##  9  2020    31      31      31     6     4    11     7 3110111 310140585     1
## 10  2020    31      31      31     6     4    11     7 3110111 310140585     1
## # ℹ 34,329 more rows
## # ℹ 137 more variables: V1023 <int>, V1030 <int>, V1031 <dbl>, V1032 <dbl>,
## #   posest <int>, A001 <int>, A001A <int>, A001B1 <int>, A001B2 <int>,
## #   A001B3 <int>, A002 <int>, A003 <int>, A004 <int>, A005 <int>, A006 <int>,
## #   A006A <int>, A006B <int>, A007 <int>, A007A <int>, A008 <int>, A009 <int>,
## #   B0011 <int>, B0012 <int>, B0013 <int>, B0014 <int>, B0015 <int>,
## #   B0016 <int>, B0017 <int>, B0018 <int>, B0019 <int>, B00110 <int>, …
#summary(db_pesos$variables$CAPITAL)
pnad_pesos <- db_pesos %>% 
  mutate(one = 1,
         Sexo = ifelse(A003 == 1, "Homem", "Mulher"),
         Idade = case_when(
           A002 %in% 15:24 ~ "15-24",
           A002 %in% 25:34 ~ "25-34", 
           A002 %in% 35:49 ~ "35-49", 
           A002 %in% 50:64 ~ "50-64", 
           A002 > 64 ~ "65+"),
         Cor = case_when(
           A004 == 1 ~ "Branca", 
           A004 == 2 ~ "Preta", 
           A004 == 4 ~ "Parda"),
         Escolaridade = factor(case_when(  ##usando factor conseguimos ordenar
           A005 %in% 1:2 ~ "Sem Instrução ou Fundamental Incompleto", 
           A005 %in% 3:4 ~ "Fundamental completo ou Médio Incompleto", 
           A005 %in% 5:6 ~ "Médio completo ou Superior Incompleto", 
           A005 == 7 ~ "Superior completo", 
           A005 == 8 ~ "Pós-graduação"), 
           levels = c("Sem Instrução ou Fundamental Incompleto",
                      "Fundamental completo ou Médio Incompleto", 
                      "Médio completo ou Superior Incompleto",
                      "Superior completo",
                      "Pós-graduação")), 
         Tipo_emprego = factor(case_when(
           C007 == 1 ~ "Trabalhador doméstico (empregado doméstico, cuidados, babá)",
           C007 == 2 ~ "Militar",
           C007 == 3 ~ "Policial ou Bombeiro",
           C007 == 4 ~ "Setor privado",
           C007 == 5 ~ "Setor público",
           C007 == 6 ~ "Empregador",
           C007 == 7 ~ "Autônomo (Conta própria)"),
           levels = c( "Trabalhador doméstico (empregado doméstico, cuidados, babá)",
               "Militar", 
               "Policial ou Bombeiro",
               "Setor privado",
               "Setor público",
               "Empregador",
               "Autônomo (Conta própria)")), 
         Faixa_salario = factor(case_when(
           C01012 <= 1044 ~ "Menos de um salário mínimo",
           C01012 %in% c(1045:2090) ~ "Entre 1 e 2",
           C01012 %in% c(2091:3135) ~ "Entre 2 e 3",
           C01012 %in% c(3136:4180) ~ "Entre 3 e 4",
           C01012 %in% c(4181:5225) ~ "Entre 4 e 5",
           C01012 >= 5226 ~ "Mais de 5"),
           levels = c("Menos de um salário mínimo",
               "Entre 1 e 2",
               "Entre 2 e 3",
               "Entre 3 e 4",
               "Entre 4 e 5",
               "Mais de 5")),
         domicilio_situacao = factor(case_when(
           F001 == 1 ~ "Próprio - já pago",
           F001 == 2 ~ "Próprio - ainda pagando" ,                                  
           F001 == 3 ~ "Alugado",
           F001 %in% 4:6 ~ "Cedido (Por empregador, Familiar ou outro)"),
           levels = c("Próprio - já pago",
                      "Próprio - ainda pagando",
                      "Alugado", 
                      "Cedido (Por empregador, Familiar ou outro)")),
         home_office = ifelse(C013 == 1, "Home Office", "Presencial"),
         auxilio_emergencial = ifelse(D0051 == 1, "Auxílio", "Sem auxílio"),
         fez_emprestimo = case_when(
           E001 == 1 ~ "Sim, e pelo menos um morador conseguiu", 
           E001 == 2 ~ "Sim, mas nenhum morador conseguiu ", 
           E001 == 3 ~ "Não solicitou"),
         teve_covid = ifelse(B009B == 1, "Sim", "Não"),
)

names(pnad_pesos)
## [1] "cluster"    "strata"     "has.strata" "prob"       "allprob"   
## [6] "call"       "variables"  "fpc"        "pps"

Análise sobre os empréstimos realizados durante a pandemia

Em meio a pandemia as dificuldades financeiras têm sido uma constante nos diferentes setores sociais. Vejamos como se dá a distribuição da quantidade de pessoas que pediram empréstimos em Belo Horizonte, segundo o PNAD.

A pergunta realizada nas pesquisas, por domicílio, foi: “Durante o período da pandemia alguém deste domicílio solicitou algum empréstimo?” (código da variável E001, segundo o dicionário do PNAD COVID). As respostas para esta pergunta se dividiram em: “Sim, e pelo menos um morador conseguiu”; “Sim, mas nenhum morador conseguiu” e “Não”. Para nossos interesses, dividimos essas respostas em “Sim” (para o primeiro caso, quando de fato o empréstimo se efetivou) ou “Não”, caso contrário.

#names(pnad_pesos$variables)
#?survey_total

# separa um conjunto de dados para fazer o gráfico desejado: empréstimo por sexo, e cor:
sexo_cor <- pnad_pesos %>%
  group_by(Sexo, Cor) %>%
  summarise(emprestimo=survey_total(E001 == 1, na.rm = TRUE)) %>% #pediu empréstimo?
  mutate(fez_emprestimo=emprestimo) %>%
  drop_na()
#head(sexo_cor)

# gera gráfico
subtitle_grafico <- "Durante o período da pandemia alguém deste domicílio solicitou algum empréstimo?"

grafico1 <- ggplot(data=sexo_cor,
                  aes(x=Sexo, y=fez_emprestimo, fill=Cor))+
  geom_bar(position="dodge", stat="identity", width=0.5) +
  scale_y_continuous(limits = c(-100, 0.5e+06)) +
  geom_text(aes(label=sprintf("%.0f", fez_emprestimo)),
            size=3, position=position_dodge(width=0.5),
            vjust=-0.5, hjust=0.5, color = 'black', fontface='bold') +
  labs(title="Pedidos de empréstimo por sexo e cor, Belo Horizonte - MG",
       subtitle=subtitle_grafico,
       x="", y="# pedidos de empréstimo", tag="  ",
       fill="Cor: ",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  theme_dark() +
  guides(col=guide_legend(nrow=1))+
  theme(axis.title = element_text(size=10, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.background = element_rect(fill="white", colour=NA),
        legend.position = 'top',
        legend.justification = 'left',
        legend.direction = 'horizontal',
        legend.title = element_text(size=rel(0.9), face="bold", vjust=0.5),
        panel.background = element_blank(),
        axis.text.x = element_text(angle=0, vjust=0.5, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.y = element_line(colour="gray", size=1),
        panel.grid.major.x = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        strip.background = element_rect(fill="grey90", colour="grey50"),
        strip.text.y = element_text(size=rel(0.8), angle=-90)
  ) 
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
grafico1

Os gráficos acima ilustram que o segmento que realizou mais empréstimos durante o período da pandemia foi o de cor branca, sendo o grupo das mulheres o que contou com a maior quantidade.

#names(pnad_pesos$variables)
#?survey_total

# separa um conjunto de dados para fazer o gráfico desejado: empresitmo por sexo e escolaridade:
sexo_escolaridade <- pnad_pesos %>%
  group_by(Sexo, Escolaridade) %>%
  summarise(emprestimo=survey_total(E001 == 1, na.rm = TRUE)) %>% #pediu empréstimo?
  mutate(fez_emprestimo=emprestimo) %>%
  drop_na()
#head(sexo_cor)

# gera gráfico
subtitle_grafico <- "Durante o período da pandemia alguém deste domicílio solicitou algum empréstimo?"

grafico2 <- ggplot(data=sexo_escolaridade,
                   aes(x=Sexo, y=fez_emprestimo, fill=Escolaridade))+
  geom_bar(position="dodge", stat="identity", width=0.5) +
  scale_y_continuous(limits = c(-100, 0.8e+06)) +
  geom_text(aes(label=sprintf("%.0f", fez_emprestimo)),
            size=2.8, position=position_dodge(width=0.5),
            vjust=0.1, hjust=-0.1, color = 'black', fontface='bold', angle=60) +
  labs(title="Pedidos de empréstimo por sexo e escolaridade, Belo Horizonte - MG",
       subtitle=subtitle_grafico,
       x="", y="# pedidos de empréstimo", tag="  ",
       fill="Escolaridade: ",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  theme_dark() +
  guides(col=guide_legend(nrow=1))+
  theme(axis.title = element_text(size=12, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.background = element_rect(fill="white", colour=NA),
        legend.position = 'right',
        legend.justification = 'center',
        legend.direction = 'vertical',
        legend.text = element_text(size=rel(0.9), vjust=0.5),
        legend.title = element_text(size=rel(0.9), vjust=0.5, face="bold"),
        panel.background = element_blank(),
        axis.text.x = element_text(angle=0, vjust=0.5, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.y = element_line(colour="gray", size=1),
        panel.grid.major.x = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        #plot.title = element_text(size=rel(0.9)),
        strip.background = element_rect(fill="grey90", colour="grey50"),
        strip.text.y = element_text(size=rel(0.7), angle=-90)
  ) 

grafico2

Dentre os que pediram empréstimo, para ambos os sexos, os que possuem grau de escolaridade ‘Sem instrução ou Fundamental’ ou ‘Médio completo e Superior Incompleto’ são os que tiveram mais participação. O gráfico ilustra esse aspecto, além de mostrar que aqueles com ‘Pós-graduação’ foram os que menos pediram empréstimo ao durante a pandemia.

# separa um conjunto de dados para fazer o gráfico desejado: solicitou empréstimo mas não conseguiu por sexo e cor em bh:
sexo_cor <- pnad_pesos %>%
  group_by(Sexo, Cor) %>%
  summarise(emprestimo=survey_total(E001 == 2, na.rm = TRUE)) %>% #pediu empréstimo e não conseguiu?
  mutate(nao_conseguiu_emprestimo=emprestimo) %>%
  drop_na()
#head(sexo_cor)

# gera gráfico
subtitle_grafico <- "Durante a pandemia alguém deste domicílio solicitou algum empréstimo, mas não conseguiu?"

grafico3 <- ggplot(data=sexo_cor,
                  aes(x=Sexo, y=nao_conseguiu_emprestimo, fill=Cor))+
  geom_bar(position="dodge", stat="identity", width=0.5) +
  scale_y_continuous(limits = c(-100, 1e+05)) +
  geom_text(aes(label=sprintf("%.0f", nao_conseguiu_emprestimo)),
            size=3, position=position_dodge(width=0.5),
            vjust=0.5, hjust=1.0, color = 'black', fontface='bold') +
  labs(title="Pedidos de empréstimo negados por sexo e cor, Belo Horizonte - MG",
       subtitle=subtitle_grafico,
       x="", y="# pedidos de empréstimo negados", tag="  ",
       fill="Cor: ",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  theme_dark() +
  guides(col=guide_legend(nrow=1))+
  theme(axis.title = element_text(size=10, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.background = element_rect(fill="white", colour=NA),
        legend.position = 'top',
        legend.justification = 'left',
        legend.direction = 'horizontal',
        legend.title = element_text(size=rel(0.9), face="bold", vjust=0.5),
        panel.background = element_blank(),
        axis.text.x = element_text(angle=0, vjust=0.5, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_line(colour="gray", size=1),
        panel.grid.major.y = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        strip.background = element_rect(fill="grey90", colour="grey50"),
        strip.text.y = element_text(size=rel(0.8), angle=-90)
  ) +
  coord_flip()

grafico3

No primeiro gráfico exibido no início deste trabalho (sob título “Pedidos de empréstimo por sexo e cor (Belo Horizonte - MG)”), vimos que as mulheres brancas formam o principal estrato, somando um total de 459016 empréstimos, quando se trata de empréstimos concedido no município de Belo Horizonte durante o período da pandemia. Já o gráfico acima, referente aos empréstimos negados, mostram que os homems pardos são os que mais solicitaram e não tiveram sucesso em seus pedidos. Ainda neste quesito, as mulheres pardas ocupam a segunda posição. Essas informações ilustram uma tendencia socio-cultural compartilhada pelos cerca de 2,5 milhões de habitantes de Belo Horizonte no período analisado.

Os gráficos mostram que cor, sexo e nível de escolaridade dos indivíduos podem influenciar no quesito “empréstimos durante a pandemia”.

Informações sobre COVID-19

Restringindo a questão “Durante a pandemia alguém do domicílio pediu empréstimo, mas não conseguiu?” aos que testaram positivo para COVID-19, a distrubuição dos dados fica como o gráfico abaixo.

# separa um conjunto de dados para fazer o gráfico desejado: solicitou empréstimo mas não conseguiu por sexo e COVID-19 em bh:
cor_sexo_covid <- pnad_pesos %>%
  filter(B009B == 1, na.rm = TRUE) %>% 
  group_by(Sexo, Cor) %>%
  summarise(emprestimo=survey_total(E001 == 2, na.rm = TRUE)) %>% # pediu empréstimo e não conseguiu?
  mutate(nao_conseguiu_emprestimo=emprestimo) %>%
  drop_na()
#head(cor_covid)

grafico4 <- ggplot(data=cor_sexo_covid,
                  aes(x=Cor, y=nao_conseguiu_emprestimo, fill=Sexo))+
  geom_bar(position="dodge", stat="identity", width=0.5) +
  scale_y_continuous(limits = c(0, 1.05e+03)) +
  geom_text(aes(label=sprintf("%.0f", nao_conseguiu_emprestimo)),
            size=3, position=position_dodge(width=0.5),
            vjust=-0.5, hjust=0.5, color = 'black', fontface='bold') +
  facet_wrap(~Cor) +
  labs(title='Pedidos de empréstimo negados por cor e sexo em pessoas com COVID-19',
       subtitle='Belo Horizonte - MG',
       x="", y="# pedidos de empréstimo negados", tag="  ",
       fill="Sexo: ",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  theme_dark() +
  guides(col=guide_legend(nrow=1))+
  theme(axis.title = element_text(size=10, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.background = element_rect(fill="white", colour=NA),
        legend.position = 'top',
        legend.justification = 'left',
        legend.direction = 'horizontal',
        legend.title = element_text(size=rel(0.9), face="bold", vjust=0.5),
        panel.background = element_blank(),
        axis.text.x = element_text(angle=0, vjust=0.5, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        strip.background = element_blank(), 
        strip.text.y = element_text(size=rel(0.8), angle=-90)
  )

grafico4

A seguir vemos a relação entre empréstimos solicitados e o tipo de emprego dos entrevistados pelo PNAD.

# tipo de emprego e empréstimos
home_emprego_emprestimo <- pnad_pesos %>%
    filter(E001 == 1) %>%
  group_by(Tipo_emprego) %>%
  summarise(esta_em_home_office = survey_total(C013 == 1, na.rm=TRUE)) %>%
  mutate(home_office_emprestimo = esta_em_home_office) %>%
  drop_na()
head(home_emprego_emprestimo)
## # A tibble: 6 × 4
##   Tipo_emprego esta_em_home_office esta_em_home_office_se home_office_emprestimo
##   <fct>                      <dbl>                  <dbl>                  <dbl>
## 1 Trabalhador…                  0                      0                      0 
## 2 Militar                       0                      0                      0 
## 3 Policial ou…                  0                      0                      0 
## 4 Setor priva…              16361.                  3411.                 16361.
## 5 Setor públi…              30902.                  5433.                 30902.
## 6 Empregador                 3150.                  1877.                  3150.
# eixo x ordenado
legenda_trabalhos <- c("Trabalhador doméstico\n (empregado doméstico \n cuidados, babá)",
                       "Militar", 
                       "Policial ou\n Bombeiro", 
                       "Setor privado", "Setor público",
                       "Empregador", "Autônomo\n (Conta própria)")

grafico5 <- ggplot(home_emprego_emprestimo, 
                   aes(x=Tipo_emprego, y=home_office_emprestimo, fill=Tipo_emprego)) +
  geom_bar(position = "dodge", stat = "identity", width=0.5) +
  scale_y_continuous(limits = c(-10, 0.35e+05)) +
  scale_x_discrete(labels = legenda_trabalhos) +
  labs(title="Pedidos de empréstimo por tipo de emprego em home-office",
       subtitle= "Belo Horizonte - MG",
       x="Tipo de emprego", y="# pedidos de empréstimo",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  geom_text(aes(label=sprintf("%.0f", home_office_emprestimo)),
            size=2.8, position =position_dodge(width=0.5),
            vjust=-0.4, hjust=0.50, color='black', fontface='bold') +
  theme_dark() +
    theme(axis.title = element_text(size=12, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.position = "none",
        panel.background = element_blank(),
        axis.text.x = element_text(angle=90, hjust=0.95, vjust=0.3, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.y = element_line(colour="gray", size=1),
        panel.grid.major.x = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        strip.background = element_rect(fill="grey90", colour="grey50"),
        strip.text.y = element_text(size=rel(0.7), angle=-90)
  )

grafico5

Comparação sobre o pedido de empréstimos no estado de SP e MG

db_pesos_br <- db_pnad %>%
  as_survey_design(ids=UPA, strata=Estrato, weights=V1032, nest=TRUE)
  

pnad_pesos_br <- db_pesos_br %>% 
  mutate(one = 1,
         Sexo = ifelse(A003 == 1, "Homem", "Mulher"),
         #estado = fct_reorder(estado, desc(estado)),
         Idade = case_when(
           A002 %in% 15:24 ~ "15-24",
           A002 %in% 25:34 ~ "25-34", 
           A002 %in% 35:49 ~ "35-49", 
           A002 %in% 50:64 ~ "50-64", 
           A002 > 64 ~ "65+"),
         Cor = case_when(
           A004 == 1 ~ "Branca", 
           A004 == 2 ~ "Preta", 
           A004 == 4 ~ "Parda"),
         Escolaridade = factor(case_when(  ##usando factor conseguimos ordenar
           A005 %in% 1:2 ~ "Sem Instrução ou Fundamental Incompleto", 
           A005 %in% 3:4 ~ "Fundamental completo ou Médio Incompleto", 
           A005 %in% 5:6 ~ "Médio completo ou Superior Incompleto", 
           A005 == 7 ~ "Superior completo", 
           A005 == 8 ~ "Pós-graduação"), 
           levels = c("Sem Instrução ou Fundamental Incompleto",
                      "Fundamental completo ou Médio Incompleto", 
                      "Médio completo ou Superior Incompleto",
                      "Superior completo",
                      "Pós-graduação")), 
         Tipo_emprego = factor(case_when(
           C007 == 1 ~ "Trabalhador doméstico (empregado doméstico, cuidados, babá)",
           C007 == 2 ~ "Militar",
           C007 == 3 ~ "Policial ou Bombeiro",
           C007 == 4 ~ "Setor privado",
           C007 == 5 ~ "Setor público",
           C007 == 6 ~ "Empregador",
           C007 == 7 ~ "Autônomo (Conta própria)"),
           levels = c( "Trabalhador doméstico (empregado doméstico, cuidados, babá)",
               "Militar", 
               "Policial ou Bombeiro",
               "Setor privado",
               "Setor público",
               "Empregador",
               "Autônomo (Conta própria)")), 
         Faixa_salario = factor(case_when(
           C01012 <= 1044 ~ "Menos de um salário mínimo",
           C01012 %in% c(1045:2090) ~ "Entre 1 e 2",
           C01012 %in% c(2091:3135) ~ "Entre 2 e 3",
           C01012 %in% c(3136:4180) ~ "Entre 3 e 4",
           C01012 %in% c(4181:5225) ~ "Entre 4 e 5",
           C01012 >= 5226 ~ "Mais de 5"),
           levels = c("Menos de um salário mínimo",
               "Entre 1 e 2",
               "Entre 2 e 3",
               "Entre 3 e 4",
               "Entre 4 e 5",
               "Mais de 5")),
         domicilio_situacao = factor(case_when(
           F001 == 1 ~ "Próprio - já pago",
           F001 == 2 ~ "Próprio - ainda pagando" ,                                  
           F001 == 3 ~ "Alugado",
           F001 %in% 4:6 ~ "Cedido (Por empregador, Familiar ou outro)"),
           levels = c("Próprio - já pago",
                      "Próprio - ainda pagando",
                      "Alugado", 
                      "Cedido (Por empregador, Familiar ou outro)")),
         home_office = ifelse(C013 == 1, "Home Office", "Presencial"),
         auxilio_emergencial = ifelse(D0051 == 1, "Auxílio", "Sem auxílio"),
         fez_emprestimo = case_when(
           E001 == 1 ~ "Sim, e pelo menos um morador conseguiu", 
           E001 == 2 ~ "Sim, mas nenhum morador conseguiu ", 
           E001 == 3 ~ "Não solicitou"),
         teve_covid = ifelse(B009B == 1, "Sim", "Não"),
         estado = case_when(
           UF == 11 ~ 'Rondônia', 
           UF == 12 ~ 'Acre',
           UF == 13 ~ 'Amazonas',
           UF == 14 ~   'Roraima',
           UF == 15 ~   'Pará',
           UF == 16 ~   'Amapá',
           UF == 17 ~   'Tocantins',
           UF == 21 ~   'Maranhão', 
           UF == 22 ~ 'Piauí',
           UF == 23 ~ 'Ceará',
           UF == 24 ~   'Rio Grande do Norte', 
           UF == 25 ~   'Paraíba',
           UF == 26 ~   'Pernambuco',
           UF == 27 ~   'Alagoas',
           UF == 28 ~   'Sergipe', 
           UF == 29 ~   'Bahia',
           UF == 31 ~ 'Minas Gerais',
           UF == 32 ~ 'Espírito Santo',
           UF == 33 ~   'Rio de Janeiro',
           UF == 35 ~   'São Paulo',
           UF == 41 ~ 'Paraná',
           UF == 42 ~   'Santa Catarina',
           UF == 43 ~   'Rio Grande do Sul',
           UF == 50 ~ 'Mato Grosso do Sul',
           UF == 51 ~   'Mato Grosso',
           UF == 52 ~   'Goiás',
           UF == 53 ~   'Distrito Federal')
)

names(pnad_pesos_br)
## [1] "cluster"    "strata"     "has.strata" "prob"       "allprob"   
## [6] "call"       "variables"  "fpc"        "pps"
#names(pnad_pesos$variables)
#?survey_total

# separa um conjunto de dados para fazer o gráfico desejado: empréstimo por sexo, e cor:

sexo_cor <- pnad_pesos_br %>%
  filter(UF %in% c(31, 35)) %>%
  group_by(estado, Sexo, Cor) %>%
  summarise(emprestimo=survey_total(E001 == 1, na.rm = TRUE)) %>% #pediu empréstimo?
  mutate(fez_emprestimo=emprestimo) %>%
  drop_na()
#sexo_cor

# gera gráfico
subtitle_grafico <- "Durante o período da pandemia alguém deste domicílio solicitou algum empréstimo?"

grafico6 <- ggplot(data=sexo_cor,
                  aes(x=Sexo, y=fez_emprestimo, fill=Cor))+
  geom_bar(position="dodge", stat="identity", width=0.5) +
  scale_y_continuous(limits = c(-100, 1.5e+06)) +
  facet_wrap(vars(estado)) +
  geom_text(aes(label=sprintf("%.0f", fez_emprestimo)),
            size=3, position=position_dodge(width=0.5),
            vjust=-0.5, hjust=0.5, color = 'black', fontface='bold') +
  labs(title="Pedidos de empréstimo por sexo e cor",
       subtitle=subtitle_grafico,
       x="", y="# pedidos de empréstimo", tag="  ",
       fill="Cor: ",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  theme_dark() +
  guides(col=guide_legend(nrow=1))+
  theme(axis.title = element_text(size=10, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.background = element_rect(fill="white", colour=NA),
        legend.position = 'top',
        legend.justification = 'left',
        legend.direction = 'horizontal',
        legend.title = element_text(size=rel(0.9), face="bold", vjust=0.5),
        panel.background = element_blank(),
        axis.text.x = element_text(angle=0, vjust=0.5, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.y = element_line(colour="gray", size=1),
        panel.grid.major.x = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        #strip.background = element_rect(fill="grey90", colour="grey50"),
        strip.text.y = element_text(size=rel(0.8), angle=-90)
  )

grafico6

#names(pnad_pesos$variables)
#?survey_total

# separa um conjunto de dados para fazer o gráfico desejado: empresitmo por sexo e escolaridade:
sexo_escolaridade <- pnad_pesos_br %>%
  filter(UF %in% c(31, 35)) %>%
  group_by(estado, Sexo, Escolaridade) %>%
  summarise(emprestimo=survey_total(E001 == 1, na.rm = TRUE)) %>% #pediu empréstimo?
  mutate(fez_emprestimo=emprestimo) %>%
  drop_na()

# gera gráfico
subtitle_grafico <- "Durante o período da pandemia alguém deste domicílio solicitou algum empréstimo?"

grafico7 <- ggplot(data=sexo_escolaridade,
                   aes(x=Sexo, y=fez_emprestimo, fill=Escolaridade))+
  geom_bar(position="dodge", stat="identity", width=0.5) +
  scale_y_continuous(limits = c(-100, 0.9e+06)) +
  facet_wrap(vars(estado)) +
  geom_text(aes(label=sprintf("%.0f", fez_emprestimo)),
            size=2.8, position=position_dodge(width=0.5),
            vjust=0.1, hjust=-0.1, color = 'black', fontface='bold', angle=60) +
  labs(title="Pedidos de empréstimo por sexo e escolaridade",
       subtitle=subtitle_grafico,
       x="", y="# pedidos de empréstimo", tag="  ",
       fill="Escolaridade: ",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  theme_dark() +
  guides(col=guide_legend(nrow=1))+
  theme(axis.title = element_text(size=12, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.background = element_rect(fill="white", colour=NA),
        legend.position = 'right',
        legend.justification = 'center',
        legend.direction = 'vertical',
        legend.text = element_text(size=rel(0.9), vjust=0.5),
        legend.title = element_text(size=rel(0.9), vjust=0.5, face="bold"),
        panel.background = element_blank(),
        axis.text.x = element_text(angle=0, vjust=0.5, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.y = element_line(colour="gray", size=1),
        panel.grid.major.x = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        strip.text.y = element_text(size=rel(0.7), angle=-90)
  ) 

grafico7

# separa um conjunto de dados para fazer o gráfico desejado: solicitou empréstimo mas não conseguiu por sexo e cor em bh:
sexo_cor <- pnad_pesos_br %>%
  filter(UF %in% c(31, 35)) %>%
  group_by(estado, Sexo, Cor) %>%
  summarise(emprestimo=survey_total(E001 == 2, na.rm = TRUE)) %>% #pediu empréstimo e não conseguiu?
  mutate(nao_conseguiu_emprestimo=emprestimo) %>%
  drop_na()
#head(sexo_cor)

# gera gráfico
subtitle_grafico <- "Durante a pandemia alguém deste domicílio solicitou algum empréstimo, mas não conseguiu?"

grafico8 <- ggplot(data=sexo_cor,
                  aes(x=Sexo, y=nao_conseguiu_emprestimo, fill=Cor))+
  geom_bar(position="dodge", stat="identity", width=0.5) +
  facet_wrap(vars(estado)) +
  geom_text(aes(label=sprintf("%.0f", nao_conseguiu_emprestimo)),
            size=3, position=position_dodge(width=0.5),
            vjust=0.5, hjust=1.0, color = 'black', fontface='bold') +
  labs(title="Pedidos de empréstimo negados por sexo e cor",
       subtitle=subtitle_grafico,
       x="", y="# pedidos de empréstimo negados", tag="  ",
       fill="Cor: ",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  theme_dark() +
  guides(col=guide_legend(nrow=1))+
  theme(axis.title = element_text(size=10, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.background = element_rect(fill="white", colour=NA),
        legend.position = 'top',
        legend.justification = 'left',
        legend.direction = 'horizontal',
        legend.title = element_text(size=rel(0.9), face="bold", vjust=0.5),
        panel.background = element_blank(),
        axis.text.x = element_text(angle=0, vjust=0.5, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_line(colour="gray", size=1),
        panel.grid.major.y = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        strip.text.y = element_text(size=rel(0.8), angle=-90)
  ) +
  coord_flip()

grafico8

# separa um conjunto de dados para fazer o gráfico desejado: solicitou empréstimo mas não conseguiu por sexo e COVID-19 em bh:

cor_sexo_covid <- pnad_pesos_br %>%
  filter(UF %in% c(31, 35)) %>%
  filter(B009B == 1, na.rm = TRUE) %>% 
  group_by(estado, Sexo, Cor) %>%
  #group_by(Sexo, Cor) %>%
  summarise(emprestimo=survey_total(E001 == 2, na.rm = TRUE)) %>% # pediu empréstimo e não conseguiu?
  mutate(nao_conseguiu_emprestimo=emprestimo) %>%
  drop_na()
head(cor_sexo_covid)
## # A tibble: 6 × 6
## # Groups:   estado, Sexo [2]
##   estado       Sexo   Cor    emprestimo emprestimo_se nao_conseguiu_emprestimo
##   <chr>        <chr>  <chr>       <dbl>         <dbl>                    <dbl>
## 1 Minas Gerais Homem  Branca      1017.         1017.                    1017.
## 2 Minas Gerais Homem  Parda        832.          832.                     832.
## 3 Minas Gerais Homem  Preta        505.          505.                     505.
## 4 Minas Gerais Mulher Branca       513.          513.                     513.
## 5 Minas Gerais Mulher Parda          0             0                        0 
## 6 Minas Gerais Mulher Preta          0             0                        0
grafico9 <- ggplot(data=cor_sexo_covid,
                  aes(x=Cor, y=nao_conseguiu_emprestimo, fill=Sexo))+
  geom_bar(position="dodge", stat="identity", width=0.5) +
  scale_y_continuous(limits = c(0, 4e+03)) +
  geom_text(aes(label=sprintf("%.0f", nao_conseguiu_emprestimo)),
            size=3, position=position_dodge(width=0.5),
            vjust=-0.5, hjust=0.5, color = 'black', fontface='bold') +
  facet_wrap(vars(estado)) +
  labs(title='Pedidos de empréstimo negados por cor e sexo em pessoas com COVID-19',
       subtitle='',
       x="", y="# pedidos de empréstimo negados", tag="  ",
       fill="Sexo: ",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  theme_dark() +
  guides(col=guide_legend(nrow=1))+
  theme(axis.title = element_text(size=10, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.background = element_rect(fill="white", colour=NA),
        legend.position = 'top',
        legend.justification = 'left',
        legend.direction = 'horizontal',
        legend.title = element_text(size=rel(0.9), face="bold", vjust=0.5),
        panel.background = element_blank(),
        axis.text.x = element_text(angle=0, vjust=0.5, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        strip.text.y = element_text(size=rel(0.8), angle=-90)
  )

grafico9

# tipo de emprego e empréstimos
home_emprego_emprestimo <- pnad_pesos_br %>%
  filter(UF %in% c(31, 35)) %>%
  filter(E001 == 1) %>%
  group_by(estado, Tipo_emprego) %>%
  summarise(esta_em_home_office = survey_total(C013 == 1, na.rm=TRUE)) %>%
  mutate(home_office_emprestimo = esta_em_home_office) %>%
  drop_na()
#head(home_emprego_emprestimo)
  
# eixo x ordenado
legenda_trabalhos <- c("Trabalhador doméstico\n (empregado doméstico \n cuidados, babá)",
                       "Militar", 
                       "Policial ou\n Bombeiro", 
                       "Setor privado", "Setor público",
                       "Empregador", "Autônomo\n (Conta própria)")

grafico10 <- ggplot(home_emprego_emprestimo, 
                   aes(x=Tipo_emprego, y=home_office_emprestimo, fill=Tipo_emprego)) +
  geom_bar(position = "dodge", stat = "identity", width=0.5) +
  scale_y_continuous(limits = c(-10, 1.4e+05)) +
  scale_x_discrete(labels = legenda_trabalhos) +
  facet_wrap(vars(estado)) +
  labs(title="Pedidos de empréstimo por tipo de emprego em home-office",
       x="Tipo de emprego", y="# pedidos de empréstimo",
       caption="Fonte: Microdados da PNAD COVID-19, IBGE, Nov/2020") +
  geom_text(aes(label=sprintf("%.0f", home_office_emprestimo)),
            size=2.8, position =position_dodge(width=0.5),
            vjust=-0.4, hjust=0.50, color='black', fontface='bold') +
  theme_dark() +
    theme(axis.title = element_text(size=12, face="italic"),
        plot.caption = element_text(hjust= 0.0, size=8, vjust=0.0),
        axis.line.y = element_blank(), 
        axis.line.x = element_line(lineend="round"),
        axis.ticks.length = unit(0.2, "lines"), 
        axis.ticks.y = element_line(colour="gray", size=1),
        legend.position = "none",
        panel.background = element_blank(),
        axis.text.x = element_text(angle=90, hjust=0.95, vjust=0.3, face="bold"),
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.y = element_line(colour="gray", size=1),
        panel.grid.major.x = element_blank(),
        plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"),
        strip.text.y = element_text(size=rel(0.7), angle=-90)
  )

grafico10