Divisão das atividades:

Hilário: Utilização do GGPlot para realização dos gráficos de Agro, Exportação, Importação e Indústria.

Guilherme Ziegler: Formatação do arquivo em markdown, adição dos títulos dos enunciados, coloquei os códigos de install packages em comentário caso o usuário ainda não os tenha instalado (dá erro na hora de gerar um markdown, Outra opção seria tirar do chunk), formatação de cores dos gráficos, ênfase no cálculo da taxa de investimento (FBKF/PIB). Exercício 2, corrigi a importação de dados via link que não estava funcionando e usei a library read(readxl) comando read_excel().

Guilherme Lourençato: Utilização do GGPlot para geração dos gráficos com taxas de desemprego das regiões Norte e Nordeste, além do gráfico do PIB.

Gustavo Munhoz: Utilização do GGPlot para geração dos gráficos com taxas de desemprego das regiões Sul e Sudeste, além de gráficos do Governo e serviços.

Instalando e rodando pacotes para a análise de dados.

# Descomente os códigos abaixo e faça a instalação dos pacotes necessários. O
# arquivo não poderá ser knitado com install packages num chunk
#install.packages("sidrar")
#install.packages("dynlm")
#install.packages("tidyverse")
#install.packages("readxl")
#install.packages("lubridate")
library(seasonal)
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(RColorBrewer)
library(sidrar)
library(ggplot2)
library(scales)
library(dynlm)
## Carregando pacotes exigidos: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(gridExtra)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:gridExtra':
## 
##     combine
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(forecast)
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble  3.1.8     ✔ purrr   0.3.4
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ readr::col_factor() masks scales::col_factor()
## ✖ dplyr::combine()    masks gridExtra::combine()
## ✖ purrr::discard()    masks scales::discard()
## ✖ dplyr::filter()     masks stats::filter()
## ✖ dplyr::lag()        masks stats::lag()
## ✖ tibble::view()      masks seasonal::view()
library(readxl)
library(lubridate)
## 
## Attaching package: 'lubridate'
## 
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(ggthemes)

#EXERCÍCIO 1

#Importando os dados

xls = get_sidra(api = '/t/1620/n1/all/v/all/p/all/c11255/90687,90691,90696,90707,93404,93405,93406,93407,93408/d/v583%202') 
## All others arguments are desconsidered when 'api' is informed
series = c(90687, 90691, 90694, 90696, 90697, 90707, 93404, 93405, 93406, 93407, 93408)
names = c('Agro', 'Indústria', 'COnstrução', 'Serviços', 'Comércio', 'PIB', 'Despesa Familia', 'Governo','FBKF', 'Importações', 'Exportações')

#Organização dos dados

xls = get_sidra(api='/t/1620/n1/all/v/all/p/all/c11255/90687,90691,90696,90707,93404,93405,93406,93407,93408/d/v583%202') %>%
  mutate(Data = as.yearqtr(`Trimestre (Código)`, format='%Y%q'))%>%
  rename('Setores'='Setores e subsetores')%>%
  select(Data, Setores, Valor)%>%
  spread(Setores, Valor)%>%
  rename('Governo' = 'Despesa de consumo da administração pública')%>%
  rename('Despesas das Famílias' = 'Despesa de consumo das famílias')%>%
  rename('Agro' = 'Agropecuária - total')%>%
  rename('Exportação' = 'Exportação de bens e serviços')%>%
  rename('Importação (-)' = 'Importação de bens e serviços (-)')%>%
  rename('Indústria'  = 'Indústria - total')%>%
  rename('PIB' = 'PIB a preços de mercado')%>%
  rename('Serviços' = 'Serviços - total')%>%
  rename('FBKF' = 'Formação bruta de capital fixo')%>%
  as_tibble()
## All others arguments are desconsidered when 'api' is informed

#c) Usando ggpplot, fa¸ca um gráfico com subplots do PIB e de seus componentes usando os dados da primeira tabela.

