#! /usr/local/r/bin/Rscript # Copyright (C), 2015, Christopher Swingley # Licensed under the terms of the Creative Commons License # Attribution - ShareAlike Version 3.0 library(dplyr) library(ggplot2) library(lubridate) library(scales) library(gtable) db <- src_postgres(dbname="dbname", host="localhost") # dplyr doesn't support views right now, so make "view" of a view: west <- tbl(db, build_sql("SELECT * FROM arduino_west_corrected WHERE obs_dt > now() - interval '7 days'")) %>% collect() stp <- tbl(db, build_sql("SELECT dt_local, air, chamber FROM stp WHERE dt_local > now() - interval '7 days'")) %>% collect() date_formatter <- function(x) { ifelse(hour(x) < 8, strftime(x, tz="US/Alaska", "%b-%d\n%H:%M"), strftime(x, tz="US/Alaska", "%H:%M")) } min_dt <- min(west$obs_dt, stp$dt_local) max_dt <- max(west$obs_dt, stp$dt_local) west <- west %>% filter(obs_dt >= min_dt, obs_dt<= max_dt) stp <- stp %>% filter(dt_local >= min_dt, dt_local <= max_dt) outside <- ggplot(data=west, aes(x=obs_dt, y=wtemp)) + theme_bw() + theme(plot.margin = unit(c(1, 1, 0, 0.5), 'lines')) + # t, r, b, l theme(axis.text.x = element_blank(), axis.title.x = element_blank()) + # geom_point(size=1) + geom_line() + scale_x_datetime(breaks=date_breaks('8 hours'), labels=date_formatter) + scale_y_continuous(name="Outside temp (degrees F)", breaks=pretty_breaks(n=10)) + ggtitle("Sewage treatment plant stats") instruments <- ggplot(data=stp, aes(x=dt_local, y=air)) + theme_bw() + theme(plot.margin = unit(c(0, 1, 0, 0.5), 'lines')) + # t, r, b, l theme(axis.text.x = element_blank(), axis.title.x = element_blank()) + geom_point(size=1) + geom_line() + scale_x_datetime(breaks=date_breaks('8 hours'), labels=date_formatter) + scale_y_continuous(name="System temp (deg F)", breaks=pretty_breaks(n=5)) chamber <- ggplot(data=stp, aes(x=dt_local, y=chamber)) + theme_bw() + theme(plot.margin = unit(c(0, 1, 0.5, 0.5), 'lines')) + theme(axis.title.x = element_blank()) + geom_point(size=1) + geom_line() + scale_x_datetime(breaks=date_breaks('8 hours'), labels=date_formatter) + scale_y_continuous(name="Chamber temp (degrees F)", breaks=pretty_breaks(n=10)) make_gt <- function(outside, instruments, chamber, width, heights) { gt1 <- ggplot_gtable(ggplot_build(outside)) gt2 <- ggplot_gtable(ggplot_build(instruments)) gt3 <- ggplot_gtable(ggplot_build(chamber)) max_width <- unit.pmax(gt1$widths[2:3], gt2$widths[2:3], gt3$widths[2:3]) gt1$widths[2:3] <- max_width gt2$widths[2:3] <- max_width gt3$widths[2:3] <- max_width gt <- gtable(widths = unit(c(width), "in"), heights = unit(heights, "in")) gt <- gtable_add_grob(gt, gt1, 1, 1) gt <- gtable_add_grob(gt, gt2, 2, 1) gt <- gtable_add_grob(gt, gt3, 3, 1) gt } rescale <- 0.75 width <- 16*rescale height <- 11*rescale heights <- c(0.40, 0.20, 0.40) * height gt <- make_gt(outside, instruments, chamber, width, heights) svg("stp.svg", height=height, width=width) grid.newpage() grid.draw(gt) dev.off() pdf("stp.pdf", height=height, width=width) grid.newpage() grid.draw(gt) dev.off()