Data Visualization: Carbon Dioxide Emissions from Food Consumption

Data Visualization R

In this project, I created a data visualization of the top 15 countries with the highest total annual carbon dioxide emissions from food consumption using highly customized aesthetics in R.

Shuying Yu
2022-05-09
Code
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      fig.align = "center")

#Attach packages
library(tidyverse)
library(janitor)
library(paletteer)

Introduction

Data on annual carbon dioxide (\(CO_2\)) emissions per person for 130 nations worldwide was published by the Food and Agriculture Organization of the United Nations (FAO) on the nu3 website, and a subset of the data used here was scraped and shared by Kasia Kulm (2020).

Data Visualization

Code
########## Read in the data

food_consumption <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-18/food_consumption.csv')


########## Clean the data

food_consumption <- food_consumption %>% 
  
  #Recode values for country
  mutate(country = recode(country,
                          "USA" = "United States"),
         country = recode(country,
                          "Hong Kong SAR. China" = "Hong Kong (China)")) 



#Rename variables to give units
food_consumption_subset <- food_consumption %>% 
  
  #Select only co2 emissions data
  select(-consumption) %>% 
  
  #Change to tidy format: long to wide
  pivot_wider(names_from = food_category,
              values_from = co2_emmission) %>% 
  
  #Recode values for country
  mutate(country = recode(country,
                          "USA" = "United States"),
         country = recode(country,
                          "Hong Kong SAR. China" = "Hong Kong (China)")) %>% 
  
  #Rename last food category name
  rename(nuts_inc_peanut_butter = "Nuts inc. Peanut Butter")



########## Top 15 countries by CO2 emissions

#Categorize CO2 emissions by food consumption and country
top15_total_co2_by_country <- food_consumption %>%

  #Group by country
  group_by(country) %>%

  #Total annual CO2 emissions per capita across food categories
  summarize(total_annual_co2_kg_person_year = sum(co2_emmission)) %>%

  #Top 15 countries with total annual CO2 emissions per year
  slice_max(total_annual_co2_kg_person_year, n = 15) %>%

  #Reorder country based on total_annual_co2_kg
  mutate(country = reorder(country, total_annual_co2_kg_person_year))



########## Top 15 countries by CO2 emissions and by food group

#Categorize CO2 emissions by food consumption and country
annual_co2_country_food <- food_consumption_subset %>% 
  
  #Group by country
  group_by(country) %>% 
  
  #Sum kg CO2/person/year across foods in new column
  rowwise() %>% 
  
  #Total annual CO2 emissions per capita across food categories
  mutate(total_annual_co2_kg_person_year = sum(c_across(Pork:nuts_inc_peanut_butter))) %>%
  
  #Ungroup
  ungroup() %>% 
  
  #Arrange in descending order
  arrange(desc(total_annual_co2_kg_person_year)) %>% 

  #Select top 15 countries with highest total annual CO2 emissions per capita 
  slice_max(total_annual_co2_kg_person_year, n = 15) %>% 
  
  #Reorder country based on total_annual_co2_kg
  mutate(country = reorder(country,
                           total_annual_co2_kg_person_year))



########## Data reordering (long format)

#Convert back to long format
annual_co2_country_food_long <- annual_co2_country_food %>%

  pivot_longer(cols = Pork:nuts_inc_peanut_butter,
               names_to = "food_category",
               values_to = "co2_emission_kg_person_year") %>% 
  
  #Recode values for food category
  mutate(food_category = recode(food_category,
                                "Lamb & Goat" = "Lamb and Goat",
                                "Milk - inc. cheese" = "Milk including Cheese",
                                "nuts_inc_peanut_butter" = "Nuts including Peanut Butter"))



########## Final figure

#Bar plot of top 15 countries with total annual CO2 emissions from consumption with food
ggplot(data = annual_co2_country_food_long, 
       aes(x = co2_emission_kg_person_year,
           y = country,
           
           #Color by food category
           fill = food_category)) +
  
  #Define stacked column/bar plot
  #Values for each food summed up within each bar
  #Bars colored based off how much of that total sum is in each food
  geom_col(stat = "identity") +
  
  #Add text for x-values
  #Adjust horizontal text placement
  #Adjust text size
  geom_text(data = top15_total_co2_by_country,
            aes(x = total_annual_co2_kg_person_year,
                y = country,
                label = total_annual_co2_kg_person_year,
                fill = NULL),
            hjust = -0.1, size = 2.7, color = "darkgrey") +
  
  #Change colors
  scale_fill_paletteer_d(palette = "unikn::pal_unikn_pair") +
  
  #Change x-axis and y-axis labels
  labs(x = expression(atop(paste(bold("Total Annual "),
                                 bold(CO[2]),
                                 bold(" Emissions from Food Consumption")),
                           paste(bold("(kg/person/year)")))),
       y = "Country",
       fill = "Food Category") +

  #Change theme
  theme_minimal() +
  
  #Change x-axis scale
  scale_x_continuous(breaks = seq(0, 2500, by = 500)) +
  
  #Focus plot area on limits and allow text outside margins
  coord_cartesian(xlim = c(0, 2500), clip = "off") +

  #Customize plot further
  #Bold and increase font of ]y-axis label
  theme(axis.title.y = element_text(size = 12, face = "bold"),
        
        #Move x-label down from plot
        axis.title.x = element_text(vjust = -3),
        
        #Bold and change size of legend title
        #Change size of legend text
        legend.title = element_text(face = "bold", size = 9),
        legend.text = element_text(size = 8.5),
        
        #Increase x-tick and y-tick text
        axis.text.x = element_text(size = 10),
        axis.text.y = element_text(size = 10),
        
        #Increase margins: theme_minimal()$plot.margin
        #Top, right, bottom, left
        plot.margin = unit(c(0.3, 0.3, 0.6, 0.3), "cm"))


Figure 1. Top 15 countries with the highest total annual carbon dioxide (\(CO_2\)) emissions (kg/person/year) based on diet or food consumption from the year 2018. Grey text to the right of the bars in the plot report the value for the total annual \(CO_2\) emissions (kg/person/year) for each country. The foods that generate the highest \(CO_2\) emissions are animal products that include beef, lamb and goat, and milk and cheese (dairy products). Foods that generate lower \(CO_2\) emissions are non-animal products, such as nuts, soybeans, and rice. Data source: nu3 (2018) & Kulma (2020).

References

Data and Literature

nu3. Food carbon footprint index 2018. https://www.nu3.de/blogs/nutrition/food-carbon-footprint-index-2018.

Kulma, K. (2020). Webscraping with R - from messy & unstructured to blisfully tidy. https://r-tastic.co.uk/post/from-messy-to-tidy/.

R Libraries

Firke, S. (2021). janitor: Simple Tools for Examining and Cleaning Dirty Data. R package version 2.1.0. https://CRAN.R-project.org/package=janitor.

Hvitfeldt, E. (2021). paletteer: Comprehensive Collection of Color Palettes. version 1.3.0. https://github.com/EmilHvitfeldt/paletteer.

Wickham et al., (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686, https://doi.org/10.21105/joss.01686.

Citation

For attribution, please cite this work as

Yu (2022, May 9). SY: Data Visualization: Carbon Dioxide Emissions from Food Consumption. Retrieved from https://esswhy.github.io/portfolio/dataviz_food_co2/

BibTeX citation

@misc{yu2022data,
  author = {Yu, Shuying},
  title = {SY: Data Visualization: Carbon Dioxide Emissions from Food Consumption},
  url = {https://esswhy.github.io/portfolio/dataviz_food_co2/},
  year = {2022}
}