Agro

agro = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`Agro`), color = "gold")+
  labs(title = 'Agro: 1995-2020')
agro

Exportação

exp = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`Exportação`), color = "purple")+
  labs(title = 'Exportação: 1995-2020')
exp

Importação

imp = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`Importação (-)`), color = "orange")+
  labs(title = 'Importação: 1995-2020')
imp

Indústria

ind = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`Indústria`), color = "pink")+
  labs(title = 'Indústria: 1995-2020')
ind

(d) Faça um gráfico com a série do PIB.

pib = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`PIB`), color = "green")+
  labs(title = 'Variação do PIB: 1995-2020')
pib

Serviços

s = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`Serviços`), color = "#FC4E07")+
  labs(title = 'Serviços: 1995-2020')
s

Governo (G)

g = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`Governo`), color = "#00AFBB")+
  labs(title = 'Despesas do Governo: 1995-2020')
g

Formação Bruta de Capital Fixo.

fbkf = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`FBKF`), color = "black")+
  labs(title = 'Formação Bruta de Capital Fixo: 1995-2020')
fbkf

Despesa das famílias

despfam = ggplot(data=xls)+
  geom_line(mapping= aes(x=Data, y=`Despesas das Famílias`), color = "darkblue")+
  labs(title = 'Despesa das Famílias: 1995-2020')
despfam

Arranjo de todos os gráficos em série

grid.arrange(agro, exp, imp, ind, pib, s, fbkf, g, despfam, nrow = 4)

Gráfico em série

PIB_serie = ggplot(xls)+
  geom_line(mapping= aes(x=Data, y=`PIB`), color = "gold") +
  scale_x_continuous (name = "Anos", breaks = seq(1995, 2020,5)) +
  scale_y_continuous(name = "PIB em série", breaks = seq(90, 180, 10))
PIB_serie

e) Calcule a taxa de investimento (Formação Bruta de Capital Fixo (FBKF) /PIB)) e façaa um gráfico usando ggplot. Coloque cíırculos com valores no seu gráfico.

# calculo da taxa de investimento equivale a FBKF/PIB
xls=mutate(xls, Taxa_de_Investimento=FBKF/PIB)

invest <- ggplot(xls, aes(x=Data, y=Taxa_de_Investimento))+
  geom_line(color = "purple")+
  geom_point(size=7,shape=21, colour="#1a476f", fill="yellow")+
  geom_text(aes(label=sprintf("%0.2f", round(`Taxa_de_Investimento`, digits = 3))), size = 2,
            hjust = 0.5, vjust = 0.5, color = 'gray13')+
  xlab('Data')+ylab('Porcentagem %')+
  labs(title = 'Taxa de Investimento 1995-2020')
  invest

EXERCÍCIO 2

Importando os dados para o seguro desemprego

library(readxl)
# para abrir os dados baixei diretamente no meu computador o seguinte arquivo:

#'http://pdet.mte.gov.br/images/Seguro-Desemprego/Segunda%20Quinzena%20de%20Maio/3-%2#0S%
#C3%A9rie%20Hist%C3%B3rica%20do%20Seguro-Desemprego%20-%202000%20a%202020%20-%20mensa#l.xlsx'

desemprego <- read_excel("3- Série Histórica do Seguro-Desemprego - 2000 a 2020 - mensal.xlsx", sheet="Tabela 1", skip = 5) %>%
drop_na()
## New names:
## • `` -> `...1`
dates = parse_date_time(colnames(desemprego[-1]),
orders='%Y/%m')
names = desemprego$...1
desemprego = t(desemprego[,-1])
row.names(desemprego) = NULL
colnames(desemprego) = names
desemprego =
desemprego %>%
as_tibble() %>%
mutate(DATAS = dates) %>%
mutate(DATAS = as.yearqtr(DATAS, format='%Y%q'))%>%
select(DATAS, everything())

Plotando dados estados da região Nordeste

al <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=ALAGOAS),color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(2000,16000, 4000))+
  labs(title='AL')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

bh <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=BAHIA), color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(12000,48000, 8000))+
  labs(title='BH')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

ce <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=CEARA),color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(7000,35000, 5000))+
  labs(title='CE')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

ma <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=MARANHAO),color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(2400,14000, 2000))+
  labs(title='MA')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

pb <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=PARAIBA),color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(2200,12000, 2000))+
  labs(title='PB')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

pe <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=PERNAMBUCO),color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(7000,35000, 5000))+
  labs(title='PE')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

pi <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=PIAUI),color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(1500,7200, 1200))+
  labs(title='PI')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

se <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=SERGIPE),color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(1700,7500, 1200))+
  labs(title='SE')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

to <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=TOCANTINS),color = "blue")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(1100,4500, 600))+
  labs(title='TO')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

grid.arrange(al, bh, ce, ma, pb, pe, pi, se, to, top='Região Nordeste', ncol=3)

Região Norte

ac <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=ACRE),color="purple4")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(300,2400, 600))+
  labs(title='AC')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

ap <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=AMAPA),color="purple4")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(400,2800, 500))+
  labs(title='AP')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

am <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=AMAZONAS),color="purple4")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(2200,13200, 2000))+
  labs(title='AM')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

pa <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=PARA),color="purple4")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(4600,22000, 4800))+
  labs(title='PA')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

ro <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=RONDONIA),color="purple4")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(1500,7500, 1000))+
  labs(title='RO')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

rr <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=RORAIMA),color="purple4")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(200,1400, 300))+
  labs(title='RR')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

grid.arrange(rr, ro, pa, am, ap, ac, top='Região Norte', ncol=2)

Região Centro-Oeste

df <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`DISTRITO FEDERAL`),color="Orange")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(4000,20000, 3000))+
  labs(title='DF')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

go <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=GOIAS),color="Orange")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(8000,32000, 6000))+
  labs(title='GO')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

mgr <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`MATO GROSSO`),color="Orange")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(4000,20000, 2500))+
  labs(title='MT')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

mgs <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`MATO GROSSO DO SUL`),color="Orange")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(3000,14000, 2000))+
  labs(title='MS')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

grid.arrange(df, go, mgr, mgs, top='Região Centro-Oeste', ncol=2)

Região Sul

rgs <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`RIO GRANDE DO SUL`),color="black")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(20000,68000, 9000))+
  labs(title='RS')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

sc <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`RIO GRANDE DO SUL`),color="black")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(10000,58000, 8000))+
  labs(title='SC')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

pr <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=PARANA),color="black")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(18000,64000, 9000))+
  labs(title='PR')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

grid.arrange(rgs, sc, pr, top='Região Sul', ncol=2)

Região Sudeste

esp.s <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`ESPIRITO SANTO`),color="yellow")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(6000,18000, 3000))+
  labs(title='ES')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

mg <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`MINAS GERAIS`),color="yellow")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(35000,104000, 22000))+
  labs(title='MG')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

rj <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`RIO DE JANEIRO`),color="yellow")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(22000,84000, 20000))+
  labs(title='RJ')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

sp <- ggplot(desemprego)+
  geom_line(mapping=aes(x=DATAS, y=`SAO PAULO`),color="yellow")+
  scale_x_continuous(name= 'Data')+
  scale_y_continuous(name= 'Desemprego', breaks=seq(99000,282000, 38000))+
  labs(title='SP')+
  theme_classic()+
  theme(plot.title=element_text(size=12, hjust=.5))+
  theme(axis.title.x=element_text(size=10, hjust=.5))+
  theme(axis.title.y=element_text(size=10, hjust=.5))

grid.arrange(esp.s, mg, rj, sp, top='Região Sudeste', ncol=2)