Lentil Production
Graphs of lentil production using FAO data
Prepare Data
# devtools::install_github("derekmichaelwright/agData")
library(agData)
library(gganimate)
# Prep data
<- "www.dblogr.com/ or derekmichaelwright.github.io/dblogr/ | Data: FAOSTAT"
myCaption #
<- agData_FAO_Crops %>% filter(Item == "Lentils, dry")
d1 <- agData_FAO_Trade_Quantity %>% filter(Item == "Lentils, dry") d2
PDF - All Lentil Data
# Prep data
<- c("darkgreen", "darkred", "darkgoldenrod2")
myColors <- c("World",
myAreas unique(agData_FAO_Region_Table$Region),
unique(agData_FAO_Region_Table$SubRegion),
unique(agData_FAO_Country_Table$Country))
<- d1 %>%
xx mutate(Value = ifelse(Measurement %in% c("Area Harvested", "Production"),
/ 1000000, Value / 1000),
Value Unit = plyr::mapvalues(Unit, c("hectares", "tonnes", "kg/ha"),
c("Million Hectares", "Million Tonnes", "Tonnes/ Hectare")))
<- myAreas[myAreas %in% xx$Area]
myAreas # 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
<- d1 %>%
xx filter(Measurement == "Production", Area != "Slovenia",
%in% agData_FAO_Country_Table$Country) %>%
Area 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
<- ggplot(xx, aes(xmin = 0, xmax = Value / 1000000,
mp 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 = 30, end_pause = 60,
width = 900, height = 600, res = 150)
Production
# Prep data
<- c("Canada", "Southern Asia", "USA", "Turkey",
myAreas "Europe", "Africa", "Oceania")
<- c("darkgreen", "darkgoldenrod2", "darkblue", "darkslategrey",
myColors "steelblue", "darkred", "antiquewhite4")
<- d1 %>%
xx filter(Area %in% myAreas, Measurement == "Production") %>%
mutate(Area = factor(Area, levels = myAreas) )
# Plot
<- ggplot(xx, aes(x = Year, y = Value / 1000000, color = Area)) +
mp 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
<- c("darkgreen", "darkgoldenrod2", "steelblue", "antiquewhite4",
myColors "darkslategrey", "darkred", "darkblue" )
<- c("Americas", "Southern Asia", "Oceania",
myRegions "Western Asia", "Eastern Asia", "Africa", "Europe")
<- d1 %>% region_Info() %>%
xx filter(Area %in% agData_FAO_Country_Table$Country,
== 2019, Measurement == "Production") %>%
Year arrange(desc(Value)) %>%
slice(1:10) %>%
mutate(Area = factor(Area, levels = Area),
Region = ifelse(Region == "Asia", SubRegion, Region),
Region = factor(Region, levels = myRegions))
# Plot
<- ggplot(xx, aes(x = Area, y = Value / 1000000, fill = Region)) +
mp 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
# Prep data
<- c("Canada", "Southern Asia", "USA", "Turkey",
myAreas "Europe", "Africa", "Oceania")
<- c("darkgreen", "darkgoldenrod2", "darkblue", "darkslategrey",
myColors "steelblue", "darkred", "antiquewhite4")
<- d1 %>%
xx filter(Area %in% myAreas, Measurement == "Area Harvested") %>%
mutate(Area = factor(Area, levels = myAreas) )
# Plot
<- ggplot(xx, aes(x = Year, y = Value / 1000000, color = Area)) +
mp 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 Top Area Harvested
# Prep data
<- c("darkgreen", "darkgoldenrod2", "steelblue", "darkblue",
myColors "darkslategrey", "darkred")
<- c("Americas", "Southern Asia", "Oceania", "Western Asia",
myRegions "Europe", "Central Asia", "Africa")
<- d1 %>% region_Info() %>%
xx filter(Area %in% agData_FAO_Country_Table$Country,
== 2019, Measurement == "Area Harvested") %>%
Year arrange(desc(Value)) %>%
slice(1:10) %>%
mutate(Area = factor(Area, levels = Area),
Region = ifelse(Region == "Asia", SubRegion, Region),
Region = factor(Region, levels = myRegions))
# Plot
<- ggplot(xx, aes(x = Area, y = Value / 1000000, fill = Region)) +
mp 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
<- d1 %>%
x1 filter(Measurement == "Area Harvested", Year == max(Year),
%in% agData_FAO_Country_Table$Country) %>%
Area top_n(10, Value)
<- d1 %>%
xx filter(Measurement == "Yield", Year > max(Year) - 5,
%in% x1$Area) %>%
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
<- ggplot(xx, aes(x = Area, y = Value / 1000, fill = Region)) +
mp 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
<- c("Canada", "India", "Ethiopia", "Turkey")
myAreas <- c("darkgreen", "darkgoldenrod2", "darkred", "darkslategrey")
myColors <- d1 %>%
xx filter(Area %in% myAreas, Measurement == "Yield") %>%
mutate(Area = factor(Area, levels = myAreas))
# Plot
<- ggplot(xx, aes(x = Year, y = Value / 1000, color = Area)) +
mp 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
<- c("World", "Canada", "India", "Nepal", "Bangladesh")
myAreas <- c("black", "darkgreen", "darkgoldenrod2", "darkred", "darkslategrey")
myColors <- d1 %>%
xx filter(Area %in% myAreas, Measurement == "Yield") %>%
mutate(Area = factor(Area, levels = myAreas))
# Plot
<- ggplot(xx, aes(x = Year, y = Value / 1000, color = Area)) +
mp 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
<- d1 %>%
xx filter(Area %in% c("Canada", "World"),
== "Production") %>%
Measurement mutate(Area = factor(Area, levels = c("World", "Canada")))
# Plot
<- ggplot(xx, aes(x = Year, y = Value / 1000000,
mp 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
<- d1 %>%
xx filter(Area %in% c("Canada", "World"),
== "Production") %>%
Measurement spread(Area, Value) %>%
mutate(`Rest of the World` = World - ifelse(is.na(Canada), 0, Canada)) %>%
select(-World) %>%
gather(Area, Value, 5:6)
<- xx %>% filter(Area == "Rest of the World") %>% pull(Value) %>% min() / 1000000
x1 <- xx %>% filter(Area == "Canada") %>% pull(Value) %>% max(na.rm = T) / 1000000
x2 # Plot
<- ggplot(xx, aes(x = Year, y = Value / 1000000, color = Area)) +
mp 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
# Plot function
<- function(crop = "Lentils, dry", measurement = "Production",
ggPie_single year = 2019, cutoff = 8,
myColors = agData_Colors[c(1:10,41:50)]) {
# Prep data
<- agData_FAO_Crops %>%
world filter(Area == "World", Year %in% year, Item == crop,
== measurement) %>%
Measurement select(Year, Total=Value)
<- agData_FAO_Crops %>% region_Info() %>%
xx filter(Item == crop, Year %in% year, Measurement == measurement,
%in% agData_FAO_Country_Table$Country) %>%
Area arrange(desc(Value)) %>%
select(Area, Year, Item, Measurement, Unit, Value)
#
<- xx %>% slice(1:cutoff)
x1 <- xx %>% slice(cutoff+1:nrow(.)) %>%
x2 group_by(Item, Measurement, Unit, Year) %>%
summarise(Value = sum(Value)) %>% ungroup() %>%
mutate(Area = "Rest of World")
<- bind_rows(x1, x2) %>%
yy 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)
}#
<- function(crop = "Lentils, dry", measurement = "Production",
ggPie_anim cutoff = 2) {
# Prep data
<- agData_FAO_Crops %>% region_Info() %>%
xx filter(Item == crop, Measurement == measurement,
%in% agData_FAO_Country_Table$Country) %>%
Area group_by(Year) %>%
mutate(Percent = round(100 * Value / sum(Value, na.rm = T),1))
<- c(xx %>% arrange(desc(Percent)) %>%
myAreas filter(Year == max(xx$Year), Percent > cutoff) %>%
pull(Area) %>% as.character(), "Other")
<- xx %>% filter(Area %in% myAreas) %>% arrange(desc(Percent))
x1 <- xx %>% filter(!Area %in% myAreas) %>%
x2 group_by(Item, Measurement, Unit, Year) %>%
summarise(Percent = sum(Percent, na.rm = T)) %>% ungroup() %>%
mutate(Area = "Other")
<- bind_rows(x1, x2) %>%
xx 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
<- ggPie_single(crop = "Lentils, dry", measurement = "Production",
mp year = 2019, cutoff = 8)
ggsave("lentil_11.png", mp, width = 5, height = 4)
Animation
Production
# Plot function
<- ggPie_anim(crop = "Lentils, dry", measurement = "Production", cutoff = 2.5)
mp anim_save("lentil_gif_02.gif", mp,
nframes = 300, fps = 10, end_pause = 30,
width = 900, height = 600, res = 150)
Area Harvested
# Plot function
<- ggPie_anim(crop = "Lentils, dry", measurement = "Area Harvested", cutoff = 2.5)
mp anim_save("lentil_gif_03.gif", mp,
nframes = 300, fps = 10, end_pause = 30,
width = 900, height = 600, res = 150)
Import/Export
Canada vs India
# Prep data
<- d2 %>% filter(Area %in% c("Canada","India") )
xx # Plot
<- ggplot(xx, aes(x = Year, y = Value / 1000000, group = Measurement,
mp 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
<- c("Americas","Asia","Europe","Africa","Oceania")
myAreas <- c("darkgreen","darkorange","darkblue","darkred","steelblue")
myColors <- d2 %>%
xx region_Info() %>%
filter(Area %in% agData_FAO_Country_Table$Region) %>%
mutate(Area = factor(Area, levels = myAreas))
# Plot
<- ggplot(xx, aes(x = Year, y = Value / 1000000,
mp 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
<- d1 %>%
x1 filter(Measurement == "Production", Year == 2018,
%in% agData_FAO_Country_Table$Country) %>%
Area top_n(10, Value)
<- d1 %>%
xx filter(Measurement == "Production", Area %in% x1$Area)
<- c("lightgrey", "darkgreen")
myColors # Plot
<- ggplot(xx, aes(x = Year, y = Area, fill = Value / 1000000)) +
mp 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)