{"cells":[{"metadata":{},"cell_type":"markdown","source":"**This kernel intends to prepare and explore power plant emissions factor data in US. **\n\nThe data is gathered from [https://www.eia.gov/electricity/data/state/](https://www.eia.gov/electricity/data/state/)"},{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"library(tidyverse)\nlibrary(readxl)\nlibrary(openintro)\nlibrary(repr)\nlibrary(ggrepel)\nlibrary(hrbrthemes)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Preparation"},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"# electricity generation\nexc_el <- read_excel(\"../input/ds4g-additional-dataset/us_electricity/annual_generation_state.xls\", range = \"A2:E51635\") %>% \n  mutate(STATE = toupper(STATE)) %>% \n  filter(!is.na(STATE))\n# electricity emissions\nexc_emm <- read_excel(\"../input/ds4g-additional-dataset/us_electricity/emission_annual.xls\") %>% \n  mutate(State = toupper(State))\n# population\nexc_pop <- read_excel(\"../input/ds4g-additional-dataset/us_electricity/nst-est2018-02.xlsx\", \n                      range = \"A11:C61\", col_names = c(\"state\", \"pop_2010\", \"pop_2018\"))\nexc_pop <- exc_pop %>% \n  mutate(state_full = gsub(\"\\\\.\", \"\", state),\n         state = state2abbr(state_full))\n\nnames(exc_el) <- c(\"year\", \"state\", \"producer_type\", \"energy_source\", \"mwh\")\nnames(exc_emm) <- c(\"year\", \"state\", \"producer_type\", \"energy_source\", \"co2\", \"so2\", \"nox\")\n\n# nationwide --------------------------------------------------------------\nsummary_ef <- exc_el %>% \n  filter(state == \"US-TOTAL\", \n         energy_source != \"Total\",\n         energy_source != \"Pumped Storage\") %>% \n  group_by(year, state, energy_source) %>% \n  summarise(mwh = sum(mwh)) %>% \n  ungroup() %>% \n  left_join(exc_emm %>% \n              filter(state == \"US-TOTAL\", energy_source != \"All Sources\") %>% \n              group_by(year, state, energy_source) %>% \n              summarise(co2 = sum(co2),\n                        so2 = sum(so2),\n                        nox = sum(nox)) %>% \n              ungroup()) %>% \n  mutate_at(vars(co2:nox), funs(replace_na(., 0))) %>% \n  mutate(co2_ef = (co2 * 10^6) / (mwh * 1000) ,\n         so2_ef = (so2 * 10^6) / (mwh * 1000),\n         nox_ef = (nox * 10^6) / (mwh * 1000))\n\n\n# state -------------------------------------------------------------------\nsummary_ef_state <- exc_el %>% \n  filter(state != \"US-TOTAL\", \n         energy_source != \"Total\",\n         energy_source != \"Pumped Storage\") %>% \n  group_by(year, state, energy_source) %>% \n  summarise(mwh = sum(mwh)) %>% \n  ungroup() %>% \n  left_join(\n    exc_emm %>% \n      filter(state != \"US-TOTAL\", energy_source != \"All Sources\") %>% \n      group_by(year, state, energy_source) %>% \n      summarise(co2 = sum(co2),\n                so2 = sum(so2),\n                nox = sum(nox)) %>% \n      ungroup()\n  ) %>% \n  mutate_at(vars(co2:nox), funs(replace_na(., 0))) %>% \n  mutate(co2_ef = (co2 * 10^6) / (mwh * 1000),\n         so2_ef = (so2 * 10^6) / (mwh * 1000),\n         nox_ef = (nox * 10^6) / (mwh * 1000))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tail(summary_ef)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tail(summary_ef_state)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Exploration"},{"metadata":{},"cell_type":"markdown","source":"### Correlation between Pollutants"},{"metadata":{"trusted":true},"cell_type":"code","source":"summary_ef_state %>% \n  filter(co2 > 0, so2 > 0, nox > 0, co2_ef > 0) %>% \n  select(co2:nox) %>% \n  cor()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Emissions Factor per Pollutants"},{"metadata":{"trusted":true},"cell_type":"code","source":"summary_ef_source <- summary_ef %>% \n  filter(energy_source %in% c(\"Coal\", \"Natural Gas\", \"Petroleum\")) %>% \n  group_by(year, energy_source) %>% \n  summarise_at(vars(mwh, co2:nox), sum) %>% \n  ungroup() %>% \n  mutate(co2_ratio = co2 / (co2+so2+nox),\n         so2_ratio = so2 / (co2+so2+nox),\n         nox_ratio = nox / (co2+so2+nox),\n         `CO2` = co2 * 1000 / mwh,\n         `SO2` = so2 * 1000 / mwh,\n         `NOx` = nox * 1000 / mwh)\n\noptions(repr.plot.width=14, repr.plot.height = 5)\nsummary_ef_source_long <- summary_ef_source %>% \n  filter(year >= 2002) %>% \n  select(year, energy_source, CO2, SO2, NOx) %>% \n  gather(key, val, -year, -energy_source) \nsummary_ef_source_long %>% \n  ggplot(aes(year, val, color = energy_source)) +\n  geom_point() +\n  geom_line() +\n  geom_label_repel(data = filter(summary_ef_source_long, (year + 2) %% 4 == 0),\n                   aes(label = round(val, 2), fill = energy_source,), color = \"white\", family = \"IBM Plex Sans\") +\n  facet_wrap(~key, scales = \"free\", nrow = 1) +\n  scale_color_ipsum(\"Energy Source\") +\n  scale_fill_ipsum(\"Energy Source\") +\n  scale_x_continuous(breaks = seq(2002,2018,4)) +\n  theme_ipsum_ps(grid = \"XY\") +\n  theme(legend.position = \"top\") +\n  labs(x = \"Year\", y = \"EF (g/kWh)\", \n       title = \"Power Plant Emissions Factor in US 2002-2018\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Emissions factor of NOx and SO2 is decreasing for each type energy source. However, emissionss factor of CO2 is increasing for Coal and Petroleum. We also can notice that Natural Gas does not produce SO2 emissions."},{"metadata":{"trusted":true},"cell_type":"code","source":"summary_ef_source %>% \n  filter(year == 2018) %>% \n  select(energy_source, NOx) %>% \n  mutate(ratio = NOx / 0.2687253) %>% \n  arrange(desc(ratio))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Petroleum has the highest emissions factor (6x compared to Natural Gas). Even though emissions factor in US and Puerto Rico might be different, we can use this number as initial baseline to validate NOx emissions factor estimation in Puerto Rico using remote sensing approach."}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":4}