knitr::opts_chunk$set( collapse = TRUE, comment = "#>", out.width = "100%", fig.path = "man/figures/README-", fig.width = 7, fig.height = 4, dpi = 150, message = FALSE, warning = FALSE, error = FALSE ) library(eia) library(ggplot2)
The eia
package provides API access to data from the US Energy Information Administration (EIA).
Pulling data from the US Energy Information Administration (EIA) API requires a registered API key. A key can be obtained at no cost here. A valid email and agreement to the API Terms of Service is required to obtain a key.
eia
includes functions for searching the EIA API data directory and importing various datasets.
Datasets returned by these functions are provided in a tidy format or alternatively in more raw form.
It also offers helper functions for working with EIA API date strings and time formats and for
inspecting different summaries of data metadata. The package also provides control over API key
storage and caching of API request results.
Install the CRAN release of eia
with
install.packages("eia")
or install the development version from GitHub with
# install.packages("remotes") remotes::install_github("ropensci/eia")
After obtaining the API key, store it somewhere such as .Renviron
and never have
to do anything with the key when using the package. Alternatively, set it manually
with eia_set_key()
in the current R session. Further, it can always be passed
explicitly to the key
argument of a given eia
function.
library(eia) # not run eia_set_key("yourkey") # set API key if not already set globally
Get a list of the EIA's data directory (and sub-directories) with eia_dir()
.
# Top-level directory eia_dir() # Electricity sub-directory eia_dir("electricity")
Get annual retail electric sales for the Ohio residential sector since 2010
(d <- eia_data( dir = "electricity/retail-sales", data = "sales", facets = list(stateid = "OH", sectorid = "RES"), freq = "annual", start = "2010", sort = list(cols = "period", order = "asc"), ))
and make a nice plot.
library(ggplot2) ggplot(d, aes(x = period, y = sales / 1e3)) + geom_bar(col = "steelblue", fill = "steelblue", stat = "identity") + theme_bw() + labs( title = "Annual Retail Sales of Electricity (GWh)", subtitle = "State: Ohio; Sector: Residential", x = "Year", y = "Sales (GWh)" )
See the collection of vignette tutorials and examples as well as complete package
documentation available at the eia
package website.
Please note that the eia
project is released with
a Contributor Code of Conduct.
By contributing to this project, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.