dblogr.com/

Lentil Production

Graphs of lentil production using FAO data


Prepare Data

# devtools::install_github("derekmichaelwright/agData")
library(agData)
library(gganimate)
# Prep data
myCaption <- "www.dblogr.com/ or derekmichaelwright.github.io/dblogr/ | Data: FAOSTAT"
#
d1 <- agData_FAO_Crops %>% filter(Item == "Lentils, dry")
d2 <- agData_FAO_Trade_Quantity %>% filter(Item == "Lentils, dry")

PDF - All Lentil Data

figures_lentil_fao.pdf”

# Prep data
myColors <- c("darkgreen", "darkred", "darkgoldenrod2")
myAreas <- c("World",
  unique(agData_FAO_Region_Table$Region),
  unique(agData_FAO_Region_Table$SubRegion),
  unique(agData_FAO_Country_Table$Country))
xx <- d1 %>%
  mutate(Value = ifelse(Measurement %in% c("Area Harvested", "Production"),
                        Value / 1000000, Value / 1000),
         Unit = plyr::mapvalues(Unit, c("hectares", "tonnes", "kg/ha"), 
                  c("Million Hectares", "Million Tonnes", "Tonnes/ Hectare")))
myAreas <- myAreas[myAreas %in% xx$Area]
# Plot
pdf("figures_lentil_fao.pdf", width = 12, height = 4)
for(i in myAreas) {
  print(ggplot(xx %>% filter(Area == i)) +
    geom_line(aes(x = Year, y = Value, color = Measurement),
              size = 1.5, alpha = 0.7) +
    facet_wrap(. ~ Measurement + Unit, ncol = 3, scales = "free_y") +
    theme_agData(legend.position = "none", 
                 axis.text.x = element_text(angle = 45, hjust = 1)) +
    scale_color_manual(values = myColors) +
    scale_x_continuous(breaks = seq(1960, 2020, by = 5) ) +
    labs(title = i, y = NULL, x = NULL, caption = myCaption) )
}
dev.off()

Bar Chart Racer

# Prep data
xx <- d1 %>% 
  filter(Measurement == "Production", Area != "Slovenia",
         Area %in% agData_FAO_Country_Table$Country) %>% 
  group_by(Area) %>%
  mutate(Value = movingAverage(Value, n = 3)) %>%
  group_by(Year) %>%
  arrange(Year, -Value) %>%
  mutate(Rank = 1:n()) %>%
  filter(Rank < 15) %>% 
  arrange(desc(Year)) %>%
  mutate(Item = factor(Item, levels = unique(.$Item)))
# Plot
mp <- ggplot(xx, aes(xmin = 0, xmax = Value / 1000000, 
                     ymin = Rank - 0.45, ymax = Rank + 0.45, y = Rank, 
                     fill = Area)) + 
  geom_rect(alpha = 0.7, color = "black") + 
  scale_fill_manual(values = agData_Colors) +
  scale_x_continuous(limits = c(-1,3.25), breaks = seq(0, 3, by=0.5)) +
  geom_text(col = "black", hjust = "right", aes(label = Area), x = -0.1) +
  scale_y_reverse() +
  theme_agData(legend.position = "none",
               axis.text.y = element_blank(), 
               axis.ticks.y = element_blank()) + 
  labs(title = "Lentil - Production - {round(frame_time)}", y = NULL,
       x = "Million Tonnes (3 year moving average)", caption = myCaption) +
  transition_time(Year) +
  view_follow()
  #view_follow(fixed_x = c(-1, NA))
anim_save("lentil_gif_01.gif", mp, 
          nframes = 600, fps = 25, end_pause = 60, 
          width = 900, height = 600, res = 150, units = "px")

Production

Regions

# Prep data
myAreas <- c("Canada", "Southern Asia", "USA", "Turkey", 
             "Europe", "Africa", "Oceania")
myColors <- c("darkgreen", "darkgoldenrod2", "darkblue", "darkslategrey", 
              "steelblue", "darkred", "antiquewhite4")
