2022-09-26
sudo apt install libgdal-dev libudunits2-dev libproj-dev
install.packages('ggplot2')
install.packages('dplyr')
install.packages('sf')
install.packages('ggpubr')
library(ggplot2) library(dplyr) library(sf) library(ggpubr)
rm(list = ls()) # Limpando o environment
load("input_data/cv_all.Rda")
cv<-cv%>%filter(state=='SP') muni<-muni%>%filter(state=='SP') plot(muni)
## Filtrando uma tabela - Criar uma tabela (cv_today) apenas com o registro mais atual de casos e mortes de COVID
cv_today=cv%>%filter(date==max(date)) #Prefiro usar o <- ao invés do = cv_today<-cv%>%filter(date==max(date)) #Verificando o nº de linhas de cada tabela nrow(cv)
## [1] 64790
nrow(cv_today)
## [1] 645
cv_test<-left_join(x=cv_today,y=muni,by=c("cod_mun","state"))
colnames(cv_test)
## [1] "cod_mun" "nome_mun" "state" "date" "week" ## [6] "cases" "deaths" "new_cases" "new_deaths" "nome" ## [11] "pop" "renda" "area" "leitos" "moradoresdom" ## [16] "geometry"
library(sf) library(units) plot(rgi)
#Consertando geometrias rgi<-st_make_valid(rgi) #cálculo de áreas rgi$area<-st_area(rgi)
ggplot(rgi,aes(fill=as.numeric(area)))+geom_sf()+ coord_sf()
colnames(muni)
## [1] "cod_mun" "nome" "pop" "renda" "area" ## [6] "leitos" "moradoresdom" "state" "geometry"
colnames(rgi)
## [1] "rgi" "nome_rgi" "geometry" "area"
#Junção espacial muni<-st_join(muni, rgi, join=st_intersects)
cv_today<-left_join(x=cv_today,y=muni,by=c("cod_mun","state"))
cv<-left_join(x=cv,y=muni,by=c("cod_mun","state"))
## [1] "cod_mun" "nome_mun" "state" "date" "week" ## [6] "cases" "deaths" "new_cases" "new_deaths" "nome" ## [11] "pop" "renda" "area.x" "leitos" "moradoresdom" ## [16] "rgi" "nome_rgi" "area.y" "geometry"
cv_today%>%summarise(rendamedia=mean(renda),
pop=sum(pop),
n=n())
| rendamedia | pop | n |
|---|---|---|
| 1161.344 | 41004920 | 645 |
cv_today_rgi<-cv_today%>%
group_by(nome_rgi)%>%
summarise(rendamedia=mean(renda),
pop=sum(pop),
n=n(),
cases=sum(cases),
deaths=sum(deaths))
| nome_rgi | rendamedia | pop | n | cases | deaths |
|---|---|---|---|---|---|
| Adamantina - Lucélia | 1237.7475 | 120182 | 10 | 14941 | 454 |
| Amparo | 1328.8248 | 122827 | 5 | 14573 | 379 |
| Andradina | 1180.2211 | 178055 | 11 | 21279 | 777 |
| Araçatuba | 1244.2669 | 288118 | 14 | 44289 | 1371 |
| Araraquara | 1088.2218 | 555626 | 17 | 66999 | 1777 |
| Araras | 1350.2683 | 238468 | 4 | 36301 | 924 |
| Assis | 1177.8482 | 225982 | 12 | 31316 | 950 |
| Avaré | 992.7479 | 204352 | 12 | 29038 | 740 |
| Barretos | 1227.5465 | 397273 | 16 | 54181 | 1828 |
| Bauru | 1196.5273 | 584732 | 19 | 89834 | 1982 |
| Birigui - Penápolis | 1076.2464 | 257494 | 19 | 38038 | 1214 |
| Botucatu | 1104.0615 | 235100 | 9 | 34366 | 702 |
| Bragança Paulista | 1150.6822 | 414343 | 11 | 52262 | 1123 |
| Campinas | 1575.6426 | 2681684 | 18 | 338167 | 10612 |
| Caraguatatuba - Ubatuba - São Sebastião | 1259.7559 | 280214 | 4 | 42429 | 822 |
| Catanduva | 1234.8591 | 235258 | 16 | 33464 | 1138 |
| Cruzeiro | 923.4357 | 150050 | 9 | 13508 | 198 |
| Dracena | 1070.7646 | 120427 | 12 | 15917 | 590 |
| Fernandópolis | 1054.6010 | 104036 | 11 | 18207 | 546 |
| Franca | 1087.1154 | 385541 | 10 | 43823 | 1087 |
| Guaratinguetá | 1102.0187 | 294929 | 8 | 25086 | 671 |
| Itapetininga | 1119.7008 | 216234 | 6 | 27670 | 784 |
| Itapeva | 766.7974 | 342564 | 19 | 42304 | 1455 |
| Ituverava | 1165.9335 | 115778 | 6 | 9080 | 472 |
| Jales | 991.3962 | 108323 | 18 | 14785 | 510 |
| Jaú | 1174.5030 | 318166 | 12 | 36861 | 1147 |
| Jundiaí | 1519.9900 | 809699 | 9 | 105242 | 2871 |
| Limeira | 1318.0454 | 331822 | 4 | 40935 | 1400 |
| Lins | 1066.4816 | 149588 | 8 | 18549 | 538 |
| Marília | 1046.2580 | 355662 | 18 | 45587 | 1407 |
| Mogi Guaçu | 1303.8656 | 300212 | 4 | 43285 | 1323 |
| Ourinhos | 1099.3309 | 218174 | 11 | 32022 | 929 |
| Piracicaba | 1400.7931 | 539591 | 11 | 90121 | 1945 |
| Piraju | 910.5359 | 54525 | 5 | 4822 | 174 |
| Presidente Epitácio-Presidente Venceslau | 922.8462 | 89491 | 5 | 7935 | 264 |
| Presidente Prudente | 1026.7685 | 499627 | 28 | 63597 | 1803 |
| Registro | 788.1318 | 246952 | 13 | 29949 | 872 |
| Ribeirão Preto | 1254.7027 | 1297583 | 26 | 175556 | 5304 |
| Rio Claro | 1364.9855 | 220954 | 5 | 18285 | 696 |
| Santa Fé do Sul | 1232.4407 | 45729 | 7 | 7365 | 212 |
| Santos | 1305.9512 | 1682039 | 11 | 170965 | 7363 |
| São Carlos | 1382.0182 | 463119 | 9 | 58034 | 1281 |
| São João da Boa Vista | 1203.8416 | 287734 | 9 | 32428 | 968 |
| São Joaquim da Barra – Orlândia | 1292.4747 | 146518 | 6 | 20305 | 495 |
| São José do Rio Pardo - Mococa | 1117.7394 | 179126 | 7 | 16642 | 573 |
| São José do Rio Preto | 1157.4095 | 809190 | 36 | 164929 | 4720 |
| São José dos Campos | 1333.2199 | 972706 | 8 | 133296 | 3164 |
| São Paulo | 1318.5882 | 19601268 | 39 | 1659632 | 72210 |
| Sorocaba | 1230.2416 | 1556541 | 22 | 200417 | 6036 |
| Tatuí | 1189.4525 | 142796 | 6 | 17418 | 596 |
| Taubaté - Pindamonhangaba | 1159.4942 | 550336 | 10 | 56251 | 1731 |
| Tupã | 957.2438 | 123584 | 8 | 14075 | 433 |
| Votuporanga | 1062.5010 | 154598 | 12 | 28138 | 808 |
hist(muni$renda)
ggplot(muni,aes(x=renda))+geom_histogram()
ggplot(muni,aes(x=renda))+geom_histogram(bins=100)
ggplot(muni,aes(x=renda))+
ggtitle('Renda Domiciliar Média por Município')+ #título do gráfico
geom_histogram(bins=50,fill='pink')+#barras em cor de rosa
theme_bw()+ #fundo branco
xlab('Renda Média (R$)')+ #título do eixo x
ylab('Nº de Municípios') #título do eixo y
ggplot(muni,aes(x=renda,fill=nome_rgi))+
ggtitle('Renda Domiciliar Média por Município')+ #título do gráfico
geom_histogram(bins=50)+
theme_bw()+ #fundo branco
xlim(c(500,2100))+
xlab('Renda Média (R$)')+ #título do eixo x
ylab('Nº de Municípios') #título do eixo y
ggplot(muni,aes(x=renda))+
ggtitle('Renda Domiciliar Média por Município')+ #título do gráfico
geom_histogram(bins=50)+
facet_wrap(~nome_rgi) # o til (~) sempre significa "por" alguma coisa
ggplot(muni,aes(x=renda))+geom_density()
ggplot(muni,aes(x=renda))+ geom_density(bw=25)
ggplot(muni,aes(x=renda,color=nome_rgi,fill=nome_rgi))+ geom_density(bw=200,alpha=.2)+ theme_bw()
ggplot(muni,aes(x=renda))+geom_density()+ facet_wrap(~nome_rgi,scales='free_y')
ggplot(muni,aes(x=nome_rgi,y=renda))+geom_boxplot()+ coord_flip()
library(forcats) muni$nome_rgi<-as.factor(muni$nome_rgi) muni<-muni%>%mutate(nome_rgi=fct_reorder(nome_rgi,renda))
ggplot(cv_today,aes(x=pop,y=cases))+ geom_point()
ggplot(cv_today,aes(x=pop,y=cases))+ geom_point()+ geom_smooth()
library(ggpubr) ggplot(cv_today,aes(x=pop,y=cases))+ geom_point()+ geom_smooth(method='lm')+ stat_regline_equation(label.y = 150000, aes(label = ..eq.label..)) + stat_regline_equation(label.y = 140000, aes(label = ..rr.label..))
cv_day<-cv%>%group_by(date)%>%summarise(deaths=sum(new_deaths, na.rm=T)) head(cv_day)
| date | deaths |
|---|---|
| 2020-02-29 | 0 |
| 2020-03-07 | 0 |
| 2020-03-14 | 0 |
| 2020-03-21 | 9 |
| 2020-03-28 | 58 |
| 2020-04-04 | 193 |
ggplot(cv_day,aes(x=date,y=deaths))+geom_path()
cv_day$year=format(cv_day$date,'%Y') cv_day$month=format(cv_day$date,'%m') #Agrupando por mês cv_month<-cv_day%>%group_by(year, month)%>%summarise(deaths=sum(deaths)) ggplot(cv_month,aes(x=month,y=deaths))+ geom_bar(stat='identity')+ facet_wrap(~year,ncol=1)+ theme_bw()