library(RCurl); library(knitr); library(jsonlite); library(tidyverse); library(magrittr)
gm_names <- c("biomass", "coal", "imports", "gas", "nuclear", "other", "hydro", "solar", "wind")
date_fixer <- function(v) {
strptime(x = v, format = "%Y-%m-%dT%H:%MZ")
}
genmix_api <- function(fromdate, todate){
fromdate <- as.Date(fromdate); todate <- as.Date(todate);
datelist <- seq.Date(fromdate, to = todate, by = "1 day")
gen_mix <- data.frame(matrix(nrow = 0, ncol = 4)) %>%
set_colnames(c("data.from", "data.to", "data.generationmix.fuel", "data.generationmix.perc"))
for (i in 1:length(datelist)){
if(fromdate < as.Date("2017-09-12")) {
cat("\n\n", "Please select a from-date of 2017-09-12 or later", "\n\n"); break
}
df_json <- RCurl::basicTextGatherer()
df_json$reset() # For caution
paste0("https://api.carbonintensity.org.uk/generation/", datelist[i], "T00:00Z/", datelist[i], "T23:59Z") %>%
RCurl::curlPerform(url = ., writefunction = df_json$update)
data_response <- df_json$value() %>%
parse_json(simplifyVector = TRUE) %>%
as.data.frame()
data_response$info <- NULL
gen_mix <- rbind(gen_mix, data_response)
} # close the loop
data.from <- gen_mix$data.from %>%
date_fixer()
data.to <- gen_mix$data.to %>%
date_fixer()
gm_percentages <- gen_mix[[3]] %>%
as.data.frame() %>%
t() %>%
set_colnames(gm_names) %>%
as.data.frame() %>%
filter(biomass != "biomass")
data.frame(data.from, data.to, gm_percentages)
} # close the function
gm_data <- genmix_api(Sys.Date() - 30, todate = Sys.Date() - 1)
gm_data %>%
head() %>%
kable(caption = "Generation Mix")
data.from | data.to | biomass | coal | imports | gas | nuclear | other | hydro | solar | wind |
---|---|---|---|---|---|---|---|---|---|---|
2019-11-08 23:30:00 | 2019-11-09 00:00:00 | 7.6 | 1.4 | 13.0 | 34.0 | 24.1 | 0.4 | 0.9 | 0.0 | 18.6 |
2019-11-09 00:00:00 | 2019-11-09 00:30:00 | 7.7 | 1.5 | 12.0 | 33.1 | 24.5 | 0.4 | 1.0 | 0.0 | 19.8 |
2019-11-09 00:30:00 | 2019-11-09 01:00:00 | 7.6 | 1.4 | 11.9 | 33.9 | 24.2 | 0.4 | 0.9 | 0.0 | 19.7 |
2019-11-09 01:00:00 | 2019-11-09 01:30:00 | 7.7 | 1.4 | 11.8 | 33.6 | 24.4 | 0.4 | 1.0 | 0.0 | 19.7 |
2019-11-09 01:30:00 | 2019-11-09 02:00:00 | 7.8 | 1.5 | 12.1 | 32.4 | 24.9 | 0.4 | 1.0 | 0.0 | 19.9 |
2019-11-09 02:00:00 | 2019-11-09 02:30:00 | 7.9 | 1.5 | 11.9 | 31.0 | 24.9 | 0.4 | 1.0 | 0.0 | 21.4 |
gm_data_longer <- gm_data %>%
pivot_longer(
cols = 3:11,
names_to = "gen_type",
values_to = "mix"
) %>%
mutate(mix = mix %>%
as.character() %>%
as.numeric())
(Code generated using the esquisse
package)
gm_data_longer %<>%
filter(data.from >= "2019-11-30 23:30:00" & data.from <= "2019-12-08 23:00:00")
ggplot(gm_data_longer) +
aes(x = data.from, y = mix, fill = gen_type) +
geom_area(size = 1L) +
scale_fill_viridis_d(option = "viridis") +
labs(
x = "Date",
y = "Mix %",
title = "Generation Mix UK",
subtitle = "Source: National Grid ESO - https://carbon-intensity.github.io"
) +
theme_bw() +
theme(legend.position = "bottom")
ggplot(gm_data_longer) +
aes(x = data.from, y = gen_type, fill = mix) +
geom_tile(size = 1L) +
scale_fill_distiller(palette = "RdYlGn") +
labs(
x = "Date",
y = "Mix",
title = "Generation Mix",
subtitle = "Weird Heatmap Thing!",
fill = "Mix %"
) +
theme_minimal() +
theme(
legend.position = "bottom",
legend.title = element_text(vjust = 0.5)
)