Skip to content

Instantly share code, notes, and snippets.

@k5cents
Forked from cavedave/coalpower.r
Last active May 28, 2019 00:26
Show Gist options
  • Save k5cents/88a94ed9e179385aa2c51b81262d51f4 to your computer and use it in GitHub Desktop.
Save k5cents/88a94ed9e179385aa2c51b81262d51f4 to your computer and use it in GitHub Desktop.
Visualizing UK Coal Usage (Simplified Code)
# this script automates the collection, processing, and plotting of gridwatch energy data
# 2019-05-27
# @kiernann
# u/WannabeWonk
# install.packages("pacman")
pacman::p_load(
RSelenium,
readr,
dplyr,
lubridate,
ggplot2
)
# this is a remake of a remake:
# v1 (twitter): https://twitter.com/Jamrat_/status/1132390396787613696, by @Jamrat_
# v2: (reddit): https://redd.it/btmbxm, by u/cavedave
## v2 code: https://gist.github.com/cavedave/2b99bd3b4e966c4f0211b6544a948026
# automate download of file with remote browser
rs_driver <- rsDriver(
port = 4444L,
browser = "firefox",
extraCapabilities = RSelenium::makeFirefoxProfile(list(
"browser.download.dir" = here::here(), # current wd
"browser.download.folderList" = 2L,
"browser.helperApps.neverAsk.saveToDisk" = "text/csv"
))
)
# navigate to gridwatch site and click download button
remote_driver <- rs_driver$client
remote_driver$navigate("http://www.gridwatch.templar.co.uk/download.php")
remote_driver$findElement(using = "css", "div.button:nth-child(109)")$clickElement()
remote_driver$close()
rs_driver$server$stop()
# read file
gridwatch <-
read_csv(
file = "gridwatch.csv",
col_types = cols(timestamp = col_datetime())) %>%
group_by(date = floor_date(timestamp, "day")) %>%
summarize(
coal = sum(coal),
other = sum(demand)) %>%
mutate(
prop_coal = coal / other,
year = year(date),
yday = yday(date),
is_zero = prop_coal == 0) %>%
filter(year > 2011)
# plot gradient tiles
gridwatch %>%
ggplot(mapping = aes(x = yday, y = year)) +
geom_tile(mapping = aes(fill = prop_coal), size = 0.1) +
geom_tile(mapping = aes(alpha = is_zero), fill = "#009E73") +
scale_y_reverse(breaks = 2011:2019) +
scale_fill_gradient(low = "white", high = "black") +
scale_alpha_manual(values = c(0, 1), guide = FALSE) +
theme_minimal() +
labs(
title = "UK Electricity from Coal",
caption = "Source: GridWatch.co.uk",
x = "Day",
y = "Year",
fill = "Percent Coal"
) +
theme(
panel.grid = element_blank(),
axis.text = element_text(size = 12),
axis.title.x = element_text(margin = margin(t = 10, b = 10), size = 18),
axis.title.y = element_text(margin = margin(l = 10, r = 10), size = 14),
plot.caption = element_text(margin = margin(t = 10, b = 10), size = 14),
legend.text = element_text(margin = margin(t = 10, b = 10, r = 10), size = 14),
legend.title = element_text(margin = margin(t = 10, b = 10, r = 10), size = 14),
plot.title = element_text(
hjust = 0.5,
vjust = 0.5,
face = "bold",
size = 24,
margin = margin(t = 10)
)
)
# save plot
ggsave(
filename = "gridwatch.png",
plot = last_plot(),
dpi = "retina",
height = 9,
width = 16
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment