dblogr/

Global Food Security

Graphs related to global food security using FAO data


Prepare Data

# devtools::install_github("derekmichaelwright/agData")
library(agData)
library(gganimate)
# Prep Data
myCaption <- "derekmichaelwright.github.io/dblogr/agdata/global_food_security | Data: FAOSTAT"
myRegions <- unique(agData_FAO_Country_Table$Region)
mySubRegions <- unique(agData_FAO_Country_Table$SubRegion)
myAreas <- c("World", myRegions, mySubRegions)
myItems1 <- c("Oilcrops, Oil Equivalent", "Treenuts, Total", 
              "Vegetables Primary", "Fruit Primary", "Sugar Crops Primary",
              "Cereals, primary", "Pulses, Total", "Roots and Tubers, Total")
myItems2 <- c("Population", "Oilcrops", "Treenuts", "Vegtables", "Fruits",
              "Sugar", "Cereals", "Pulses", "Tubers")
myColors1 <- c("purple4",  "darkgoldenrod2", "red", "steelblue", "darkblue",
              "grey20", "darkgreen", "darkred", "darkslategray")
myColors2 <- c("purple4",  "darkgreen", "darkred", "steelblue")
myColors3 <- c("darkred", "darkgreen", "darkorange", "darkblue", "steelblue")
dd <- agData_FAO_Crops2 %>% select(-Unit) %>%
  filter(Area %in% myAreas, Item %in% myItems1) %>%
  spread(Measurement, Value)
pp <- agData_FAO_Population %>%
  filter(Measurement == "Total") %>%
  select(Area, Year, Population=Value)
dd <- left_join(dd, pp, by = c("Area", "Year")) %>%
  left_join(agData_FAO_Region_Table, by = c("Area"="SubRegion")) %>%
  mutate(TonnesPerPerson = Production / Population,
         Item = mv(Item, myItems1, myItems2[-1]),
         Area = mv(Area, "Australia and New Zealand", "Au and NZ"),
         Population_R1961 = NA, Production_R1961 = NA,
         Area_R1961 = NA, Yield_R1961 = NA)
for(i in unique(dd$Item)) {
  for(j in unique(dd$Area)) {
    # Population
    p1961 <- dd %>% 
      filter(Area == j, Item == i, Year == 1961) %>% pull(Population)
    dd <- dd %>% mutate(Population_R1961 = ifelse(Area == j & Item == i, 
              100*(Population - p1961) / p1961, Population_R1961))
    # Production
    p1961 <- dd %>% 
      filter(Area == j, Item == i, Year == 1961) %>% pull(Production)
    dd <- dd %>% mutate(Production_R1961 = ifelse(Area == j & Item == i, 
              100*(Production - p1961) / p1961, Production_R1961))
    # Area
    p1961 <- dd %>% 
      filter(Area == j, Item == i, Year == 1961) %>% pull(`Area Harvested`)
    dd <- dd %>% mutate(Area_R1961 = ifelse(Area == j & Item == i, 
              100*(`Area Harvested` - p1961) / p1961, Area_R1961))
    # Yield
    p1961 <- dd %>% 
      filter(Area == j, Item == i, Year == 1961) %>% pull(Yield)
    dd <- dd %>% mutate(Yield_R1961 = ifelse(Area == j & Item == i, 
              100*(Yield - p1961) / p1961, Yield_R1961))
  }
}

Relative to Population

Crops

# Prep data
xx <- dd %>% filter(Area == "World")
# Plot
mp <- ggplot(xx, aes(x = Year, )) +
  geom_line(aes(y = Population_R1961, color = "Population"), 
            size = 1.75, alpha = 0.8) +
  geom_line(aes(y = Production_R1961, color = Item), 
            size = 0.75, alpha = 0.7) +
  scale_x_continuous(breaks = seq(1960, 2020, by = 10)) +
  scale_color_manual(name = NULL, values = myColors1, breaks = myItems2) +
  theme_agData() +
  labs(title = "Production Relative to 1961", x = NULL,
       y = "Percent Change", caption = myCaption)
ggsave("global_food_security_01.png", mp, width = 6, height = 4)

Production, Area & Yield

# Prep data
xx <- dd %>% filter(Area == "World")
# Plot
mp <- ggplot(xx, aes(x = Year)) +
  geom_line(aes(y = Population_R1961, color = "Population"),
            size = 1.25, alpha = 0.7) +
  geom_line(aes(y = Production_R1961, color = "Production"),
            size = 1.25, alpha = 0.7) +
  geom_line(aes(y = Area_R1961, color = "Area"),
            size = 1.25, alpha = 0.7) +
  geom_line(aes(y = Yield_R1961, color = "Yield"),
            size = 1.25, alpha = 0.7) +
  facet_wrap(. ~ Item, ncol = 4, scales = "free_y") +
  scale_x_continuous(breaks = seq(1960, 2020, by = 10)) +
  scale_color_manual(name = NULL, values = myColors2,
                     breaks = c("Population","Production", "Yield", "Area")) +
  theme_agData(legend.position = "bottom",
               axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Relative to 1961", x = NULL,
       y = "Percent Change", caption = myCaption)
