#!/usr/bin/env Rscript library(argparse) parser <- ArgumentParser(description="Create plots for aggregated cluster data") parser$add_argument("cluster_file", help = "file to visualise") parser$add_argument("-i", "--img-path", dest = "img_path", default = "../img/", help = "path to store plots in; default: ../img/") parser$add_argument("-p", "--postfix", dest = "postfix", default = "_plot", help = "postfix for files, default: _plot") parser$add_argument("--width", dest = "width", default = 40, help = "width (cm), default 40", type = "double") parser$add_argument("--height", dest = "height", default = 25, help = "height (cm), default 25", type = "double") parser$add_argument("--pkl", dest = "csv", help = "load from pickle instead of csv", action = "store_false") args <- parser$parse_args() library(ggplot2, warn.conflicts = FALSE, quietly = TRUE) library(dplyr, warn.conflicts = FALSE, quietly = TRUE) library(tidyr, warn.conflicts = FALSE, quietly = TRUE) library(TSA, warn.conflicts = FALSE, quietly = TRUE) library(forecast, warn.conflicts = FALSE, quietly = TRUE) theme_set(theme_bw()) if (args$csv) { aggdf <- read.csv(args$cluster_file, header = TRUE, stringsAsFactors = FALSE) aggdf$read_time <- as.POSIXct(aggdf$read_time) } else { library(reticulate, warn.conflicts = FALSE, quietly = TRUE) p <- import("pandas") aggdf <- p$read_pickle(args$cluster_file) } aggdf$cluster <- factor(aggdf$cluster) clusters = levels(aggdf$cluster) cbp <- c('#0173b2', '#de8f05', '#029e73', '#d55e00', '#cc78bc', '#ca9161', '#fbafe4', '#949494', '#ece133', '#56b4e9', '#0173b2', '#de8f05', '#029e73', '#d55e00', '#cc78bc', '#ca9161', '#fbafe4', '#949494', '#ece133', '#56b4e9', '#0173b2', '#de8f05', '#029e73', '#d55e00', '#cc78bc', '#ca9161', '#fbafe4', '#949494', '#ece133', '#56b4e9', '#0173b2', '#de8f05', '#029e73', '#d55e00', '#cc78bc', '#ca9161', '#fbafe4', '#949494', '#ece133', '#56b4e9')[1:length(clusters)] facall <- ggplot(aggdf, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) + geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) + labs(title = "Cluster behaviour over 2017", x = "Date", y = "kwh") + scale_color_manual(values = cbp) + scale_fill_manual(values = cbp) + theme(legend.position = "none") + scale_x_datetime(date_breaks = "1 month", date_labels = "%-d %b %y") allcon <- facall + facet_grid(cluster ~ .) allfre <- facall + facet_grid(cluster ~ ., scales = "free") midjan <- filter(aggdf, read_time >= as.POSIXct("2017-01-15", tz = "UTC"), read_time <= as.POSIXct("2017-01-22", tz = "UTC")) facjan <- ggplot(midjan, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) + geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) + labs(title = "Cluster behaviour over third week of January", x = "Date", y = "kwh") + scale_color_manual(values = cbp) + scale_fill_manual(values = cbp) + theme(legend.position = "none") + scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y") jancon <- facjan + facet_grid(cluster ~ .) janfre <- facjan + facet_grid(cluster ~ ., scales = "free") midap <- filter(aggdf, read_time >= as.POSIXct("2017-04-16", tz = "UTC"), read_time <= as.POSIXct("2017-04-23", tz = "UTC")) facap <- ggplot(midap, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) + geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) + labs(title = "Cluster behaviour over third week of April 2017", x = "Date", y = "kwh") + scale_color_manual(values = cbp) + scale_fill_manual(values = cbp) + theme(legend.position = "none") + scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y") apcon <- facap + facet_grid(cluster ~ .) apfre <- facap + facet_grid(cluster ~ ., scales = "free") midjul <- filter(aggdf, read_time >= as.POSIXct("2017-07-16", tz = "UTC"), read_time <= as.POSIXct("2017-07-23", tz = "UTC")) facjul <- ggplot(midjul, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) + geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) + labs(title = "Cluster behaviour over third week of July 2017", x = "Date", y = "kwh") + scale_color_manual(values = cbp) + scale_fill_manual(values = cbp) + theme(legend.position = "none") + scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y") julcon <- facjul + facet_grid(cluster ~ .) julfre <- facjul + facet_grid(cluster ~ ., scales = "free") midoct <- filter(aggdf, read_time >= as.POSIXct("2017-10-15", tz = "UTC"), read_time <= as.POSIXct("2017-10-22", tz = "UTC")) facoct <- ggplot(midoct, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) + geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) + labs(title = "Cluster behaviour over third week of October 2017", x = "Date", y = "kwh") + scale_color_manual(values = cbp) + scale_fill_manual(values = cbp) + theme(legend.position = "none") + scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y") octcon <- facoct + facet_grid(cluster ~ .) octfre <- facoct + facet_grid(cluster ~ ., scales = "free") ggsave(paste0("all_fix", args$postfix, ".png"), allcon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("all_fre", args$postfix, ".png"), allfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("jan_fix", args$postfix, ".png"), jancon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("jan_fre", args$postfix, ".png"), janfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("apr_fix", args$postfix, ".png"), apcon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("apr_fre", args$postfix, ".png"), apfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("jul_fix", args$postfix, ".png"), julcon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("jul_fre", args$postfix, ".png"), julfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("oct_fix", args$postfix, ".png"), octcon, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm") ggsave(paste0("oct_fre", args$postfix, ".png"), octfre, path = args$img_path, dpi = "retina", width = args$width, height = args$height, units = "cm")