MÉMOVISUELDAGRONOMIE

Evolution of energy consumption in agriculture


The illustration we are about to reproduce shows the evolution of energy use in agriculture, from 1900 to the present day. It focuses on France. Data taken from the excellent thesis by Souhil Harchaoui.

In this tutorial, we will different ways of plotting this dataset: line, stacked area chart and finally a streamgraph.

Basically, a streamgraph is a type of stacked area chart, with rounded edges to to give the impression of a stream or plume of smoke. This is an interesting visual alternative if the important thing is to represent proportions rather than give exact values.

1. Load packages and data

In R, streamgraph may be easily created with the the {ggstream} package. In addition to this library, we will the {tidyverse} package to process and plot data:

# To install {ggstream}:
# install.packages("ggstream") 

# Load pacakges
library(tidyverse)
library(ggstream)   

About the dataset, Souhil Harchaoui has transformed various items in the metabolic system of French agriculture (feed for farmers or draught animals, fuel for farm machinery, fertilizers, etc.) into energy consumption.

For this tutorial, I’ve extracted a simplified version of this dataset, which can be loaded into R as follows:

data<-read_csv('https://raw.githubusercontent.com/BjnNowak/TidyTuesday/refs/heads/main/data/energy_harchaoui.csv')     

# Look at first lines
head(data)
## # A tibble: 6 × 3
##    year type        value
##   <dbl> <chr>       <dbl>
## 1  1882 animals     225. 
## 2  1882 farmers      53.3
## 3  1882 fertilizers   0  
## 4  1882 fuels         0  
## 5  1882 others        0  
## 6  1883 animals     225.

2. Plotting the data

Since the dataset is already in a long format, no additional data cleaning is required here and a first line chart may be created this way:

# Make line_plot
p_line <- ggplot(data,aes(x=year,y=value,color=type))+
  geom_line()+
  labs(x="Year",y="Energy (PJ)",color="Item")+
  theme_light()

p_line

We will now add the custom palette of the book to this plot:

# Create custom palette
pal <- c(
  "animals" = "#4CB944",
  "farmers" = "#FF7D00",
  "others" = "#D6E681",
  "fertilizers" = "#01BEFE",
  "fuels" = "#FF006D"
)

p_line <- p_line+
  scale_color_manual(values=pal)

p_line

We can see, for example, the decline in the category of draft animals during the world wars (which could be used by the army).

# Annotate WWI and WWII
p_line <- p_line+
  annotate(
    geom="rect",
    xmin=1914,xmax=1918,ymin=-Inf,ymax=Inf,
    fill="dimgrey",
    alpha=0.5
  )+
  annotate(
    geom="rect",
    xmin=1939,xmax=1945,ymin=-Inf,ymax=Inf,
    fill="dimgrey",
    alpha=0.5
  )

p_line

3. Make a streamgraph

Data can also be represented in the form of a stacked area chart. In this case, the “fill” attribute will be used to color the categories.

# Make stacked area chart
p_area<-ggplot(
    data,
    aes(x=year,y=value,fill=type) # Use 'fill' instead of 'color'
  )+
  geom_area(color="white",linewidth=0.5)+
  labs(x="Year",y="Energy (PJ)",fill="Item")+
  scale_fill_manual(values=pal)+ 
  theme_light()

p_area

With {ggstream}, you only have to replace geom_area() by geom_stream() to create a streamgraph:

# Make streamgraph:
p_stream<-ggplot(
    data%>%filter(year>1899),
    aes(x=year,y=value,fill=type)
  )+
  geom_stream(color="white",linewidth=0.5)+
  scale_x_continuous(breaks=seq(1920,2000,20))+
  labs(x="Year",fill="Item")+
  scale_fill_manual(values=pal)+
  theme_minimal()+
  # Customize theme
  theme(
    panel.grid=element_blank(),
    axis.title.y = element_blank(),
    axis.text.y = element_blank()
  )

p_stream

We can thus see :

  • the replacement of draught animals by tractors;

  • the high energy cost of nitrogen fertilizers.