Created
November 7, 2014 02:08
-
-
Save randyzwitch/10ee7e43f1974015bf55 to your computer and use it in GitHub Desktop.
Adobe Analytics Anomaly Detection ggplot
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
#Plot data using ggplot2 | |
library(ggplot2) | |
#Calculate points crossing UCL or LCL | |
pageviews_w_forecast$outliers <- | |
ifelse(pageviews_w_forecast$pageviews > pageviews_w_forecast$upperBound.pageviews, pageviews_w_forecast$pageviews, | |
ifelse(pageviews_w_forecast$pageviews < pageviews_w_forecast$lowerBound.pageviews, pageviews_w_forecast$pageviews, NA)) | |
#Add LCL and UCL labels | |
LCL <- vector(mode = "character", nrow(pageviews_w_forecast)) | |
LCL[nrow(pageviews_w_forecast)] <- "LCL" | |
UCL <- vector(mode = "character", nrow(pageviews_w_forecast)) | |
UCL[nrow(pageviews_w_forecast)] <- "UCL" | |
pageviews_w_forecast <- cbind(pageviews_w_forecast, LCL) | |
pageviews_w_forecast <- cbind(pageviews_w_forecast, UCL) | |
#Create ggplot with actual, UCL, LCL, outliers | |
ggplot(pageviews_w_forecast, aes(datetime)) + | |
theme_bw() + | |
#theme(text = element_text(size=20)) + | |
geom_line(aes(y = pageviews), colour = "grey40") + | |
geom_point(aes(y = pageviews), colour = "grey40", size=3) + | |
geom_point(aes(y = outliers), colour = "red", size=3) + | |
geom_line(aes(y = upperBound.pageviews), colour = "green4", linetype = "dashed") + | |
geom_line(aes(y = lowerBound.pageviews), colour = "green4", linetype = "dashed") + | |
xlab("\nDate\n") + | |
ylab("Page Views\n") + | |
geom_text(aes(label=UCL), y = pageviews_w_forecast$upperBound.pageviews, size=4.5, hjust = -.1) + | |
geom_text(aes(label=LCL), y = pageviews_w_forecast$lowerBound.pageviews, size=4.5, hjust = -.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment