Skip to content

Instantly share code, notes, and snippets.

@tts
Last active January 20, 2019 13:34
Show Gist options
  • Save tts/50a916fafce04d9050290372ea00a74e to your computer and use it in GitHub Desktop.
Save tts/50a916fafce04d9050290372ea00a74e to your computer and use it in GitHub Desktop.
Line graph on energy production in district heating of the Helsinki capital area
library(tidyverse)
library(readxl)
library(httr)
#-------------------------------------------------------------
# hri.fi
#
# Pääkaupunkiseudun kaukolämmön tuotannon energialähteet (GWh)
#-------------------------------------------------------------
url <- "https://www.hsy.fi/sites/AvoinData/AvoinData/Kaukolammon_paastot_kulutus_polttoaineet_PKS_1990_2000-2013.xlsx"
GET(url, write_disk(tf <- tempfile(fileext = ".xlsx")))
df <- read_xlsx(tf, sheet = "Energialähteet", skip = 3)
unlink(tf)
df <- df %>%
rename(Source = `POLTTOAINEET (GWh)`)
df_t <- as.data.frame(t(df), stringsAsFactors = FALSE)
names(df_t) <- df_t[1,]
df_t$Year <- row.names(df_t)
data <- df_t[2:nrow(df_t),]
data2 <- data %>%
select(Year, Coal = Kivihiili, Gas = Maakaasu,
Oil = Öljy, Bio, `Heat pump`= Lämpöpumput)
data3 <- data.frame(data2, row.names = NULL)
data3$Coal <- as.numeric(data3$Coal)
data3$Gas <- as.numeric(data3$Gas)
data3$Oil <- as.numeric(data3$Oil)
data3$Bio <- as.numeric(data3$Bio)
data3$Heat.pump <- as.numeric(data3$Heat.pump)
data3$Year <- as.integer(data3$Year)
energy <- data3 %>%
gather(Source, Production, Coal:Heat.pump)
before2000 <- energy[energy$Year <= 2000,]
after2000 <- energy[energy$Year >= 2000,]
e <- ggplot() +
geom_line(data = before2000,
aes( x = Year, y = Production, colour = Source), linetype = "dashed", size = 1.5) +
geom_line(data = after2000,
aes( x = Year, y = Production, colour = Source), size = 1.5) +
scale_x_continuous(breaks=seq(1990, 2013, 2)) +
scale_colour_brewer(palette="Spectral") +
labs(title="District heating in Finnish capital region 1990-2013",
subtitle = "Energy sources used in production",
caption="Data: hri.fi",
y = "Production (GWh)") +
theme_dark()
ggsave("energy.png", e, height = 8, width = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment