dblogr/

Import/Export by Area

Graphs of of import/export by area using FAO data


Prepare Data

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

Import/Export

Ploting function

xx <- agData_FAO_Trade_Quantity %>%
  filter(Area == "Canada")
# Create plotting functions
ggIEA <- function(myArea = "Canada") {
  # Prep data
  myItems <- c("Cereals", "Pulses", "Oilseeds", "Roots and Tubers", "Fruit", "Vegetables")
  myColors <- c("steelblue", "darkred")
  xx <- agData_FAO_Trade_Quantity %>%
    filter(Area == myArea, Item %in% myItems) %>%
    mutate(Measurment = gsub(" Quantity", "", Measurement),
           Item = factor(Item, levels = myItems))
  # Plot
  ggplot(xx, aes(x = Year, y = Value / 1000000, fill = Measurement, 
                 group = Measurement)) +
    geom_bar(stat = "identity", position = "dodge", 
             color = "black", lwd = 0.2, alpha = 0.7) +
    facet_wrap(Item ~ ., scales = "free_y", ncol = 2) +
    scale_fill_manual(name = NULL, values = myColors) +
    scale_x_continuous(breaks = seq(1960, 2020, by = 10)) +
    theme_agData(legend.position = "bottom", 
                 axis.text.x = element_text(angle = 45, hjust = 1)) +
    labs(title = myArea, y = "Million Tonnes", 
         x = NULL, caption = myCaption)
}

Canada

mp <- ggIEA(myArea = "Canada")
ggsave("import_export_1_canada.png", mp, width = 10, height = 6)

USA

mp <- ggIEA(myArea = "USA")
ggsave("import_export_1_usa.png", mp, width = 10, height = 6)

Mexico

mp <- ggIEA(myArea = "Mexico")
ggsave("import_export_1_mexico.png", mp, width = 10, height = 6)

Russia

mp <- ggIEA(myArea = "Russia")
ggsave("import_export_1_russia.png", mp, width = 10, height = 6)

Ukraine

mp <- ggIEA(myArea = "Ukraine")
ggsave("import_export_1_ukraine.png", mp, width = 10, height = 6)

Germany

mp <- ggIEA(myArea = "Germany")
ggsave("import_export_1_germany.png", mp, width = 10, height = 6)

Netherlands

mp <- ggIEA(myArea = "Netherlands")
ggsave("import_export_1_netherlands.png", mp, width = 10, height = 6)

India

mp <- ggIEA(myArea = "India")
ggsave("import_export_1_india.png", mp, width = 10, height = 6)

Africa

mp <- ggIEA(myArea = "Africa")
ggsave("import_export_1_africa.png", mp, width = 10, height = 6)

Europe

mp <- ggIEA(myArea = "Europe")
ggsave("import_export_1_europe.png", mp, width = 10, height = 6)

Asia

mp <- ggIEA(myArea = "Asia")
ggsave("import_export_1_asia.png", mp, width = 10, height = 6)

Northern America

mp <- ggIEA(myArea = "Northern America")
ggsave("import_export_1_northernamerica.png", mp, width = 10, height = 6)

Southern America

mp <- ggIEA(myArea = "South America")
ggsave("import_export_1_southamerica.png", mp, width = 10, height = 6)

2019

Ploting function

#
ggIEAY <- function(myArea = "Canada", myYear = 2017) {
  # Prep data
  myItems <- c("Cereals", "Pulses", "Oilseeds", "Roots and Tubers", "Fruit", "Vegetables")
  myColors <- c("darkgreen", "darkred", "darkgoldenrod2", "darkblue", "steelblue", "purple4")
  xx <- agData_FAO_Trade_Quantity %>%
    filter(Area == myArea, Year == myYear, Item %in% myItems) %>%
    mutate(Measurment = gsub(" Quantity", "", Measurement),
           Item = factor(Item, levels = myItems))
  # Plot
  ggplot(xx, aes(x = Measurement, y = Value / 1000000, 
                 fill = Item, alpha = Measurement)) +
    geom_bar(stat = "identity", color = "black") +
    facet_wrap(. ~ Item, ncol = 3, scale = "free_y") +
    scale_fill_manual(values = myColors) +
    scale_alpha_manual(values = c(0.5,0.8)) +
    theme_agData(legend.position = "none", 
                 axis.text.x = element_text(angle = 45, hjust = 1)) +
    labs(title = paste(myArea, myYear), y = "Million Tonnes", 
         x = NULL, caption = myCaption)
}

Canada

mp <- ggIEAY(myArea = "Canada", myYear = 2019)
ggsave("import_export_2_canada.png", mp, width = 5, height = 5)

USA

mp <- ggIEAY(myArea = "USA", myYear = 2019)
ggsave("import_export_2_usa.png", mp, width = 5, height = 5)

Russia

mp <- ggIEAY(myArea = "Russia", myYear = 2019)
ggsave("import_export_2_russia.png", mp, width = 5, height = 5)

Ukraine

mp <- ggIEAY(myArea = "Ukraine", myYear = 2019)
ggsave("import_export_2_ukraine.png", mp, width = 5, height = 5)

Germany

mp <- ggIEAY(myArea = "Germany", myYear = 2019)
ggsave("import_export_2_germany.png", mp, width = 5, height = 5)

India

mp <- ggIEAY(myArea = "India", myYear = 2019)
ggsave("import_export_2_india.png", mp, width = 5, height = 5)

Africa

mp <- ggIEAY(myArea = "Africa")
ggsave("import_export_2_africa.png", mp, width = 5, height = 5)

Europe

mp <- ggIEAY(myArea = "Europe")
ggsave("import_export_2_europe.png", mp, width = 5, height = 5)

Asia

mp <- ggIEAY(myArea = "Asia")
ggsave("import_export_2_asia.png", mp, width = 5, height = 5)

Northern America

mp <- ggIEAY(myArea = "Northern America")
ggsave("import_export_2_northernamerica.png", mp, width = 5, height = 5)

Southern America

mp <- ggIEAY(myArea = "South America")
ggsave("import_export_2_southamerica.png", mp, width = 5, height = 5)


dblogr/


© Derek Michael Wright