xx <- d1 %>% 
  filter(Area %in% myAreas, Measurement == "Production") %>%
  mutate(Area = factor(Area, levels = myAreas) ) 
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000000, color = Area)) + 
  geom_line(size = 1.5, alpha = 0.7) + 
  scale_color_manual(name = NULL, values = myColors) +
  scale_x_continuous(breaks = seq(1960, 2020, 5), minor_breaks = NULL) +
  coord_cartesian(xlim = c(min(xx$Year)+1, max(xx$Year)-2)) +
  theme_agData(legend.position = "bottom") +
  guides(color = guide_legend(nrow = 1)) +
  labs(title = "Lentil - Production", x = NULL,
       y = "Million Tonnes", caption = myCaption)
ggsave("lentil_01.png", width = 6, height = 4)

2019

# Prep data
myColors <- c("darkgreen", "darkgoldenrod2", "steelblue", "antiquewhite4",
              "darkslategrey",  "darkred", "darkblue" )
myRegions <- c("Americas", "Southern Asia", "Oceania", 
               "Western Asia", "Eastern Asia", "Africa", "Europe")
xx <- d1 %>% region_Info() %>%
  filter(Area %in% agData_FAO_Country_Table$Country,
         Year == 2019, Measurement == "Production") %>%
  arrange(desc(Value)) %>% 
  slice(1:10) %>%
  mutate(Area = factor(Area, levels = Area),
         Region = ifelse(Region == "Asia", SubRegion, Region),
         Region = factor(Region, levels = myRegions))
# Plot
mp <- ggplot(xx, aes(x = Area, y = Value / 1000000, fill = Region)) + 
  geom_col(color = "black", alpha = 0.7) +
  scale_fill_manual(values = myColors) +
  theme_agData(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Lentil - Production - 2019", x = NULL,
       y = "Million Tonnes", caption = myCaption)
ggsave("lentil_02.png", mp, width = 6, height = 4)

Area Harvested

Regions

# Prep data
myAreas <- c("Canada", "Southern Asia", "USA", "Turkey", 
             "Europe", "Africa", "Oceania")
myColors <- c("darkgreen", "darkgoldenrod2", "darkblue", "darkslategrey", 
              "steelblue", "darkred", "antiquewhite4")
xx <- d1 %>% 
  filter(Area %in% myAreas, Measurement == "Area Harvested") %>%
  mutate(Area = factor(Area, levels = myAreas) ) 
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000000, color = Area)) + 
  geom_line(size = 1.5, alpha = 0.7) + 
  scale_color_manual(name = NULL, values = myColors) +
  scale_x_continuous(breaks = seq(1960, 2020, 5), minor_breaks = NULL) +
  coord_cartesian(xlim = c(min(xx$Year)+1, max(xx$Year)-2)) +
  theme_agData(legend.position = "bottom") +
  guides(color = guide_legend(nrow = 1)) +
  labs(title = "Lentil - Area Harvested", x = NULL, 
       y = "Million Hectares", caption = myCaption)
ggsave("lentil_03.png", width = 6, height = 4)

2019

# Prep data
myColors <- c("darkgreen", "darkgoldenrod2", "steelblue", "darkblue", 
              "darkslategrey", "darkred")
myRegions <- c("Americas", "Southern Asia", "Oceania", "Western Asia", 
               "Europe", "Central Asia", "Africa")
xx <- d1 %>% region_Info() %>%
  filter(Area %in% agData_FAO_Country_Table$Country,
         Year == 2019, Measurement == "Area Harvested") %>%
  arrange(desc(Value)) %>% 
  slice(1:10) %>%
  mutate(Area = factor(Area, levels = Area),
         Region = ifelse(Region == "Asia", SubRegion, Region),
         Region = factor(Region, levels = myRegions))
# Plot
mp <- ggplot(xx, aes(x = Area, y = Value / 1000000, fill = Region)) + 
  geom_col(color = "black", alpha = 0.7) +
  scale_fill_manual(values = myColors) +
  theme_agData(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Lentil - Area Harvested - 2019", 
       y = "Million Hectares", x = NULL, caption = myCaption)
ggsave("lentil_04.png", mp, width = 6, height = 4)

Yields

# Prep data
x1 <- d1 %>% 
  filter(Measurement == "Area Harvested", Year == max(Year),
         Area %in% agData_FAO_Country_Table$Country) %>%
    top_n(10, Value)
xx <- d1 %>% 
  filter(Measurement == "Yield", Year > max(Year) - 5,
         Area %in% x1$Area) %>% 
  group_by(Area) %>%
  summarise(Value = mean(Value)) %>% 
  region_Info() %>%
  arrange(desc(Value)) %>%
  mutate(Area = factor(Area, levels = Area),
         Region = ifelse(Region == "Asia", SubRegion, Region),
         Region = factor(Region, levels = myRegions))
# Plot
mp <- ggplot(xx, aes(x = Area, y = Value / 1000, fill = Region)) + 
  geom_col(color = "black", alpha = 0.7) + 
  scale_fill_manual(values = myColors) +
  theme_agData(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Lentil Yields - 5 year average",
       y = "Tonnes / Hectare", x = NULL, caption = myCaption)
ggsave("lentil_05.png", mp, width = 6, height = 4)

# Prep data
myAreas <- c("Canada", "India", "Ethiopia", "Turkey")
myColors <- c("darkgreen", "darkgoldenrod2", "darkred", "darkslategrey")
xx <- d1 %>%
  filter(Area %in% myAreas, Measurement == "Yield") %>%
  mutate(Area = factor(Area, levels = myAreas))
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000, color = Area)) +
  geom_line(alpha = 0.2) +
  stat_smooth(geom = "line", method = "loess", 
              size = 1.5, alpha = 0.7, se = F) +
  scale_x_continuous(breaks = seq(1960, 2020, by = 5), minor_breaks = NULL) +
  coord_cartesian(xlim = c(min(xx$Year)+1, max(xx$Year)-2)) +
  scale_color_manual(name = NULL, values = myColors) +
  theme_agData(legend.position = "bottom") +
  labs(title = "Lentil Yields", x = NULL,
       y = "Tonnes / Hectare", caption = myCaption)
ggsave("lentil_06.png", mp, width = 6, height = 4)

# Prep data
myAreas <- c("World", "Canada", "India", "Nepal", "Bangladesh")
myColors <- c("black", "darkgreen", "darkgoldenrod2", "darkred", "darkslategrey")
xx <- d1 %>%
  filter(Area %in% myAreas, Measurement == "Yield") %>%
  mutate(Area = factor(Area, levels = myAreas))
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000, color = Area)) +
  geom_line(alpha = 0.2) +
  stat_smooth(geom = "line", method = "loess", 
              size = 1.5, alpha = 0.7, se = F) +
  scale_x_continuous(breaks = seq(1960, 2020, by = 5), minor_breaks = NULL) +
  coord_cartesian(xlim = c(min(xx$Year)+1, max(xx$Year)-2)) +
  scale_color_manual(name = NULL, values = myColors) +
  theme_agData(legend.position = "bottom") +
  labs(title = "Lentil Yields", x = NULL,
       y = "Tonnes / Hectare", caption = myCaption)
ggsave("lentil_07.png", mp, width = 6, height = 4)

Canada’s Global Contribution

# Prep data
xx <- d1 %>% 
  filter(Area %in% c("Canada", "World"),
         Measurement == "Production") %>%
  mutate(Area = factor(Area, levels = c("World", "Canada")))
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000000, 
                     fill = Area, alpha = Area, color = I("Black"))) +
  geom_area(position = "identity") +
  theme(legend.position = "bottom") +
  scale_fill_manual(name = NULL, values = c("darkgreen", "darkred")) +
  scale_alpha_manual(name = NULL, values = c(0.6, 0.8)) +
  scale_x_continuous(breaks = seq(1960, 2020, 5), minor_breaks = NULL)  +
  coord_cartesian(xlim = c(min(xx$Year)+1, max(xx$Year)-3)) +
  theme_agData() +
  labs(title   = "Canada's Contribution to Global Lentil Production", 
       y = "Million Tonnes", x = NULL, caption = myCaption)
ggsave("lentil_08.png", mp, width = 6, height = 4)

Canada vs the World

