forked from dgrtwo/data-screencasts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2020_08_04_europe_energy.Rmd
187 lines (156 loc) · 5.2 KB
/
2020_08_04_europe_energy.Rmd
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
---
title: "TidyTemplate"
date: 2020-08-04
output: html_output
---
# TidyTuesday
Join the R4DS Online Learning Community in the weekly #TidyTuesday event!
Every week we post a raw dataset, a chart or article related to that dataset, and ask you to explore the data.
While the dataset will be “tamed”, it will not always be tidy! As such you might need to apply various R for Data Science techniques to wrangle the data into a true tidy format.
The goal of TidyTuesday is to apply your R skills, get feedback, explore other’s work, and connect with the greater #RStats community!
As such we encourage everyone of all skills to participate!
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tidytuesdayR)
library(scales)
theme_set(theme_light())
```
# Load the weekly Data
Dowload the weekly data and make available in the `tt` object.
```{r Load}
tt <- tt_load("2020-08-04")
energy_types <- tt$energy_types %>%
pivot_longer(cols = starts_with("2"),
names_to = "year",
values_to = "gigawatt_hours") %>%
mutate(year = as.integer(year)) %>%
replace_na(list(country_name = "United Kingdom")) %>%
mutate(country = ifelse(country == "UK", "GB", country),
country = ifelse(country == "EL", "GR", country),
country = str_to_lower(country))
energy_types %>%
count(level, type, sort = TRUE)
energy_types %>%
count(country_name, sort = TRUE)
```
What will we do today?
* Graph of energy consumption by type over time
* Map
* Not an animation
```{r}
tt$country_totals %>%
count(type, sort = TRUE)
```
We may look at exports, imports
### Total energy consumption in Europe
```{r}
europe_totals <- energy_types %>%
filter(level == "Level 1") %>%
group_by(year, type) %>%
summarize(total_power = sum(gigawatt_hours)) %>%
ungroup() %>%
mutate(type = fct_reorder(type, total_power, sum))
europe_totals %>%
ggplot(aes(year, total_power, fill = type)) +
geom_col() +
scale_y_continuous(labels = comma) +
labs(x = "Year",
y = "Total power production (gigawatt-hours)",
fill = "Type")
europe_totals %>%
ggplot(aes(year, total_power)) +
geom_col() +
scale_y_continuous(labels = comma) +
labs(x = "Year",
y = "Total power production (gigawatt-hours)") +
facet_wrap(~ type, scales = "free_y")
```
```{r}
europe_totals %>%
filter(year == 2018) %>%
mutate(type = fct_reorder(type, total_power, sum)) %>%
ggplot(aes(total_power, type)) +
geom_col() +
scale_x_continuous(labels = comma) +
labs(x = "Total power production in 2018 in Europe (gigawatt-hours)")
```
```{r}
library(tidytext)
# devtools::install_github("rensa/ggflags")
library(ggflags)
data_prepared <- energy_types %>%
filter(level == "Level 1",
year == 2018,
gigawatt_hours > 0,
type != "Other") %>%
group_by(type) %>%
mutate(country_name = fct_lump(country_name, 10, w = gigawatt_hours),
country = as.character(fct_lump(country, 10, w = gigawatt_hours))) %>%
mutate(country_name = reorder_within(country_name, gigawatt_hours, type, fun = sum),
type = fct_reorder(type, -gigawatt_hours, sum))
data_prepared %>%
filter(country != "Other") %>%
mutate(country = str_to_lower(country)) %>%
ggplot(aes(gigawatt_hours, country_name)) +
geom_col(width = .1) +
geom_flag(aes(country = country)) +
scale_y_reordered() +
facet_wrap(~ type, scales = "free") +
scale_x_continuous(labels = comma) +
scale_country(guide = FALSE) +
theme_minimal() +
theme(panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank()) +
labs(x = "Total power production in 2018 (gigawatt-hours)",
y = "")
```
Try ggflag!
```{r}
plot_slopegraph <- function(tbl) {
tbl %>%
filter(gigawatt_hours > 1) %>%
ggplot(aes(year, gigawatt_hours)) +
geom_line(aes(group = country)) +
geom_flag(aes(country = country)) +
geom_text(aes(label = ifelse(year == 2017, NA, country_name),
hjust = ifelse(year == 2016, 1.2, -.2)),
check_overlap = TRUE) +
scale_x_continuous(breaks = c(2016, 2018),
limits = c(2015, 2019)) +
theme(panel.grid = element_blank()) +
labs(x = "",
y = "Gigawatt hours produced in this year")
}
energy_types %>%
filter(type == "Wind") %>%
plot_slopegraph() +
scale_y_continuous(labels = comma) +
# scale_y_log10(labels = comma) +
labs(title = "Wind power production over time by country")
energy_types %>%
filter(type == "Solar") %>%
plot_slopegraph() +
scale_y_continuous(labels = comma) +
# scale_y_log10(labels = comma) +
labs(title = "Solar power production over time by country")
energy_types %>%
filter(type %in% c("Solar", "Wind", "Conventional thermal", "Hydro")) %>%
group_by(type) %>%
filter(fct_lump(country, 10, w = gigawatt_hours) != "Other") %>%
ungroup() %>%
plot_slopegraph() +
scale_y_continuous(labels = comma) +
facet_wrap(~ type, scales = "free_y") +
# scale_y_log10(labels = comma) +
labs(title = "Solar power production over time by country")
```
```{r}
energy_types %>%
filter(level == "Level 1",
year == 2018,
gigawatt_hours > 0) %>%
ggplot(aes(type, gigawatt_hours)) +
geom_point() +
geom_flag(aes(country = country))
```