Share of renewable energy production in the world

Getting the data & Data description

The National Bureau of Economic Research (NBER) has a a very interesting dataset on the adoption of about 200 technologies in more than 150 countries since 1800. This is theCross-country Historical Adoption of Technology (CHAT) dataset.

The following is a description of the variables

variable class description
variable character Variable name
label character Label for variable
iso3c character Country code
year double Year
group character Group (consumption/production)
category character Category
value double Value (related to label)
technology <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-07-19/technology.csv')

#get all technologies
labels <- technology %>% 
  distinct(variable, label)

# Get country names using 'countrycode' package
technology <- technology %>% 
  filter(iso3c != "XCD") %>% 
  mutate(iso3c = recode(iso3c, "ROM" = "ROU"),
         country = countrycode(iso3c, origin = "iso3c", destination = "country.name"),
         country = case_when(
           iso3c == "ANT" ~ "Netherlands Antilles",
           iso3c == "CSK" ~ "Czechoslovakia",
           iso3c == "XKX" ~ "Kosovo",
           TRUE           ~ country))

#make smaller dataframe on energy
energy <- technology %>% 
  filter(category == "Energy")

# download CO2 per capita from World Bank using {wbstats} package
# https://data.worldbank.org/indicator/EN.ATM.CO2E.PC
co2_percap <- wb_data(country = "countries_only", 
                      indicator = "EN.ATM.CO2E.PC", 
                      start_date = 1970, 
                      end_date = 2022,
                      return_wide=FALSE) %>% 
  filter(!is.na(value)) %>% 
  #drop unwanted variables
  select(-c(unit, obs_status, footnote, last_updated))

# get a list of countries and their characteristics
# we just want to get the region a country is in and its income level
countries <-  wb_cachelist$countries %>% 
  select(iso3c,region,income_level)

Produce the barplot gragh with the highest and lowest % contribution of renewables in energy production.

new_energy <- energy %>%
  filter(year == 2019) %>% 
  group_by(country, variable) %>% 
  summarise(count = sum(value)) %>% 
  ungroup() %>% 
  pivot_wider(names_from = "variable", values_from = "count") %>% 
  mutate(renew_energy = elec_hydro + elec_solar + elec_wind + elec_renew_other)
  
new_energy[is.na(new_energy)] <- 0

new_energy <- new_energy %>% 
  mutate(percent = renew_energy / elecprod*100) %>% 
  arrange(desc(percent)) %>% 
  filter(renew_energy != 0, percent != Inf) 

p1 <- ggplot(new_energy %>% slice_max(order_by = percent, n = 20), aes(x = percent, 
             y = fct_reorder(country, percent))) +
  geom_col() + 
  labs(title = "Highest and lowest % of renewables in energy production",
       subtitle = "2019 data ", 
       y = NULL, 
       x = NULL, 
       caption = NULL) +
  scale_x_continuous(labels=scales::percent)

p2 <- ggplot(new_energy %>% slice_min(order_by = percent, n = 20), aes(x = percent, 
             y = fct_reorder(country, percent))) +   
  geom_col() + 
  labs(title = NULL,
       subtitle = NULL, 
       y = NULL, 
       x = NULL, 
       caption = "Source: NBER CHAT Database") +
  scale_x_continuous(labels=scales::percent)

p1 + p2

Produce an animation to explore the relationship between CO2 per capita emissions and the deployment of renewables.

new_co2_percap <- merge(co2_percap, countries, by="iso3c") # merge all the data into one dataset
new_co2_percap <- merge(new_co2_percap, new_energy, by="country" )
data <- new_co2_percap[,c(1,2,6,7,9,21)]

ggplot(data, aes(x=percent, y=value, color=income_level)) + 
  geom_point() + facet_wrap(~income_level, nrow = 2)  + 
  labs(title = 'Year: {round(frame_time,0)}', 
       x = '% renewables', 
       y = 'CO2 per cap') +
  transition_time(date) +
  ease_aes('linear') + theme(legend.position = "none")