knitr::opts_chunk$set(echo = TRUE)

# Data analysis libraries
library(tidyverse)
library(magrittr) # extended pipe functionality
library(lubridate) # dealing with date objects
library(here) # for OS-agnostic filepaths
library(readxl) # for reading Excel files

# Visualization libraries
library(ggrepel) # for neater text labels
library(gghighlight) # for easier highlighting
library(geofacet) # for making facets looks like the USA
library(ggridges)
library(extrafont) # for... extra fonts
library(gridExtra) # for combining plots

# Geospatial tools and related libraries
library(sf)
library(zipcode) # for geocoding zipcodes
library(noncensus) # for more geocoding data 
library(rnaturalearth)
library(rnaturalearthdata)

# Load previously-imported data
# Data assemby process captured in /scripts/data-import.R
load(here('data', 'solar_data_master.RData'))
# Set custom theme
theme_custom <- function(base_size = 12, base_family = "", 
                         base_line_size = base_size/22,
                         base_rect_size = base_size/22) {
  theme_minimal(base_size = base_size, base_family = base_family,
             base_line_size = base_line_size,
             base_rect_size = base_rect_size) %+replace%
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_line(color = "#EEEEEE"),
          plot.title = element_text(family = "Asap SemiBold",
                                    size = 16,
                                    hjust = 0,
                                    margin = margin(5, 0, 5, 0),
                                    color = "#0F0F0F"),
          plot.subtitle = element_text(family = "Asap",
                                       size = 12,
                                       hjust = 0,
                                       margin = margin(0, 0, 10, 0),
                                       color = "#3F3F3F"),
          plot.caption = element_text(family = "Asap",
                                      face = "italic",
                                      size = 8,
                                      hjust = 1,
                                      margin = margin(5, 0, 0, 0)),
          axis.ticks = element_blank(),
          axis.title = element_text(family = "Asap SemiBold",
                                   size = 12,
                                   face = "plain",
                                   margin = margin(10, 10, 10, 10)),
          axis.text.x = element_text(family = "Asap",
                                     size = 10,
                                     face = "plain",
                                     margin = margin(0, 0, 5, 0)),
          axis.text.y = element_text(family = "Asap",
                                     size = 10,
                                     face = "plain",
                                     margin = margin(0, 0, 0, 5)),          
          legend.title = element_text(family = "Asap SemiBold",
                                      size = 10,
                                      color = "#3F3F3F"),
          legend.text = element_text(family = "Asap",
                                     size = 10,
                                     color = "#3F3F3F"),
          strip.text = element_text(family = "Asap Medium",
                                    size = 10,
                                    color = "#3F3F3F",
                                    margin = margin(10, 10, 10, 10)))
}

# Set custom colors
theme_purple <- "#5D2BF0"
theme_orange <- "#FF810F"
theme_green <- "#4DAF4A"
theme_blue <- "#377EB8"
theme_bg_gray <- "#DDDDDD"
text_black <- "#000000"

Introduction

Investment in renewable energy is such a fascinating policy area. It’s one of the few that feature the interplay of energy as a national utility, yet with semi-reliance on individuals and corporations as contributors to industry supply - e.g. in installing solar panels or, at a greater scale, solar farms.

Much of the national conversation around renewable energy today tends to revolve around switching from gasoline power to low-emission alternatives (say, a regular petrol car vs. a Tesla, or a city transit system switching to electric buses). In many cases, this makes sense - the Department of Energy states that “electricity is less expensive than gasoline and [electric vehicles] are more efficient than gasoline vehicles”.

More efficient or not, however, this still shifts the energy load from gasoline to the electrical grid, which itself is mostly generated from non-renewable sources. The generation of energy from renewable sources is a critical part of reducing emissions when we as a species increasingly consume more and more energy (looking at you, bitcoin miners). The question I’m more preoccupied with is: where is all this energy coming from?

Where all this energy is coming from

First, we’ll take a look at data on solar photovoltaic energy generated by the commercial, industrial, power and residential sectors, courtesy of the U.S. Energy Information Administration’s State Energy Data Systems (SEDS) database.

Why focus on solar energy? Of all the common sources of renewable energy - solar, wind, hydroelectric, geothermal, etc. - solar energy fascinates me because it’s so participatory. You can’t install a wind turbine in your backyard or dam up the local river, but you could invest in a set of solar panels for your house fairly easily. In the SEDS data, solar energy is also the only renewable source of energy to have a line item for “generation by small-scale applications in the residential sector”.

gen %>%
  group_by(Year, source, energy_type) %>%
  summarize(total_btu = sum(Data)) %>% 
  mutate(full_source = paste0(energy_type, ", ", source)) %>%
  mutate(highlight_col = ifelse(source == "Residential", "2",
                              ifelse(energy_type == "Solar", "1", "0"))) %>%
  ##### PLOT BEGINS
  ggplot(aes(x = Year, y = total_btu, group = full_source, color = highlight_col)) +
  geom_line(show.legend = FALSE, 
            na.rm = TRUE) +
  scale_color_manual(values = c(theme_bg_gray, theme_orange, theme_purple)) +
  labs(title = "Residential systems are the 2nd-largest source of solar energy",
       subtitle = "While utility-owned solar farms were the largest generators of solar energy in 2016, small-scale \nresidential systems overtook the commercial sector in 2014 to become the 2nd-largest source \nof solar energy and the 5th-largest source of renewable energy overall.",
       x = "Year",
       y = "Log Annual Energy Generated (BTUs)",
       caption = "Data source: SEDS (U.S. Energy Information Administration)") +
  geom_text_repel(data = . %>% filter(Year == 2016),
                  aes(x = Year, y = total_btu, label = full_source),
                  size = 3,
                  hjust = 0,
                  nudge_x = 1,
                  family = "Asap Medium",
                  show.legend = FALSE,
                  na.rm = TRUE) +
  scale_y_log10(breaks = c(10, 100, 1000, 10000, 100000),
                labels = c("10", "100", "1K", "10K", "100K")) +
  scale_x_continuous(limits = c(1960, 2030),
                     breaks = seq(1960, 2010, 10)) +
  annotate(geom = "text",
           x = 2017, y = 5,
           label = "Solar energy is the only \nrenewable energy source in \nthe data with a Residential \ncategory.",
           size = 3,
           lineheight = 1,
           color = "#000000",
           hjust = 0, vjust = 1,
           family = "Asap",
           fontface = "italic") +
  theme_custom() +
  theme(panel.grid.major.x = element_blank())