ggsave("global_food_security_02.png", mp, width = 10, height = 6)

Cereals

# Prep data
xx <- dd %>% filter(Area == "World", Item == "Cereals")
# Plot
mp <- ggplot(xx, aes(x = Year)) +
  geom_line(aes(y = Population_R1961, color = "Population"),
            size = 1.5, alpha = 0.7) +
  geom_line(aes(y = Production_R1961, color = "Production"),
            size = 1.5, alpha = 0.7) +
  geom_line(aes(y = Yield_R1961, color = "Yield"),
            size = 1.5, alpha = 0.7) +
  geom_line(aes(y = Area_R1961, color = "Area"),
            size = 1.5, alpha = 0.7) +
  scale_x_continuous(breaks = seq(1960, 2020, by = 10)) +
  scale_color_manual(name = NULL, values = myColors2,
                     breaks = c("Population","Production", "Yield", "Area")) +
  theme_agData() +
  labs(title = "Relative to 1961 - Cereals", x = NULL,
       y = "Percent Change", caption = myCaption)
ggsave("global_food_security_03.png", mp, width = 6, height = 4)

Cereals vs. Pulses

# Prep data
xx <- dd %>% filter(Area == "World", Item %in% c("Cereals", "Pulses"))
# Plot
mp <- ggplot(xx, aes(x = Year)) +
  geom_line(aes(y = Population_R1961, color = "Population"),
            size = 1.5, alpha = 0.7) +
  geom_line(aes(y = Production_R1961, color = "Production"),
            size = 1.5, alpha = 0.7) +
  geom_line(aes(y = Yield_R1961, color = "Yield"),
            size = 1.5, alpha = 0.7) +
  geom_line(aes(y = Area_R1961, color = "Area"),
            size = 1.5, alpha = 0.7) +
  facet_grid(. ~ Item) +
  scale_x_continuous(breaks = seq(1960, 2020, by = 10)) +
  scale_color_manual(name = NULL, values = myColors2,
                     breaks = c("Population","Production", "Yield", "Area")) +
  theme_agData(legend.position = "bottom") +
  labs(title = "Relative to 1961", x = NULL,
       y = "Percent Change", caption = myCaption)
ggsave("global_food_security_04.png", mp, width = 6, height = 4)

Tonnes Per Person

Regions

# Prep data
xx <- dd %>% filter(Area %in% myRegions, Year == max(Year))
# Plot
mp <- ggplot(xx, aes(x = Area, y = TonnesPerPerson, fill = Area)) + 
  geom_col(color = "black", alpha = 0.7) +
  facet_wrap(Item ~ ., scales = "free", ncol = 4) +
  scale_fill_manual(values = myColors3) +
  theme_agData_col(legend.position = "none", 
                   axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = paste("Food Security -", max(xx$Year)), 
       y = "Tonnes Per Person", x = NULL, caption = myCaption)
ggsave("global_food_security_05.png", mp, width = 12, height = 6)

SubRegions

# Prep data
xx <- dd %>% filter(Area %in% mySubRegions, Year == max(Year))
# Plot
mp <- ggplot(xx, aes(x = Area, y = TonnesPerPerson, fill = Region)) + 
  geom_col(color = "black", alpha = 0.7) +
  facet_grid(Item ~ Region, scales = "free", space = "free_x") +
  scale_fill_manual(values = myColors3) +
  theme_agData_col(legend.position = "none", 
                   axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = paste("Food Security -", max(xx$Year)), 
       y = "Tonnes Per Person", x = NULL, caption = myCaption)
ggsave("global_food_security_06.png", mp, width = 6, height = 8)

Scatter Plot

# Prep Data
xx <- dd %>% filter(Area %in% mySubRegions, Year == max(Year)) %>%
  select(Area, Region, Item, TonnesPerPerson) %>%
  spread(Item, TonnesPerPerson)
# Plot
mp <- ggplot(xx, aes(x = Cereals, y = Pulses)) + 
  geom_point(aes(size = Tubers, color = Region), alpha = 0.8) + 
  geom_text_repel(aes(label = Area), size = 2, alpha = 0.8) +
  scale_color_manual(values = myColors3) +
  theme_agData() +
  labs(title = paste("Tonnes Per Person -", max(dd$Year)),
       caption = myCaption)
ggsave("global_food_security_07.png", mp, width = 6, height = 4)

SubRegions

Unscaled

# Prep data
xx <- dd %>% filter(Area %in% mySubRegions)
# Plot
mp <- ggplot(xx, aes(x = Year, y = TonnesPerPerson, color = Item)) + 
  geom_line(size = 1, alpha = 0.7) +
  facet_wrap(. ~ Region + Area, ncol = 7) +
  scale_x_continuous(breaks = seq(1970,2010, by = 20)) +
  scale_color_manual(name = NULL, values = myColors1[-1]) +
  theme_agData() +
  labs(title = "Global Food Security - SubRegions", x = NULL,
       y = "Tonnes Per Person", caption = myCaption)
