Import/Export by Area
Graphs of of import/export by area using FAO data
Prepare Data
# devtools::install_github("derekmichaelwright/agData")
library(agData)
library(gganimate)
#
myCaption <- "derekmichaelwright.github.io/dblogr/ | Data: FAOSTAT"
Plot by Area Function
xx <- agData_FAO_Trade_Quantity %>%
filter(Area == "Canada")
# Create plotting functions
ggIEA <- function(myArea = "Canada") {
# Prep data
myItems <- c("Cereals", "Oilseeds", "Pulses", "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_grid(Item ~ ., scales = "free_y") +
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)
}
Plot by Year Function
#
ggIEAY <- function(myArea = "Canada", myYear = 2017) {
# Prep data
myItems <- c("Cereals", "Oilseeds", "Pulses", "Fruit", "Vegetables")
myColors <- c("darkgreen", "darkgoldenrod2", "darkred", "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 = 5, 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)
}
Animation function
ggIEAA <- function(myArea = "Canada") {
# Prep data
myItems <- c("Cereals", "Oilseeds", "Pulses", "Fruit", "Vegetables")
myColors <- c("darkgreen", "darkgoldenrod2", "darkred", "steelblue", "purple4")
xx <- agData_FAO_Trade_Quantity %>%
filter(Area == myArea, Item %in% myItems) %>%
mutate(Item = factor(Item, levels = myItems))
# Plot
ggplot(xx, aes(x = Item, y = Value / 1000000000, fill = Item)) +
geom_bar(stat = "identity", color = "black", alpha = 0.7) +
facet_grid(. ~ Measurement) +
scale_fill_manual(values = myColors) +
theme_agData(legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Canada - {round(frame_time)}", y = "Billion Tonnes",
x = NULL, caption = myCaption) +
transition_time(Year)
}
Import/Export
Canada
Russia
Ukraine
Germany
2019
Canada
mp <- ggIEAY(myArea = "Canada", myYear = 2019)
ggsave("import_export_2_canada.png", mp, width = 6, height = 4)
USA
mp <- ggIEAY(myArea = "USA", myYear = 2019)
ggsave("import_export_2_usa.png", mp, width = 6, height = 3.75)
Russia
mp <- ggIEAY(myArea = "Russia", myYear = 2019)
ggsave("import_export_2_russia.png", mp, width = 6, height = 3.75)
Ukraine
mp <- ggIEAY(myArea = "Ukraine", myYear = 2019)
ggsave("import_export_2_ukraine.png", mp, width = 6, height = 3.75)