Carbon Emissions Data from the US Energy Information Administration

The US Energy Information Administration (EIA) has lots of data and information on carbon emissions.

Load the R packages

# install.packages('pacman') #uncomment if don't already have
library(pacman) # package manager
p_load(eia, hrbrthemes, tidyverse)

eia package

For background and examples using the eia package go here. Follow link on that page to get an API key.

Put the API key in the variable my_eia_api_key in the .Renviron file. (See Environment variables here)

my_eia_api_key <- Sys.getenv("my_eia_api_key")
eia_set_key(my_eia_api_key)

Identify the series ids

Identify the series ids that you want to retreive data for. The series id for Total carbon dioxide emissions from all sectors, all fuels, United States is EMISS.CO2-TOTV-TT-TO-US.A.

id_tot <- "EMISS.CO2-TOTV-TT-TO-US.A"
id_coal <- "EMISS.CO2-TOTV-TT-CO-US.A"
id_natural <- "EMISS.CO2-TOTV-TT-NG-US.A"
id_pet <- "EMISS.CO2-TOTV-TT-PE-US.A"

Assign to vector ids

ids <- c(id_tot, id_coal, id_natural, id_pet)

carbon_series <- eia_series(ids)

carbon_series %>% glimpse()
## Observations: 4
## Variables: 14
## $ series_id   <chr> "EMISS.CO2-TOTV-TT-TO-US.A", "EMISS.CO2-TOTV-TT-CO-US.A",…
## $ name        <chr> "Total carbon dioxide emissions from all sectors, all fue…
## $ units       <chr> "million metric tons CO2", "million metric tons CO2", "mi…
## $ f           <chr> "A", "A", "A", "A"
## $ unitsshort  <chr> "mmt CO2", "mmt CO2", "mmt CO2", "mmt CO2"
## $ description <chr> "See http://www.eia.gov/environment/emissions/state/analy…
## $ copyright   <chr> "None", "None", "None", "None"
## $ source      <chr> "EIA, U.S. Energy Information Administration", "EIA, U.S.…
## $ iso3166     <chr> "USA-US", "USA-US", "USA-US", "USA-US"
## $ geography   <chr> "USA-US", "USA-US", "USA-US", "USA-US"
## $ start       <chr> "1980", "1980", "1980", "1980"
## $ end         <chr> "2017", "2017", "2017", "2017"
## $ updated     <chr> "2019-10-30T13:20:11-0400", "2019-10-30T13:20:11-0400", "…
## $ data        <list> [<tbl_df[38 x 3]>, <tbl_df[38 x 3]>, <tbl_df[38 x 3]>, <…
carbon_series %>% select(series_id, data)
## # A tibble: 4 x 2
##   series_id                 data             
##   <chr>                     <list>           
## 1 EMISS.CO2-TOTV-TT-TO-US.A <tibble [38 × 3]>
## 2 EMISS.CO2-TOTV-TT-CO-US.A <tibble [38 × 3]>
## 3 EMISS.CO2-TOTV-TT-NG-US.A <tibble [38 × 3]>
## 4 EMISS.CO2-TOTV-TT-PE-US.A <tibble [38 × 3]>
carbon_series %>%
  select(series_id, data) %>%
  filter(series_id != "EMISS.CO2-TOTV-TT-TO-US.A") # don't include total in graph
## # A tibble: 3 x 2
##   series_id                 data             
##   <chr>                     <list>           
## 1 EMISS.CO2-TOTV-TT-CO-US.A <tibble [38 × 3]>
## 2 EMISS.CO2-TOTV-TT-NG-US.A <tibble [38 × 3]>
## 3 EMISS.CO2-TOTV-TT-PE-US.A <tibble [38 × 3]>
carbon_series %>%
  select(series_id, data) %>%
  filter(series_id != "EMISS.CO2-TOTV-TT-TO-US.A") %>%
  unnest(cols = data)
## # A tibble: 114 x 4
##    series_id                 value date        year
##    <chr>                     <dbl> <date>     <int>
##  1 EMISS.CO2-TOTV-TT-CO-US.A 1320. 2017-01-01  2017
##  2 EMISS.CO2-TOTV-TT-CO-US.A 1357. 2016-01-01  2016
##  3 EMISS.CO2-TOTV-TT-CO-US.A 1483. 2015-01-01  2015
##  4 EMISS.CO2-TOTV-TT-CO-US.A 1717. 2014-01-01  2014
##  5 EMISS.CO2-TOTV-TT-CO-US.A 1721. 2013-01-01  2013
##  6 EMISS.CO2-TOTV-TT-CO-US.A 1658. 2012-01-01  2012
##  7 EMISS.CO2-TOTV-TT-CO-US.A 1876. 2011-01-01  2011
##  8 EMISS.CO2-TOTV-TT-CO-US.A 1987. 2010-01-01  2010
##  9 EMISS.CO2-TOTV-TT-CO-US.A 1879. 2009-01-01  2009
## 10 EMISS.CO2-TOTV-TT-CO-US.A 2135. 2008-01-01  2008
## # … with 104 more rows
carbon_series %>%
  select(series_id, data) %>%
  filter(series_id != "EMISS.CO2-TOTV-TT-TO-US.A") %>%
  unnest(cols = data) %>%
  select(-date) -> df

df
## # A tibble: 114 x 3
##    series_id                 value  year
##    <chr>                     <dbl> <int>
##  1 EMISS.CO2-TOTV-TT-CO-US.A 1320.  2017
##  2 EMISS.CO2-TOTV-TT-CO-US.A 1357.  2016
##  3 EMISS.CO2-TOTV-TT-CO-US.A 1483.  2015
##  4 EMISS.CO2-TOTV-TT-CO-US.A 1717.  2014
##  5 EMISS.CO2-TOTV-TT-CO-US.A 1721.  2013
##  6 EMISS.CO2-TOTV-TT-CO-US.A 1658.  2012
##  7 EMISS.CO2-TOTV-TT-CO-US.A 1876.  2011
##  8 EMISS.CO2-TOTV-TT-CO-US.A 1987.  2010
##  9 EMISS.CO2-TOTV-TT-CO-US.A 1879.  2009
## 10 EMISS.CO2-TOTV-TT-CO-US.A 2135.  2008
## # … with 104 more rows
df %>%
  ggplot(aes(year, value, color = series_id)) +
  geom_line() +
  theme_ft_rc() +
  scale_y_continuous(limits = c(0, 3000)) +
  scale_x_continuous(
    breaks = seq(from = 1980, to = 2018, by = 5),
    minor_breaks = seq(from = 1980, to = 2018, by = 1)
  ) +
  scale_color_ft(labels = c("coal", "natural gas", "petroleum")) +
  labs(
    title = "US total carbon dioxide emissions",
    subtitle = "from all sectors by fuel type",
    y = carbon_series$units[1],
    color = NULL
  ) +
  theme(legend.position = "bottom")

Avatar
Elizabeth Stanny
Professor of Business Administration

My research interests include climate change and political disclosures.

Related