1 Executive summary

library(tidyverse)
library(lubridate)
library(plotly)


theme_set(theme_bw(18) +
            theme(legend.position = "bottom"))

2 Loading data

filenames = c("forest.csv", "forest_area.csv", "brazil_loss.csv",
                "soybean_use.csv", "vegetable_oil.csv")

online_files = paste0("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-04-06/",
              filenames)

purrr::map2(
  .x = filenames,
  .y = online_files,
  .f = ~ download.file(url = .y, destfile = .x)
)

# brazil_loss <- readr::read_csv('brazil_loss.csv')
# soybean_use <- readr::read_csv('soybean_use.csv')
forest <- readr::read_csv('forest.csv')
# forest_area <- readr::read_csv('forest_area.csv')
# vegetable_oil <- readr::read_csv('vegetable_oil.csv')
subforest = forest %>% 
  dplyr::filter(net_forest_conversion != 0) %>% 
  tidyr::complete(expand(., nesting(entity, code), year), 
                  fill = list(net_forest_conversion = NA)) %>% 
  dplyr::mutate(net_forest_conversion_log10 = sign(net_forest_conversion)*log10(abs(net_forest_conversion)))
# subforest %>% glimpse()
write_csv(x = subforest, file = "./subforest.csv")

3 Plotly visualisation with slider

fig <- plot_ly(
  subforest,
  type = 'choropleth',
  locations = ~code,
  z = ~net_forest_conversion_log10,
  text = ~entity,
  frame = ~year,
  colors = "RdYlGn") %>% 
  layout(
    geo = list(projection = list(type = "orthographic")),
    showlegend = FALSE)

fig