# Prep data
xx <- d1 %>% 
  filter(Area %in% c("Canada", "World"),
         Measurement == "Production") %>%
  spread(Area, Value) %>%
  mutate(`Rest of the World` = World - ifelse(is.na(Canada), 0, Canada)) %>%
  select(-World) %>%
  gather(Area, Value, 5:6)
x1 <- xx %>% filter(Area == "Rest of the World") %>% pull(Value) %>% min() / 1000000
x2 <- xx %>% filter(Area == "Canada") %>% pull(Value) %>% max(na.rm = T) / 1000000
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000000, color = Area)) +
  geom_hline(yintercept = x1, lty = 2, alpha = 0.7) +
  geom_hline(yintercept = x2, lty = 2, alpha = 0.7) +
  geom_line(size = 1.25, alpha = 0.7) +
  theme(legend.position = "bottom") +
  scale_color_manual(name = NULL, values = c("darkred", "darkgreen")) +
  scale_x_continuous(breaks = seq(1960, 2020, 5), minor_breaks = NULL)  +
  theme_agData() +
  labs(title   = "Canada vs. Rest of the World", 
       y = "Million Tonnes", x = NULL, caption = myCaption)
ggsave("lentil_09.png", mp, width = 6, height = 4)

Production Pie Charts

Plotting Function

# Plot function
ggPie_single <- function(crop = "Lentils, dry", measurement = "Production", 
                         year = 2019, cutoff = 8,
                         myColors = agData_Colors[c(1:10,41:50)]) {
  # Prep data
  world <- agData_FAO_Crops %>% 
    filter(Area == "World", Year %in% year, Item == crop, 
           Measurement == measurement) %>%
    select(Year, Total=Value)
  xx <- agData_FAO_Crops %>% region_Info() %>%
    filter(Item == crop, Year %in% year, Measurement == measurement,
           Area %in% agData_FAO_Country_Table$Country) %>%
    arrange(desc(Value)) %>% 
    select(Area, Year, Item, Measurement, Unit, Value)
  #
  x1 <- xx %>% slice(1:cutoff)
  x2 <- xx %>% slice(cutoff+1:nrow(.)) %>% 
    group_by(Item, Measurement, Unit, Year) %>%
    summarise(Value = sum(Value)) %>% ungroup() %>%
    mutate(Area = "Rest of World")
  yy <- bind_rows(x1, x2) %>% 
    mutate(Total = world$Total,
           Percentage = round(100 * Value / Total), 
           Area = factor(Area, levels = rev(.$Area)),
           Cummulative_P = cumsum(Percentage),
           Label_y = Cummulative_P - (Percentage / 2))
  # Plot
  ggplot(yy, aes(x = 1)) +
    geom_col(aes(y = Percentage, fill = Area), color = "black", alpha = 0.7) +
    coord_polar("y", start = 0) +
    geom_label(aes(label = paste(Percentage, "%"), y = Label_y), 
               size = 2.5, alpha = 0.9, nudge_x = 0.29) + 
    facet_grid(. ~ Year) +
    theme_agData_pie(plot.caption = element_text(size = 7)) +
    xlim(0.542, 1.45) +
    scale_fill_manual(name = NULL, values = myColors,
                      breaks = yy$Area) +
    labs(title = paste(crop, measurement), caption = myCaption)
}
#
ggPie_anim <- function(crop = "Lentils, dry", measurement = "Production", 
                       cutoff = 2) {
  # Prep data
  xx <- agData_FAO_Crops %>% region_Info() %>%
    filter(Item == crop, Measurement == measurement,
           Area %in% agData_FAO_Country_Table$Country) %>%
    group_by(Year) %>%
    mutate(Percent = round(100 * Value / sum(Value, na.rm = T),1))
  myAreas <- c(xx %>% arrange(desc(Percent)) %>%
    filter(Year == max(xx$Year), Percent > cutoff) %>% 
    pull(Area) %>% as.character(), "Other")
  x1 <- xx %>% filter(Area %in% myAreas) %>% arrange(desc(Percent))
  x2 <- xx %>% filter(!Area %in% myAreas) %>% 
    group_by(Item, Measurement, Unit, Year) %>%
    summarise(Percent = sum(Percent, na.rm = T)) %>% ungroup() %>%
    mutate(Area = "Other")
  xx <- bind_rows(x1, x2) %>%
    arrange(Value) %>%
    mutate(Area = factor(Area, levels = myAreas),
           ISO2 = ifelse(Area == "Other", "", ISO2))
  # Plot
  ggplot(xx, aes(x = 1, fill = Area)) +
    geom_col(aes(y = -Percent), position = "fill", color = "black", alpha = 0.7) +
    coord_polar("y", start = 0) +
    theme_agData_pie() +
    scale_fill_manual(values = agData_Colors) +
    xlim(0.545, 1.5) +
    labs(title = paste(crop, "|", measurement, "| {round(frame_time)}"),
         caption = myCaption) +
    transition_time(Year)
}

