Skip to content

Instantly share code, notes, and snippets.

@kguidonimartins
Created October 30, 2019 03:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kguidonimartins/31859e480d5ba4163510f50f28e9171b to your computer and use it in GitHub Desktop.
Save kguidonimartins/31859e480d5ba4163510f50f28e9171b to your computer and use it in GitHub Desktop.
Script para obtenção, limpeza e visualização dos dados sobre a presença de oléo em praias do Nordeste brasileiro
############################################################
# #
# instalação dos pacotes #
# #
############################################################
# ipak function: install and load multiple R packages.
# Check to see if packages are installed.
# Install them if they are not, then load them into the R session.
# Forked from: https://gist.github.com/stevenworthington/3178163
ipak <- function(pkg) {
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg)){
install.packages(new.pkg, dependencies = TRUE)
}
suppressPackageStartupMessages(sapply(pkg, require, character.only = TRUE))
}
ipak(c("tidyverse", "tidylog", "readxl", "here", "biogeo", "plotly"))
theme_set(theme_light(base_size = 14))
############################################################
# #
# download dos dados #
# #
############################################################
link <- "https://www.ibama.gov.br/phocadownload/notas/2019/2019-10-28_LOCALIDADES_AFETADAS.xlsx"
get_data <- function(link_data, dest_folder){
if (!dir.exists(dest_folder)){
dir.create(dest_folder)
}
dest_and_name <- paste0(dest_folder, "/", basename(link_data))
if (!file.exists(dest_and_name)){
download.file(url = link_data, destfile = dest_and_name)
} else {
message("Arquivo ", dest_and_name, " já existe")
}
}
get_data(
link_data = link,
dest_folder = "derramento-de-oleo"
)
############################################################
# #
# leitura e limpeza dos dados #
# #
############################################################
df <-
read_xlsx(
path = here("derramento-de-oleo", "2019-10-28_LOCALIDADES_AFETADAS.xlsx")
) %>%
rename_all(str_to_lower)
clean_coord <- function(coord){
coord <- iconv(coord, "utf-8", "ascii", sub="") %>% {
gsub(pattern = "(')|(\")", replacement = "", x = .)
}
return(coord)
}
df_clean <-
df %>%
mutate(
latitude = clean_coord(latitude),
longitude = clean_coord(longitude)
) %>%
separate(
col = latitude,
into = c("lat_graus", "lat_minutos", "lat_segundos", "lat_letra"),
sep = " ",
convert = TRUE
) %>%
separate(
col = longitude,
into = c("long_graus", "long_minutos", "long_segundos", "long_letra"),
sep = " ",
convert = TRUE
) %>%
mutate(
latitude = dms2dd(
dd = lat_graus,
mm = lat_minutos,
ss = lat_segundos,
ns = lat_letra
),
longitude = dms2dd(
dd = long_graus,
mm = long_minutos,
ss = long_segundos,
ns = long_letra
)
) %>%
select(-starts_with("lat_"), -starts_with("long_"))
############################################################
# #
# visualização #
# #
############################################################
df_clean %>%
group_by(estado) %>%
count() %>%
ungroup() %>%
mutate(estado = fct_reorder(estado, n)) %>%
ggplot(aes(x = estado, y = n)) +
geom_col() +
coord_flip() +
labs(
x = "",
y = "Número de registros",
title = "Registros por Estado",
caption = "Fonte: https://www.ibama.gov.br/notas/2047-manchas-de-oleo-no-litoral-do-nordeste"
)
############################################################
# #
# mapa #
# #
############################################################
shape_brasil <- borders(
database = "world",
regions = "Brazil",
fill = "white",
colour = "grey90"
)
mapa_oleo <-
df_clean %>%
ggplot() +
shape_brasil +
theme_bw() +
labs(
x = "Longitude",
y = "Latitude"
) +
theme(
panel.border = element_blank(),
panel.grid.major = element_line(colour = "grey80"),
panel.grid.minor = element_blank()
) +
coord_map() +
geom_point(
aes(
x = longitude,
y = latitude,
colour = status
),
alpha = 0.5,
size = 3
)
# mapa estático
mapa_oleo
# mapa estático por status
mapa_oleo +
facet_wrap( ~status)
############################################################
# #
# mapa interativo #
# #
############################################################
ggplotly(mapa_oleo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment