Exercício - Mapas: Respostas

Author

Marina Scalon

Chegou a hora de fixar o nosso aprendizado sobre a aula de mapas!

Para isso, vamos fazer dois exercícios independentes e usar diferentes conjunto de dados do tidytuesday

Exercício 1

Utilizando qualquer recurso apresentado em aula, faça um mapa destacando ou mostrando o estado e município que você nasceu.

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.0.5
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.4.2     v purrr   1.0.1
v tibble  3.2.1     v dplyr   1.1.2
v tidyr   1.3.0     v stringr 1.5.0
v readr   2.1.4     v forcats 1.0.0
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(geobr)
Loading required namespace: sf
datasets <- list_geobr() 

muni_uberaba <- read_municipality(code_muni = 3170107, 
                             year=2020, showProgress = FALSE)
Using year 2020
state_all <- read_state(code_state = "all", 
                              year=2020, showProgress = FALSE)
Using year 2020
mapa_br <- ggplot()+ borders("world", fill = "white", colour = "gray")+
  geom_sf(data = state_all, fill="white", color="darkgray", show.legend = FALSE)+
  geom_sf(data = filter(state_all, name_state=="Minas Gerais"), fill="lightgray", color="darkgray", show.legend = FALSE)+
  coord_sf(xlim= c(-80,-30), ylim=c(-40,10))+
  theme_void()
mapa_br

library(png)

uberaba <- readPNG("uberabasuchus.png")

mapa_uberaba <- ggplot()+ 
  geom_sf(data = filter(state_all, name_state=="Minas Gerais"), fill="lightgray", color="darkgray", show.legend = FALSE)+
  geom_sf(data = muni_uberaba, fill="red", color="darkgray", show.legend = FALSE)+
  labs(title="Uberaba, MG")+
  theme_void()+
  annotation_raster(uberaba, xmin=-51, xmax=-48, ymin=-17, ymax=-15)

mapa_uberaba

mapa_br

library(cowplot)
Warning: package 'cowplot' was built under R version 4.0.5
final <- ggdraw()+
  coord_equal(xlim = c(0, 40), ylim = c(0, 40), expand = FALSE) +
  annotation_custom(ggplotGrob(mapa_uberaba), xmin = 20, xmax = 40, ymin = 0, ymax = 30) +
  annotation_custom(ggplotGrob(mapa_br), xmin = 0, xmax = 25, ymin = 10, ymax = 40)
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.
final

Exercício 2

Utilizando os recursos do banco de dados do GBIF e utilizando o pacote rgbif, apresente a localização de alguma espécie que você estude ou goste. Lembre-se que se a distribuição é muito ampla, os dados vão demorar a carregar, então limitar o número de observações ou escolher uma espécie de distribuição restrita pode ser uma opção!

library(rgbif)
Warning: package 'rgbif' was built under R version 4.0.5
ocorrencia <- occ_search(scientificName = "Amyema sanguinea", hasCoordinate = T)

localizacao <- as.data.frame(ocorrencia$data) 

library(jpeg)
img <- readJPEG("amyema.jpg")
imgr <- as.raster(img)

library(ggmap)
Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
Please cite ggmap if you use it! See citation("ggmap") for details.

Attaching package: 'ggmap'
The following object is masked from 'package:cowplot':

    theme_nothing
library(maps)
Warning: package 'maps' was built under R version 4.0.5

Attaching package: 'maps'
The following object is masked from 'package:purrr':

    map
ggplot()+
  borders("world", region="Australia", fill="lightgray")+
  geom_point(data = localizacao, aes(x= decimalLongitude, y = decimalLatitude), colour = "red", size=2, alpha=0.3)+
  coord_fixed(ylim=c(-45,-10))+
  theme(panel.background = element_rect(fill="black"), axis.text = element_blank(), axis.ticks = element_blank(),axis.title = element_blank(), panel.grid = element_blank()) + 
  annotation_raster(imgr, 160, 150, -20, -10, interpolate = FALSE)

Exercício 3

Utilizando os dados de Numbats na Australia, faça um mapa de ocorrência nos diferentes anos.(um mapa ou vários mapas)

numbats <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-07/numbats.csv')
Rows: 805 Columns: 16-- Column specification --------------------------------------------------------
Delimiter: ","
chr  (6): scientificName, taxonConceptID, recordID, dataResourceName, month,...
dbl  (7): decimalLatitude, decimalLongitude, year, hour, prcp, tmax, tmin
lgl  (1): dryandra
dttm (1): eventDate
date (1): day
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
mapa_aus<-map_data("world", region="Australia")

ggplot(data=numbats, aes(x=decimalLongitude, y=decimalLatitude))+
  coord_equal()+
  geom_map(data=mapa_aus, map=mapa_aus, aes(x=long, y=lat, map_id=region), fill="lightgray", color="black")+
  stat_density2d(aes(fill = ..level..), alpha = .5,
                 geom = "polygon", data = numbats)+
  facet_wrap(~year)
Warning in geom_map(data = mapa_aus, map = mapa_aus, aes(x = long, y = lat, :
Ignoring unknown aesthetics: x and y
Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.

Warning: Please use `after_stat(level)` instead.
Warning: Removed 83 rows containing non-finite values (`stat_density2d()`).
Warning: Computation failed in `stat_density2d()`

Warning: Computation failed in `stat_density2d()`

Warning: Computation failed in `stat_density2d()`
Warning: Computation failed in `stat_density2d()`

Warning: Computation failed in `stat_density2d()`
Warning: Computation failed in `stat_density2d()`
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf
Warning: `stat_contour()`: Zero contours were generated
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf

numbats2 <- numbats %>% mutate(decades = case_when(year <= 2000 ~ "1956-2000",
                                                   year <= 2010 ~ "2001-2010",
                                                   year <= 2023 ~ "2011-2023"))

ggplot(data=numbats2, aes(x=decimalLongitude, y=decimalLatitude))+
  coord_equal()+
  geom_map(data=mapa_aus, map=mapa_aus, aes(x=long, y=lat, map_id=region), fill="lightgray", color="black")+
  geom_point(aes(color=decades))+
  scale_color_manual(values = c("gold","pink","lightblue"), na.value = NA)+
  facet_wrap(~decades, drop=T)
Warning in geom_map(data = mapa_aus, map = mapa_aus, aes(x = long, y = lat, :
Ignoring unknown aesthetics: x and y
Warning: Removed 268 rows containing missing values (`geom_point()`).

ggplot(data=filter(numbats2, (!is.na(decades))), aes(x=decimalLongitude, y=decimalLatitude))+
  geom_map(data=mapa_aus, map=mapa_aus, aes(x=long, y=lat, map_id=region), fill="#FBF4FE", color="black")+
  geom_point(aes(color=decades))+
  scale_color_manual(values = c("gold","pink","lightblue"))+
  facet_wrap(~decades, drop=T)+
  theme_void()+
  labs(title = "Ocorrência de numbats na Australia entre 1986-2023")+
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        legend.position = "none",
        plot.background = element_rect(fill="#DDCDE5", color=NA),
        panel.background = element_rect(fill="#DDCDE5", color=NA),
        strip.background = element_rect(fill="#DDCDE5", color=NA),
        plot.title = element_text(size = 18, margin=margin(2,2,30,1)))
Warning in geom_map(data = mapa_aus, map = mapa_aus, aes(x = long, y = lat, :
Ignoring unknown aesthetics: x and y
Warning: Removed 15 rows containing missing values (`geom_point()`).

# tentei usar o gganimate
# library(gganimate)

# ggplot(data=filter(numbats2, (!is.na(decades))), aes(x=decimalLongitude, y=decimalLatitude))+
#   geom_map(data=mapa_aus, map=mapa_aus, aes(x=long, y=lat, map_id=region), fill="#FBF4FE", color="black")+
#   geom_point(aes(color=decades))+
#   scale_color_manual(values = c("gold","pink","lightblue"))+
#   theme_void()+
#   labs(title = "Ocorrência de numbats na Australia entre 1986-2023")+
#   theme(axis.title = element_blank(),
#         axis.text = element_blank(),
#         legend.position = "none",
#         plot.background = element_rect(fill="#DDCDE5", color=NA),
#         panel.background = element_rect(fill="#DDCDE5", color=NA),
#         plot.title = element_text(size = 18, margin=margin(2,2,30,1)))+
#   transition_states(decades)

Exercício 4

Utilizando os dados de Culturas agrícolas do passado faça um mapa mostrando a ocorrência das diferentes famílias de culturas agrícolas. (um mapa ou vários mapas)

founder_crops <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-04-18/founder_crops.csv')
Rows: 4490 Columns: 24-- Column specification --------------------------------------------------------
Delimiter: ","
chr (18): source, source_id, source_site_name, site_name, phase, phase_descr...
dbl  (6): latitude, longitude, age_start, age_end, n, prop
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
ggplot()+ borders("world", fill = "lightgray", colour = "gray")+
  geom_point(data = filter(founder_crops, (!is.na(category))), aes(x= longitude, y= latitude, color=category), alpha=0.5)+
  coord_fixed(xlim=c(20, 60), ylim=c(20, 50))+
  facet_wrap(~category)+
  theme_void()+
  theme(legend.position = "none")