-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
technology-adoption.R
101 lines (88 loc) · 3.76 KB
/
technology-adoption.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Loading libraries
library(tidytuesdayR)
library(countrycode)
library(tidyverse)
library(ggthemes)
# Loading data
tt <- tt_load("2022-07-19")
# Printing a summary of tt$technology
tt$technology
# Printing the distinct "variable" and "label" pairs for the "Energy" category
## This will be used as a reference to create the "energy_type" column/variable
tt$technology %>% filter(category == "Energy") %>% select(variable, label) %>%
distinct()
# Setting a seed to make results reproducible
set.seed("20220719")
# Using sample() to select six rows of tt$technology at random
sample_rows <- sample(x = rownames(tt$technology), size = 6)
# Creating a subset using the random rows
technology_sample <- tt$technology[sample_rows, ]
# Printing a summary of the randomly sampled subset
technology_sample
# Adding continent and country name columns/variables to the sample subset,
# using the countrycode::countrycode() function
technology_sample <- technology_sample %>%
mutate(continent = countrycode(iso3c, origin = "iso3c",
destination = "continent"),
country = countrycode(iso3c, origin = "iso3c", destination = "country.name"))
# Selecting the country ISO code, continent and country name of the sample
# subset, to confirm that countrycode() worked as intended
technology_sample %>% select(iso3c, continent, country)
# Adding the corresponding continent for each country in tt$technology;
# filtering to select for the "Energy" category; adding a more succinct
# "energy_type" variable; and dropping rows with missing values
energy_tbl <- tt$technology %>%
mutate(continent = countrycode(iso3c, origin = "iso3c",
destination = "continent")) %>%
filter(category == "Energy") %>%
mutate(energy_type = fct_recode(variable,
"Consumption" = "elec_cons", "Coal" = "elec_coal", "Gas" = "elec_gas",
"Hydro" = "elec_hydro", "Nuclear" = "elec_nuc", "Oil" = "elec_oil",
"Other renewables" = "elec_renew_other", "Solar" = "elec_solar",
"Wind" = "elec_wind", "Output" = "elecprod",
"Capacity" = "electric_gen_capacity")) %>%
drop_na()
# Printing a summary of energy_tbl
energy_tbl
# Filtering energy_table for fossil fuel rows
fossil_fuel_tbl <- energy_tbl %>%
filter(energy_type != "Consumption" & energy_type != "Output"
& energy_type != "Capacity") %>%
filter(energy_type == "Coal" | energy_type == "Gas" | energy_type == "Oil")
# Printing a summary of the tibble
fossil_fuel_tbl
# Filtering energy_table for low-carbon energy source rows
low_carbon_tbl <- energy_tbl %>%
filter(energy_type != "Consumption" & energy_type != "Output"
& energy_type != "Capacity") %>%
filter(energy_type != "Coal" & energy_type != "Gas" & energy_type != "Oil")
# Printing a summary of the tibble
low_carbon_tbl
# Plotting distributions of electricity produced from fossil fuels
fossil_fuel_tbl %>%
ggplot(aes(x = fct_reorder(energy_type, value), y = value, fill = energy_type)) +
geom_boxplot() +
theme_solarized() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none") +
scale_colour_discrete() +
scale_y_log10() +
facet_wrap(~continent, scales = "free") +
labs(
title = "Electricity generated from fossil fuels by continent",
y = "Output in log terawatt-hours: log10(TWh)",
x = "Source")
# Plotting distributions of electricity produced from low-carbon sources
low_carbon_tbl %>%
ggplot(aes(x = fct_reorder(energy_type, value), y = value, fill = energy_type)) +
geom_boxplot() +
theme_solarized() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "none") +
scale_colour_discrete() +
scale_y_log10() +
facet_wrap(~continent, scales = "free") +
labs(
title = "Electricity generated from low-carbon sources by continent",
y = "Output in log terawatt-hours: log10(TWh)",
x = "Source")