dblogr/

Crop Import/Export

Graphs of of crop import/export using FAO data


Prepare Data

# devtools::install_github("derekmichaelwright/agData")
library(agData)
library(gganimate)
#
myCaption <- "derekmichaelwright.github.io/dblogr/ | Data: FAOSTAT"

Plot Crop Function

# Create plotting functions
ggIEC <- function(myItem = "Wheat", myYear = 2019) {
  # Prep data
  myAreas <- select(agData_FAO_Country_Table, Area=Country, Region) %>% 
    filter(!duplicated(Area))
  myColors <- c("darkgreen", "darkblue", "darkred", 
              "darkgoldenrod2", "darkslategrey")
  xx <- agData_FAO_Trade_Quantity %>% 
    filter(Area %in% unique(agData_FAO_Country_Table$Country),
           Item == myItem, Year == myYear) %>%
    left_join(myAreas, by = "Area") %>%
    spread(Measurement, Value) %>%
    mutate(Net = `Export Quantity` - `Import Quantity`) %>%
    filter(!is.na(Net)) %>% 
    arrange(Net) %>%
    mutate(Area = factor(Area, levels = rev(unique(.$Area))))
  x1 <- top_n(xx, 10, Net) %>% 
    mutate(Label = "Top 10")
  x2 <- top_n(xx %>% mutate(Net = -Net), 10, Net) %>% 
    mutate(Label = "Bottom 10", Net = -Net)
  xx <- bind_rows(x1, x2) %>% 
    mutate(Label = factor(Label, levels = c("Top 10", "Bottom 10")))
  # Plot
  ggplot(xx, aes(x = Area, y = Net / 1000000, fill = Region)) + 
    geom_bar(stat = "identity", color = "black", alpha = 0.7) +
    facet_grid(. ~ Label, scales = "free_x") +
    scale_fill_manual(name = NULL, values = myColors) +
    theme_agData(axis.text.x = element_text(angle = 45, hjust = 1)) + 
    labs(title = myItem, subtitle = "Net = Export - Import", 
         y = "Million Tonnes", x = NULL, caption = myCaption)
}

2019

Wheat

mp <- ggIEC(myItem = "Wheat", myYear = 2019)
ggsave("import_export_wheat.png", mp, width = 6, height = 3.75)

Maize

mp <- ggIEC(myItem = "Maize (corn)", myYear = 2019)
ggsave("import_export_maize.png", mp, width = 6, height = 3.75)

Rice

mp <- ggIEC(myItem = "Rice", myYear = 2019)
ggsave("import_export_rice.png", mp, width = 6, height = 3.75)

Soybeans

mp <- ggIEC(myItem = "Soya beans", myYear = 2019)
ggsave("import_export_soybeans.png", mp, width = 6, height = 3.75)

Sorghum

mp <- ggIEC(myItem = "Sorghum", myYear = 2019)
ggsave("import_export_sorghum.png", mp, width = 6, height = 3.75)

Rapeseed

mp <- ggIEC(myItem = "Rapeseed or canola oil, crude", myYear = 2019)
ggsave("import_export_rapeseed.png", mp, width = 6, height = 3.75)

Beans

mp <- ggIEC(myItem = "Beans, dry", myYear = 2019)
ggsave("import_export_beans.png", mp, width = 6, height = 3.75)

Chickpeas

mp <- ggIEC(myItem = "Chick peas, dry", myYear = 2019)
ggsave("import_export_chickpeas.png", mp, width = 6, height = 3.75)

Lentils

mp <- ggIEC(myItem = "Lentils, dry", myYear = 2019)
ggsave("import_export_lentils.png", mp, width = 6, height = 3.75)

Peas

mp <- ggIEC(myItem = "Peas, dry", myYear = 2019)
ggsave("import_export_peas.png", mp, width = 6, height = 3.75)


dblogr/


© Derek Michael Wright