# ===================================================================================================================== # EThekwini Electricity Usage and Access Andrew Collier (andrew@exegetic.biz) # Exegetic Analytics / www.exegetic.biz / info@exegetic.biz / @exegeticdata # ===================================================================================================================== # https://edge.durban/dataset/electricity-usage library(readxl) library(ggplot2) library(janitor) library(dplyr) library(tidyr) # DOWNLOAD XLSX ------------------------------------------------------------------------------------------------------- FILENAME = "electricity-use-ethekwini.xlsx" URL = "http://edge.durban/dataset/d57748e4-c446-4e7c-914d-40f754b8d77a/resource/434026e6-7b26-4dc5-9247-13d6ea31697c/download/" # URL = file.path(URL, FILENAME) download.file(URL, FILENAME) # LOAD DATA ----------------------------------------------------------------------------------------------------------- electricity <- read_xlsx(FILENAME) View(electricity) # # Those column names are going to be a problem! electricity <- clean_names(electricity) View(electricity) # PREPARE DATA -------------------------------------------------------------------------------------------------------- # The year column is not a number! # electricity <- electricity %>% separate(year, c("year", NA)) %>% mutate(year = as.integer(year)) # PLOTS --------------------------------------------------------------------------------------------------------------- ggplot(electricity, aes(x = year, y = number_of_customers / 1000)) + geom_col(fill = "#3498db") + scale_x_continuous(NULL, breaks = seq(1980, 2020, 5)) + scale_y_continuous("Customers [thousands]") ggsave("electricity-usage-customers.png", width = 6, height = 3, dpi = 150) ggplot(electricity, aes(x = year, y = energy_k_wh_sold / 1e9)) + geom_col(fill = "#ffa330") + geom_vline(xintercept = 2007, lty = "dashed") + scale_x_continuous(NULL, breaks = seq(1980, 2020, 5)) + scale_y_continuous("Energy [tWh]") ggsave("electricity-usage-energy.png", width = 6, height = 3, dpi = 150) ggplot(electricity, aes(x = year, y = energy_k_wh_sold / number_of_customers / (365.25 * 24))) + geom_line() + geom_point() + geom_vline(xintercept = 2007, lty = "dashed") + scale_x_continuous(NULL, breaks = seq(1980, 2020, 5)) + scale_y_continuous("Average Power per Customer [kW]") ggsave("electricity-usage-power-per-customer.png", width = 6, height = 3, dpi = 150)