Repository for Petra's work at ampli Jan-Feb 2019

clusterviz.R 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. library(reticulate)
  2. library(ggplot2)
  3. library(dplyr)
  4. theme_set(theme_bw())
  5. use_virtualenv("../venv/")
  6. p <- import("pandas")
  7. sns <- import("seaborn")
  8. cbp <- as.character(p$Series(sns$color_palette("colorblind", as.integer(9))$as_hex()))
  9. aggdf <- p$read_pickle("../data/9-clusters.agg.pkl")
  10. aggdf <- as.data.frame(aggdf)
  11. aggdf$cluster <- factor(aggdf$cluster)
  12. str(aggdf)
  13. ggplot(aggdf, aes(y = kwh_tot_mean, x = cluster)) + geom_boxplot()
  14. facall <- ggplot(aggdf, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  15. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  16. labs(title = "Cluster behaviour over full year, 2017", x = "Date", y = "kwh") +
  17. scale_color_manual(values = cbp) +
  18. scale_fill_manual(values = cbp) +
  19. theme(legend.position = "none") +
  20. scale_x_datetime(date_breaks = "1 month", date_labels = "%-d %B")
  21. allcon <- facall + facet_grid(cluster ~ .)
  22. allfre <- facall + facet_grid(cluster ~ ., scales = "free")
  23. midjan <- filter(aggdf, read_time >= as.POSIXct("2017-01-15", tz = "UTC"), read_time <= as.POSIXct("2017-01-22", tz = "UTC"))
  24. facjan <- ggplot(midjan, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  25. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  26. labs(title = "Cluster behaviour over third week of January", x = "Date", y = "kwh") +
  27. scale_color_manual(values = cbp) +
  28. scale_fill_manual(values = cbp) +
  29. theme(legend.position = "none") +
  30. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  31. jancon <- facjan + facet_grid(cluster ~ .)
  32. janfre <- facjan + facet_grid(cluster ~ ., scales = "free")
  33. midap <- filter(aggdf, read_time >= as.POSIXct("2017-04-16", tz = "UTC"), read_time <= as.POSIXct("2017-04-23", tz = "UTC"))
  34. facap <- ggplot(midap, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  35. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  36. labs(title = "Cluster behaviour over third week of April 2017", x = "Date", y = "kwh") +
  37. scale_color_manual(values = cbp) +
  38. scale_fill_manual(values = cbp) +
  39. theme(legend.position = "none") +
  40. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  41. apcon <- facap + facet_grid(cluster ~ .)
  42. apfre <- facap + facet_grid(cluster ~ ., scales = "free")
  43. midjul <- filter(aggdf, read_time >= as.POSIXct("2017-07-16", tz = "UTC"), read_time <= as.POSIXct("2017-07-23", tz = "UTC"))
  44. facjul <- ggplot(midjul, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  45. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  46. labs(title = "Cluster behaviour over third week of July 2017", x = "Date", y = "kwh") +
  47. scale_color_manual(values = cbp) +
  48. scale_fill_manual(values = cbp) +
  49. theme(legend.position = "none") +
  50. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  51. julcon <- facjul + facet_grid(cluster ~ .)
  52. julfre <- facjul + facet_grid(cluster ~ ., scales = "free")
  53. midoct <- filter(aggdf, read_time >= as.POSIXct("2017-10-15", tz = "UTC"), read_time <= as.POSIXct("2017-10-22", tz = "UTC"))
  54. facoct <- ggplot(midoct, aes(x = read_time, y = kwh_tot_mean, color = cluster, fill = cluster)) +
  55. geom_line(size = 1.5) + geom_ribbon(aes(ymin = kwh_tot_CI_low, ymax = kwh_tot_CI_high), alpha = 0.2, color = NA) +
  56. labs(title = "Cluster behaviour over third week of October 2017", x = "Date", y = "kwh") +
  57. scale_color_manual(values = cbp) +
  58. scale_fill_manual(values = cbp) +
  59. theme(legend.position = "none") +
  60. scale_x_datetime(date_breaks = "1 day", date_labels = "%a, %-d %B %Y")
  61. octcon <- facoct + facet_grid(cluster ~ .)
  62. octfre <- facoct + facet_grid(cluster ~ ., scales = "free")
  63. ggsave("all-9-fix.png", allcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  64. ggsave("all-9-fre.png", allfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  65. ggsave("jan-9-fix.png", jancon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  66. ggsave("jan-9-fre.png", janfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  67. ggsave("apr-9-fix.png", apcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  68. ggsave("apr-9-fre.png", apfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  69. ggsave("jul-9-fix.png", julcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  70. ggsave("jul-9-fre.png", julfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  71. ggsave("oct-9-fix.png", octcon, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")
  72. ggsave("oct-9-fre.png", octfre, path = "../img/", dpi = "retina", width = 40, height = 25, units = "cm")