2019

# Plot
mp <- ggPie_single(crop = "Lentils, dry", measurement = "Production", 
                   year = 2019, cutoff = 8)
ggsave("lentil_11.png", mp, width = 5, height = 4)

Animation

Production

# Plot function
mp <- ggPie_anim(crop = "Lentils, dry", measurement = "Production", cutoff = 2.5)
anim_save("lentil_gif_02.gif", mp,
          nframes = 300, fps = 10, end_pause = 30, 
          width = 900, height = 600, res = 150, units = "px")

Area Harvested

# Plot function
mp <- ggPie_anim(crop = "Lentils, dry", measurement = "Area Harvested", cutoff = 2.5)
anim_save("lentil_gif_03.gif", mp,
          nframes = 300, fps = 10, end_pause = 30, 
          width = 900, height = 600, res = 150, units = "px")

Import/Export

Canada vs India

# Prep data
xx <- d2 %>% filter(Area %in% c("Canada","India") )
# Plot
mp <- ggplot(xx, aes(x = Year, y = Value / 1000000, group = Measurement, 
                     color = Measurement)) + 
  geom_line(size = 1.25, alpha = 0.7) + 
  facet_grid(. ~ Area) +
  theme(legend.position = "bottom") +
  scale_color_manual(name = NULL, values = c("darkgreen", "darkorange")) +
  scale_x_continuous(breaks       = seq(1965, 2015, by = 10),
                     minor_breaks = seq(1965, 2015, by = 5))  +
  theme_agData() +
  labs(title = "Lentil Import/Export", x = NULL,
       y = "Million Tonnes", caption = myCaption)
ggsave("lentil_12.png", mp, width = 6, height = 4)

Regions

# Prep data
myAreas <- c("Americas","Asia","Europe","Africa","Oceania")
myColors <- c("darkgreen","darkorange","darkblue","darkred","steelblue")
xx <- d2 %>%
  region_Info() %>%
  filter(Area %in% agData_FAO_Country_Table$Region) %>%
  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(. ~ Measurement) +
  scale_x_continuous(breaks       = seq(1965, 2015, by = 10),
                     minor_breaks = seq(1965, 2015, by = 5))  +
  scale_color_manual(name = NULL, values = myColors) +
  theme_agData(legend.position = "bottom") + 
  labs(title = "Lentil Import/Export", x = NULL,
       y = "Million Tonnes", caption = myCaption)
ggsave("lentil_13.png", mp, width = 6, height = 4)

Heatmap

# Prep data
x1 <- d1 %>% 
  filter(Measurement == "Production", Year == 2018,
         Area %in% agData_FAO_Country_Table$Country) %>%
    top_n(10, Value)
xx <- d1 %>% 
  filter(Measurement == "Production", Area %in% x1$Area)
myColors <- c("lightgrey", "darkgreen")
# Plot
mp <- ggplot(xx, aes(x = Year, y = Area, fill = Value / 1000000)) +
  geom_tile(color = "white", size = 0.35) +
  scale_x_continuous(expand = c(0,0)) +
  scale_fill_gradientn(name = "Million Tonnes", colors = myColors, na.value = 'white') +
  theme_agData(legend.position = "bottom", bgFill = "white") +
  labs(title = "Lentil Production", x = NULL, y = NULL, caption = myCaption)
ggsave("lentil_14.png", mp, width = 6, height = 4)


dblogr.com/


© Derek Michael Wright