#!/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("-v", "--virtualenv", dest = "virtualenv", default = "../venv/", help = "path of virtualenv; default: ../venv/; '-' for none") 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") args <- parser$parse_args() library(reticulate) library(ggplot2) library(dplyr) library(tidyr) library(TSA) library(forecast) theme_set(theme_bw()) if (args$virtualenv != "-") { print(args$virtualenv) use_virtualenv(args$virtualenv) } p <- import("pandas") sns <- import("seaborn") cbp <- as.character(p$Series(sns$color_palette("colorblind", as.integer(9))$as_hex())) aggdf <- p$read_pickle(args$cluster_file) # aggdf <- as.data.frame(aggdf) aggdf$cluster <- factor(aggdf$cluster) str(aggdf) clusters = levels(aggdf$cluster) ggplot(aggdf, aes(y = kwh_tot_mean, x = cluster)) + geom_boxplot() 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")