-
-
Save IronistM/8c321a701ba2d288414a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://learnr.wordpress.com/2009/05/18/ggplot2-three-variable-time-series-panel-chart/ | |
# http://www.talkstats.com/showthread.php/21228-time-series-in-ggplot2 | |
# http://had.co.nz/ggplot2/ | |
# http://stats.stackexchange.com/questions/14513/align-multiple-ggplot2-plots-with-grid | |
# http://wiki.stdout.org/rcookbook/Graphs/Facets%20(ggplot2) | |
# colors: red: "730202", lightyellow: D5D95B, lightgreen: 4F7302 midgreed=274001 darkgreen=092601 | |
setwd('~/repositories/uni/thesis-repos/Opad4lsssExperiments/r_scripts/') | |
library(ggplot2) | |
library(gridExtra) | |
#sink(file = 'rsink.log', append = FALSE, type = c("output", "message"), | |
# split = FALSE) | |
times <<- c(1324565899723,1324565904723,1324565909723,1324565914723,1324565919723,1324565924723,1324565929723,1324565934723,1324565939723,1324565944723,1324565949723,1324565954723,1324565959723,1324565964723,1324565969723,1324565974723,1324565979723,1324565984723,1324565989723,1324565994723,1324565999723,1324566004723,1324566004723,1324566004723) | |
measures <<- c(57034.90780000002,88741.47900000006,46667.94900000008,65948.59480000008,64890.55660000007,28163.47790000006,88817.65450000006,80971.82600000004,62030.19100000006,47352.13400000007,42610.53874000006,66468.73460000007,35083.45883000006,0.0,64695.670500000066,5453.871,29406.723900000048,NA,NA,NA,NA,NA,NA,NA) | |
forecasts <<- c(84928.97293333337,77955.45665000004,80112.66112000003,74538.54243333338,73311.4070571429,72258.80075000005,67359.32043333339,69505.15384000006,70547.57858181823,69837.79628333339,68108.1299538462,66286.87343857148,66298.99751600006,64348.02634812506,60562.848327647116,60792.449559444496,57879.892793157946,56456.23434850005,56456.23434850005,56456.23434850005,56456.23434850005,56456.23434850005,52919.32773526321,52016.07649833339) | |
anomalies <<- c(0.15497808999359292,0.05110219905693613,0.2299430756863613,0.052870976295805415,0.05372423387443585,0.4103297141333393,0.12198179695326135,0.06879810178077138,0.059207738316031155,0.1797674389705455,0.21741870835044844,0.0012784513322236088,0.29431892187569564,1.0,0.03110430643718774,0.8277736653134393,0.315031196225673,1.0,1.0,1.0,1.0,1.0,1.0,1) | |
#anomalies <<- c(0.15497808999359292,0.05110219905693613,0.2299430756863613,0.052870976295805415,0.05372423387443585,0.4103297141333393,0.12198179695326135,0.06879810178077138,0.059207738316031155,0.1797674389705455,0.21741870835044844,0.0012784513322236088,0.29431892187569564,1.0,0.03110430643718774,0.8277736653134393,0.315031196225673,1.0,1.0,1.0,1.0,1.0,1.0,NA) | |
plot2 <- function(times, measures, forecasts, anomalies) { | |
df <- data.frame( | |
times = as.POSIXct(times/1000, origin="1970-01-01 00:00:00"), | |
times = times / 1000, | |
measures = measures, | |
anomalies = anomalies | |
) | |
xbegin <- min(df$times) | |
xend <- max(df$times) | |
colours <- c("#2121D9", "#9999FF") | |
anomalyThreshold = 0.4 | |
sForecastAxisTitle <- paste("Measures and Forecasts") | |
sAspectTitle <- paste("ASPECT.IDENTIFIER: ", runif(1, 1.0, 3.5) ) | |
commonOpts = opts(axis.text.y = theme_text( size = 10), | |
axis.ticks = theme_blank(), | |
plot.margin = rep(unit(0,"null"),4), | |
panel.grid.minor = theme_blank(), | |
axis.text.y = theme_blank() ) | |
# strip.text.y = theme_text(size = 4, colour = 'white') | |
#yAxisOpts = opts(axis.text.y = theme_bw()) | |
# ANOMALIES | |
arrIsAnomaly <- ifelse(anomalies < anomalyThreshold, "1", "0") | |
plotAnomalies <- ggplot( data=df ) | |
plotAnomalies <- plotAnomalies + geom_point(aes(x = times, y = anomalies, colour = arrIsAnomaly), legend=FALSE, size = 6) | |
plotAnomalies <- plotAnomalies + scale_colour_manual(values = c( "#FF0221", "#006913")) | |
plotAnomalies <- plotAnomalies + opts(legend.position = "none") | |
plotAnomalies <- plotAnomalies + geom_hline(yintercept = anomalyThreshold, colour = "grey50") + annotate("text",x = min(df$times),y = anomalyThreshold+0.05, label = "Anomaly", colour = "grey80", size = 4) | |
plotAnomalies <- plotAnomalies + opts(title=sAspectTitle) | |
plotAnomalies <- plotAnomalies + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank()) | |
plotAnomalies <- plotAnomalies + opts(panel.background = theme_blank()) | |
plotAnomalies <- plotAnomalies + commonOpts | |
#plotAnomalies <- plotAnomalies + geom_point(data = NINO34, aes(colour = ifelse(value < 0, "blue", "red"))) | |
# MEASURES | |
plotMeasures <- ggplot( data=df ) | |
plotMeasures <- plotMeasures + scale_x_datetime( name=sForecastAxisTitle, limits=c(xbegin, xend), format = "%Y-%m-%d, %M:%S" ) | |
#plotMeasures <- plotMeasures + geom_line(aes(x = times, y = measures)) | |
plotMeasures <- plotMeasures + geom_line(aes(x = times, y = measures)) | |
plotMeasures <- plotMeasures + geom_line(aes(x = times, y = forecasts, colour = '1'), size = 1) | |
plotMeasures <- plotMeasures + scale_colour_manual(values = c( "#006913")) | |
plotMeasures <- plotMeasures + opts(legend.position = "none") | |
plotMeasures <- plotMeasures + commonOpts | |
#sidebysideplot <- grid.arrange(plotAnomalies, plotMeasures, nrow=2) | |
vplayout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y) | |
grid.newpage() | |
pushViewport(viewport(layout=grid.layout(2,1))) | |
print(plotAnomalies, vp=vplayout(1, 1)) | |
print(plotMeasures, vp=vplayout(2, 1)) | |
} | |
plotOrPDF <- function(file, times, measures, forecasts, anomalies) { | |
dprint(paste("Plotting...")); | |
if( exists("OPAD_CONTEXT") ) { | |
pdf(file) } | |
plot2(times, measures, forecasts, anomalies) | |
if( exists("OPAD_CONTEXT") ) { | |
dev.off() } | |
dprint(paste("Plotted to file ", file)); | |
} | |
if (exists("OPAD_CONTEXT")) { | |
} else { | |
print( plot(times, measures, forecasts, anomalies) ) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment