Atividade final

Author

Luana

O que aprendi na disciplina?

  • Manipulação de dados

    Pacotes como tidyverse e dplyr são muito bons para manipular bases de dados.

    Exemplo

    Usei funções como select, mutate e recode para manipular um dos meus datasets.

    setwd("~/Luana/vegetacao")
    
    library(readr)
    Warning: package 'readr' was built under R version 4.2.3
    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 'purrr' was built under R version 4.2.3
    Warning: package 'dplyr' was built under R version 4.2.3
    Warning: package 'stringr' 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     ✔ purrr     1.0.1
    ✔ forcats   1.0.0     ✔ stringr   1.5.0
    ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
    ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
    ── 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
    veg<- read_delim("vegetacao_PR2019.csv", delim = ";", escape_double = FALSE, trim_ws = TRUE)
    New names:
    Rows: 4723 Columns: 7
    ── Column specification
    ──────────────────────────────────────────────────────── Delimiter: ";" chr
    (2): mun, areakm2 dbl (5): ...1, X, layer, value, count
    ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
    Specify the column types or set `show_col_types = FALSE` to quiet this message.
    • `` -> `...1`
    head(veg)
    # A tibble: 6 × 7
       ...1     X layer value count mun    areakm2
      <dbl> <dbl> <dbl> <dbl> <dbl> <chr>  <chr>  
    1     1     1     1     3 28520 Abatiá 256.68 
    2     2     2     1     9  2009 Abatiá 18.081 
    3     3     3     1    11    86 Abatiá 0.774  
    4     4     4     1    15 58744 Abatiá 528.696
    5     5     5     1    20  4531 Abatiá 40.779 
    6     6     6     1    21 84479 Abatiá 760.311
    vegetacao<- veg %>% select(value, areakm2, mun) %>%  
      filter(value  %in% c("1", "3", "4", "5", "49", "10", "11", "12", "32", "50"," 13")) %>%
      rename(area=areakm2, formacao=value) %>% 
      mutate(formacao = recode(formacao, "3"= "f_florestal", "4" ="savana", "5"= "mangue", "49"= "restinga_a", "10"= "fn_florestal", "11" ="c_alagado", "12" = "f_campestre", "32" = "apicum", "50" = "restinga_h"," 13" = "outras_fnf"))
    
    head(vegetacao)
    # A tibble: 6 × 3
      formacao    area       mun                
      <chr>       <chr>      <chr>              
    1 f_florestal 256.68     Abatiá             
    2 c_alagado   0.774      Abatiá             
    3 f_florestal 10.102.455 Adrianópolis       
    4 f_florestal 1.044.045  Agudos do Sul      
    5 c_alagado   5.058      Agudos do Sul      
    6 f_florestal 1.135.521  Almirante Tamandaré
    str(vegetacao)
    tibble [742 × 3] (S3: tbl_df/tbl/data.frame)
     $ formacao: chr [1:742] "f_florestal" "c_alagado" "f_florestal" "f_florestal" ...
     $ area    : chr [1:742] "256.68" "0.774" "10.102.455" "1.044.045" ...
     $ mun     : chr [1:742] "Abatiá" "Abatiá" "Adrianópolis" "Agudos do Sul" ...
    summary(vegetacao)
       formacao             area               mun           
     Length:742         Length:742         Length:742        
     Class :character   Class :character   Class :character  
     Mode  :character   Mode  :character   Mode  :character  
  • Visualização de Dados

É possível gerar gráficos e painéis de diferentes formas para facilitar a visualização dos dados.

library(ggplot2)
library(dplyr)
library(ggtext)
Warning: package 'ggtext' was built under R version 4.2.3
library(patchwork)
Warning: package 'patchwork' was built under R version 4.2.3
veg1<- vegetacao  %>% subset(area != "NA") %>% mutate(area=as.numeric(area)) 
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `area = as.numeric(area)`.
Caused by warning:
! NAs introduzidos por coerção
head(veg1)
# A tibble: 6 × 3
  formacao       area mun                
  <chr>         <dbl> <chr>              
1 f_florestal 257.    Abatiá             
2 c_alagado     0.774 Abatiá             
3 f_florestal  NA     Adrianópolis       
4 f_florestal  NA     Agudos do Sul      
5 c_alagado     5.06  Agudos do Sul      
6 f_florestal  NA     Almirante Tamandaré
class(veg1$area)
[1] "numeric"
apicum<- veg1 %>% subset(formacao=="apicum")%>% select(area, mun)

savana<- veg1 %>% subset(formacao=="savana") %>% select(area, mun)

savbar<-ggplot(savana, aes(x=area, y=mun, fill=mun)) + 
  geom_bar(stat="identity", position=position_dodge())+
  theme_bw()+
  labs(x="Área de savana (km²)", y= "Município")+
       theme(legend.position = "none")
savbar

apibar<-ggplot(apicum, aes(x=area, y=mun, fill=mun)) + 
  geom_bar(stat="identity", position=position_dodge())+
  theme_bw()+
  labs(x="Área de apicum (km²)", y= "Município")+
     theme(legend.position = "none")

apibar

Criar um painel

