Aula16

Author

Arlam

Mapas no R

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'dplyr' 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 conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)
library(ggplot2)
library(r4pde)
library(rnaturalearth)
Warning: package 'rnaturalearth' was built under R version 4.2.3
library(rnaturalearthhires)

Instalar pacote do github:

remotes::install_github("ropensci/rnaturalearthhires")
Skipping install of 'rnaturalearthhires' from a github remote, the SHA1 (c3785a8c) has not changed since last install.
  Use `force = TRUE` to force installation

Conjunto de dados: Para plotar o mapa do pais, usa-se a função ne_countries

sbr <- RustSoybean

BRA <- ne_countries(country = "Brazil", 
                    returnclass = "sf")
ggplot(BRA) +
geom_sf(fill = "white")

Para plotar os estados:

BRA <- ne_states(country = "Brazil", 
                    returnclass = "sf")
ggplot(BRA) +
geom_sf(color = "white",
          fill = "darkgreen") +
  theme_void()

Para selecionar um estado: Para

BRA <- ne_states(country = "Brazil", 
                    returnclass = "sf")
MG <- BRA |> filter(name_en == "Minas Gerais")
ggplot(BRA) +
geom_sf(color = "black",
          fill = "white") +
  geom_sf(data = MG, color = "black",
            fill = "green")

Para inserir os pontos especificos dos dados (latitude e longitude): Para plotar os pontos, precisa-se das coordenadas de onde foram coletados os pontos. Ex.: pontos de coleta - precisa-se coletar as coordenadas para plotar em um mapa (no caso de ser só o municipio, pode pegar na internet as coordenadas).

BRA <- ne_states(country = "Brazil", 
                    returnclass = "sf")
MG <- BRA |> filter(name_en == "Minas Gerais")
ggplot(BRA) +
geom_sf(color = "black",
          fill = "white") +
  geom_point(data = sbr, aes(longitude, latitude), alpha = 0.5)

Para separar a data em dia, mês e ano:

sbr2 <- sbr |>
  separate(planting, into = 
             c("year", "month", "day"), sep = "-", remove = FALSE)

BRA <- ne_states(country = "Brazil", 
                    returnclass = "sf")
MG <- BRA |> filter(name_en == "Minas Gerais")
ggplot(BRA) +
geom_sf(color = "black",
          fill = "white") +
  geom_point(data = sbr2,
             aes(longitude, latitude, color = year), alpha = 0.5)+
  facet_wrap(~year)+
  theme_void()

Como inserir a rosa dos ventos e a escala no mapa:

library(ggspatial)
Warning: package 'ggspatial' was built under R version 4.2.3
ggplot(BRA) +
  annotation_north_arrow(location = "bl")+
  annotation_scale(location = "br")+
  geom_sf(color = "black",
          fill = "white") +
  geom_point(data = sbr2,
             aes(longitude, latitude, color = year, size = severity), alpha = 0.5)+
  labs(color = "Planting Year")+
  theme_minimal()+
  theme(legend.position = "right")
Scale on map varies by more than 10%, scale bar may be inaccurate

Malha de municipios: Pega o arquivo shape, que baixado dos municipios em um site do IBJE ou outro confiável. Usa o pacote rgdal e executa os eeguintes comandos:

library(ggplot2)
ggplot()+
   geom_sf(data = MG, fill = "red")+
     labs(title = "ESTADO DE MINAS GERAIS")