ggsave("global_food_security_08.png", mp, width = 10, height = 6)

Scaled

# Prep data
xx <- dd %>% filter(Area %in% mySubRegions)
# Plot
mp <- ggplot(xx, aes(x = Year, y = TonnesPerPerson, color = Item)) + 
  geom_line(size = 1, alpha = 0.7) +
  facet_wrap(. ~ Region + Area, scales = "free_y", ncol = 7) +
  scale_x_continuous(breaks = seq(1970,2010, by = 20)) +
  scale_color_manual(name = NULL, values = myColors1[-1]) +
  theme_agData() +
  labs(title = "Global Food Security - SubRegions", x = NULL,
       y = "Tonnes Per Person", caption = myCaption)
ggsave("global_food_security_09.png", mp, width = 12, height = 6)

Import/Export

Grains

# Prep data
myItems <- c("Wheat", "Maize (corn)", "Rice", "Sorghum")
xx <- bind_rows(agData_FAO_Trade_Import_Quantity,
                agData_FAO_Trade_Export_Quantity) %>%
  filter(Item %in% myItems, Area %in% myRegions)# %>%
  #mutate(Area = factor(Area, levels = myAreas))
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000000, 
                     group = Area, color = Area)) + 
  geom_line(size = 1, alpha = 0.7) + 
  facet_grid(Item ~ Measurement, scales = "free_y") +
  scale_color_manual(name = NULL, values = myColors3) +
  scale_x_continuous(breaks       = seq(1965, 2020, by = 10),
                     minor_breaks = seq(1965, 2020, by = 5))  +
  theme_agData(legend.position = "bottom") + 
  labs(title = "Import/Export of Major Grains", x = NULL, 
       y = "Million Tonnes", caption = myCaption)
ggsave("global_food_security_10.png", mp, width = 6, height = 6)

Legumes

# Prep data
myItems <- c("Soya beans", "Beans, dry", "Chick peas, dry", "Lentils, dry")
xx <- bind_rows(agData_FAO_Trade_Import_Quantity,
                agData_FAO_Trade_Export_Quantity) %>%
  filter(Item %in% myItems, Area %in% myRegions)# %>%
  #mutate(Area = factor(Area, levels = myAreas))
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000000, 
                     group = Area, color = Area)) + 
  geom_line(size = 1, alpha = 0.7) + 
  facet_grid(Item ~ Measurement, scales = "free_y") +
  scale_color_manual(name = NULL, values = myColors3) +
  scale_x_continuous(breaks       = seq(1965, 2020, by = 10),
                     minor_breaks = seq(1965, 2020, by = 5))  +
  theme_agData(legend.position = "bottom") + 
  labs(title = "Import/Export of Major Legumes", x = NULL, 
       y = "Million Tonnes", caption = myCaption)
ggsave("global_food_security_11.png", mp, width = 6, height = 6)

Pulses

# Prep data
myPulses <- c("Lentils, dry", "Chick peas, dry", 
              "Peas, dry", "Peas, green", "Pigeon peas, dry", "Cow peas, dry",
              "Beans, dry", "Other beans, green", "Broad beans and horse beans, dry", 
              "Broad beans and horse beans, green")
xx <- agData_FAO_Crops %>%
  filter(Area == "World", Measurement == "Production",
         Item %in% myPulses) %>%
  mutate(Item = factor(Item, levels = myPulses))
#
mp <- ggplot(xx, aes(x = Year, y = Value/1000000)) +
  geom_line(color = "darkgreen", linewidth = 1) +
  facet_wrap(Item ~ ., ncol = 5) +
  theme_agData() +
  labs(title = "Pulses", y = "Million Tonnes", caption = myCaption)
ggsave("global_food_security_12.png", mp, width = 8, height = 5)

mp <- ggplot(xx, aes(x = Year, y = Value/1000000)) +
  geom_line(color = "darkgreen", linewidth = 1) +
  facet_wrap(Item ~ ., ncol = 5, scales = "free_y") +
  theme_agData() +
  labs(title = "Pulses", y = "Million Tonnes", caption = myCaption)
ggsave("global_food_security_13.png", mp, width = 8, height = 5)

# Prep data
xx <- xx %>% arrange(Area, Year) %>%
  group_by(Area) %>%
  mutate(GrowthRate = 100 * (Value - lag(Value)) / lag(Value))
#
mp <- ggplot(xx, aes(x = Year, y = GrowthRate)) +
  geom_line(color = "darkgreen", linewidth = 1) +
  facet_wrap(Item ~ ., ncol = 5, scales = "free_y") +
  theme_agData() +
  labs(title = "Pulses", y = "Growth Rate (%)", caption = myCaption)
ggsave("global_food_security_14.png", mp, width = 8, height = 5)


dblogr/


© Derek Michael Wright