sav_api<-savbar/apibar+ plot_annotation(
  title = 'Área de duas formações vegetais no PR',
  subtitle = 'Área em km² de Savana e Apicum nos municípios em que as formações ocorrem',
  caption = 'Dados obtidos através do Mapbiomas',
  theme = theme(plot.title = element_markdown(size=15,family="Arial",face="italic", colour = "black"),
                plot.subtitle = element_markdown(size=10,hjust=0,family="Palatino", color = "darkgreen"),
                plot.caption = element_markdown(size=10,hjust=0,family="Courier"),
                plot.background = element_rect(fill="lightgreen", color=NA)))

sav_api
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

  • Visualização de Mapas

    Pacote geobr tem vários dados para o Brasil!

library(geobr)  
Warning: package 'geobr' was built under R version 4.2.3
Loading required namespace: sf
datasets <- list_geobr() 

muni_PR <- read_municipality(code_muni = "PR", 
  year=2020, showProgress = FALSE)
Using year 2020
ggplot() +
  geom_sf(data=muni_PR, fill="lightgrey", color="black", show.legend = FALSE) +
  theme_classic()

Destacando no mapa os municípios com a maior área de savana (Juaguariaíva) e apicum (Paranaguá).

datasets <- list_geobr() 

muni_PR <- read_municipality(code_muni = "PR", 
                             year=2020, showProgress = FALSE)
Using year 2020
muni_all<-read_municipality(code_muni = "all",
                            year=2020, showProgress = FALSE)
Using year 2020
Problem connecting to data server. Please try again in a few minutes.
jagua <- read_municipality(code_muni = 4112009, 
                           year=2020, showProgress = FALSE)
Using year 2020
library(jpeg)

img <- readJPEG("campos.jpg")
imgr <- as.raster(img)

mapa_jagua2 <- ggplot()+ 
geom_sf(data=muni_all, fill="lightyellow", color="lightgray", show.legend = FALSE) +
  geom_sf(data=muni_PR, fill="lightyellow", color="black", show.legend = FALSE) +
  geom_sf(data=jagua , fill="orange", color="orange", show.legend = FALSE)+
  labs(title="Juaguariaíva, PR")+
  coord_sf(xlim= c(-55,-48), ylim=c(-27,-22))+
  annotation_raster(imgr, xmin=-49.1, xmax=-45, ymin=-24.5, ymax=-23)+
  theme_minimal()+
  theme(legend.position = c(0.8,0.9), legend.background = element_blank(), legend.text=element_text(size=10),
        legend.title = element_blank(),
        legend.key.size = unit(0.4, "cm"),
        panel.grid.major = element_line(color = "gray", linetype = "dashed", size = 0.5), 
        panel.background = element_rect(fill = "lightblue"),
        axis.title = element_blank())
Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
mapa_jagua2

img2 <- readJPEG("apicum.jpg")
imgr2 <- as.raster(img2)

paran <- read_municipality(code_muni = 4118204, 
                           year=2020, showProgress = FALSE)
Using year 2020
mapa_paran <- ggplot()+ 
  geom_sf(data=muni_all, fill="lightyellow", color="lightgray", show.legend = FALSE) +
  geom_sf(data=muni_PR, fill="lightyellow", color="black", show.legend = FALSE) +
  geom_sf(data=paran, fill="orange", color="orange", show.legend = FALSE)+
  labs(title="Paranaguá, PR")+
  coord_sf(xlim= c(-55,-48), ylim=c(-27,-22))+
  annotation_raster(imgr2, xmin=-49.1, xmax=-45, ymin=-24.5, ymax=-23)+
  theme_minimal()+
  theme(legend.position = c(0.8,0.9), legend.background = element_blank(), legend.text=element_text(size=10),
        legend.title = element_blank(),
        legend.key.size = unit(0.4, "cm"),
        panel.grid.major = element_line(color = "gray", linetype = "dashed", size = 0.5), 
        panel.background = element_rect(fill = "lightblue"),
        axis.title = element_blank())

mapa_paran

Tip

Painel dos gráficos de barra e dos mapas

apicum<-apibar/mapa_paran + plot_annotation(
  title = 'Área de Apicum no PR',
  subtitle = 'Paranaguá é o município com maior área no estado',
  caption = 'Dados obtidos através do Mapbiomas',
  theme = theme(plot.title = element_markdown(size=15,family="Arial",face="italic", colour = "black"),
                plot.subtitle = element_markdown(size=10,hjust=0,family="Palatino", color = "blue"),
                plot.caption = element_markdown(size=10,hjust=0,family="Courier"),
                plot.background = element_rect(fill="lightsteelblue", color=NA)))

apicum
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

savanaa<-savbar/mapa_jagua2+ plot_annotation(
  title = 'Área de Savana no PR',
  subtitle = 'Jaguariaíva é o município com maior área no estado',
  caption = 'Dados obtidos através do Mapbiomas',
  theme = theme(plot.title = element_markdown(size=15,family="Arial",face="italic", colour = "black"),
                plot.subtitle = element_markdown(size=10,hjust=0,family="Palatino", color = "blue"),
                plot.caption = element_markdown(size=10,hjust=0,family="Courier"),
                plot.background = element_rect(fill="lightsteelblue", color=NA)))

savanaa
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database

Aprendi muitas coisas, mas ainda não sei como colocar tudo em um script que funcione hehehe