Conhecendo o R
#R BƔsico
#Calculadora
2+2
## [1] 4
5-1
## [1] 4
2*3
## [1] 6
4/5
## [1] 0.8
#Sequencia
1:100
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## [91] 91 92 93 94 95 96 97 98 99 100
#Vetor / objetos
valor <- 5
pares <- c(0,2,4,6,8)
cores <- c("azul","vermelho","amarelo")
valor
## [1] 5
pares
## [1] 0 2 4 6 8
cores
## [1] "azul" "vermelho" "amarelo"
#Listas
ls()
## [1] "cores" "pares" "valor"
#Matriz
matrix(1:100,nrow=20,ncol=5)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 21 41 61 81
## [2,] 2 22 42 62 82
## [3,] 3 23 43 63 83
## [4,] 4 24 44 64 84
## [5,] 5 25 45 65 85
## [6,] 6 26 46 66 86
## [7,] 7 27 47 67 87
## [8,] 8 28 48 68 88
## [9,] 9 29 49 69 89
## [10,] 10 30 50 70 90
## [11,] 11 31 51 71 91
## [12,] 12 32 52 72 92
## [13,] 13 33 53 73 93
## [14,] 14 34 54 74 94
## [15,] 15 35 55 75 95
## [16,] 16 36 56 76 96
## [17,] 17 37 57 77 97
## [18,] 18 38 58 78 98
## [19,] 19 39 59 79 99
## [20,] 20 40 60 80 100
array(1:100,dim=c(5,5,4))
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 6 11 16 21
## [2,] 2 7 12 17 22
## [3,] 3 8 13 18 23
## [4,] 4 9 14 19 24
## [5,] 5 10 15 20 25
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 26 31 36 41 46
## [2,] 27 32 37 42 47
## [3,] 28 33 38 43 48
## [4,] 29 34 39 44 49
## [5,] 30 35 40 45 50
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 51 56 61 66 71
## [2,] 52 57 62 67 72
## [3,] 53 58 63 68 73
## [4,] 54 59 64 69 74
## [5,] 55 60 65 70 75
##
## , , 4
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 76 81 86 91 96
## [2,] 77 82 87 92 97
## [3,] 78 83 88 93 98
## [4,] 79 84 89 94 99
## [5,] 80 85 90 95 100
#Graficos
plot(1:10,10:1)

plot(1:10,10:1,pch=2)

plot(1:10,10:1,pch=2,col=2)

plot(1:10,10:1,pch=2,xlab="eixo x")

plot(1:10,10:1,pch=2,xlab="eixo x",ylab="eixo y")

hist(rnorm(1000),col=33)
arrows(2,100,2,70,col=2)

boxplot(iris$Sepal.Length~iris$Species)

#FunƧƵes
media=function(objeto){sum(objeto)/length(objeto)}
media(c(1,2,3))
## [1] 2
alturas=c(1.4,1.7,2.0,1.6,1.8)
media(alturas)
## [1] 1.7
#Medidas estatisticas
sum(alturas) # soma dos elementos do objeto "alturas"
## [1] 8.5
length(alturas) # nĆŗmero de elementos do objeto "alturas"
## [1] 5
mean(alturas) # mƩdia dos elementos do objeto "alturas"
## [1] 1.7
Adquirindo e preparando os dados
#Bibliotecas
library(geobr)
## Warning: package 'geobr' was built under R version 4.2.3
## Loading required namespace: sf
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_FileGDB.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_FileGDB.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_MSSQLSpatial.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_MSSQLSpatial.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_OCI.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_OCI.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_PG.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_PG.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_FileGDB.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_FileGDB.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_MSSQLSpatial.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_MSSQLSpatial.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_OCI.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_OCI.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_PG.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
## Warning in CPL_gdal_init(): GDAL Error 1: Can't load requested DLL: C:\Program Files\GeoDa Software\ogr_PG.dll
## 126: NĆ£o foi possĆvel encontrar o módulo especificado.
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.3.2, PROJ 7.2.1; sf_use_s2() is TRUE
library(microdatasus)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## āā 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 ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
# Download base grƔfica
mun <- read_municipality(code_muni = 33, year = 2020)
## Using year 2020
##
Downloading: 780 B
Downloading: 780 B
Downloading: 2 kB
Downloading: 2 kB
Downloading: 18 kB
Downloading: 18 kB
Downloading: 34 kB
Downloading: 34 kB
Downloading: 51 kB
Downloading: 51 kB
Downloading: 67 kB
Downloading: 67 kB
Downloading: 83 kB
Downloading: 83 kB
Downloading: 99 kB
Downloading: 99 kB
Downloading: 120 kB
Downloading: 120 kB
Downloading: 130 kB
Downloading: 130 kB
Downloading: 140 kB
Downloading: 140 kB
Downloading: 150 kB
Downloading: 150 kB
Downloading: 160 kB
Downloading: 160 kB
Downloading: 180 kB
Downloading: 180 kB
Downloading: 190 kB
Downloading: 190 kB
Downloading: 200 kB
Downloading: 200 kB
Downloading: 210 kB
Downloading: 210 kB
Downloading: 220 kB
Downloading: 220 kB
Downloading: 240 kB
Downloading: 240 kB
Downloading: 250 kB
Downloading: 250 kB
Downloading: 260 kB
Downloading: 260 kB
Downloading: 270 kB
Downloading: 270 kB
Downloading: 280 kB
Downloading: 280 kB
Downloading: 280 kB
Downloading: 280 kB
mun$MUNIC_RES <- str_sub(mun$code_muni, end = -2)
#Download dados tabulares
df <- fetch_datasus(year_start = 2021, month_start = 3,
year_end = 2021, month_end = 3,
uf = "RJ",
information_system = "SIH-RD")
## Your local Internet connection seems to be ok.
## DataSUS FTP server seems to be up. Starting download...
df_a <- process_sih(df)
df_b <- process_sih(df, municipality_data = FALSE)
#Filtro SIH Covid
df_c<-df_b %>%
filter(DIAG_PRINC == "B342")
#CƔlculo indicador
df_d<-df_c %>%
group_by(MUNIC_RES, MARCA_UTI) %>%
summarise(n=n()) %>%
transmute(MARCA_UTI, percent = (n/sum(n)*100))
## `summarise()` has grouped output by 'MUNIC_RES'. You can override using the
## `.groups` argument.
#Join dados graficos e dados tabulares
mapa<-mun %>%
left_join(y=df_d, by=c("MUNIC_RES"="MUNIC_RES"))
Gerando o mapa
# plot
ggplot() + #plot do mapa
geom_sf(data = mun, alpha = .9, color = 1) + #camada base
geom_sf(data=mapa, aes(fill=percent), size=.15) + #camada mapa
facet_wrap(~MARCA_UTI, ncol=5)+ #facet de mapas
labs(subtitle="Utilização de UTI, 2020", size=8) + # tĆtutlo do mapa
scale_fill_viridis_c(direction = -1, name="%", limits = c(0, 100)) #cores e legenda
