# Set workspace ----
#set wd
setwd("~/Documents/Classes/Year 5/Fall 2019/Visual Analytics/Module 11")

#install package(s)
#install.packages(c("CarletonStats", "devtools", "epanetReader", "fmsb", "ggplot2", "ggthemes","latticeExtra", "MASS", "PerformanceAnalytics", "psych", "plyr","prettyR", "plotrix","proto", "RCurl", "reshape", "reshape2"))
#install.packages("ggExtra")

#load libraries
library("CarletonStats")
library("devtools")
## Loading required package: usethis
library("epanetReader")
library("fmsb")
library("ggplot2")
library("ggthemes")
library("ggExtra")
library("latticeExtra")
## Loading required package: lattice
## Loading required package: RColorBrewer
## 
## Attaching package: 'latticeExtra'
## The following object is masked from 'package:ggplot2':
## 
##     layer
library("MASS")
library("PerformanceAnalytics")
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Registered S3 method overwritten by 'xts':
##   method     from
##   as.zoo.xts zoo
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library("psych")
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library("plyr")
library("prettyR")
## 
## Attaching package: 'prettyR'
## The following objects are masked from 'package:psych':
## 
##     describe, skew
library("plotrix")
## 
## Attaching package: 'plotrix'
## The following object is masked from 'package:prettyR':
## 
##     valid.n
## The following object is masked from 'package:psych':
## 
##     rescale
library("proto")
library("RCurl")
## Loading required package: bitops
library("reshape")
## 
## Attaching package: 'reshape'
## The following objects are masked from 'package:plyr':
## 
##     rename, round_any
library("reshape2")
## 
## Attaching package: 'reshape2'
## The following objects are masked from 'package:reshape':
## 
##     colsplit, melt, recast
#  Minimal line plot [included code] ----
#set vars
x <- 1967:1977
y <- c(0.5,1.8,4.6,5.3,5.3,5.7,5.4,5,5.5,6,5)

#export options
pdf(width=10, height=6)

#set plot
plot(y ~ x, axes=F, xlab="", ylab="", pch=16, type="b")
axis(1, at=x, label=x, tick=F, family="serif")
axis(2, at=seq(1,6,1), label=sprintf("$%s", seq(300,400,20)), tick=F, las=2, family="serif")

#plot line
abline(h=6,lty=2)
abline(h=5,lty=2)

#plot text
text(max(x), min(y)*2.5,"Per capita\nbudget expanditures\nin constant dollars", adj=1, 
     family="serif")
text(max(x), max(y)/1.08, labels="5%", family="serif")

#clear
dev.off()
## quartz_off_screen 
##                 2
# Marginal histogram scatterplot in ggplot2 ----
#load dataset (old faithful eruptions)
source_url("https://raw.githubusercontent.com/sjmurdoch/fancyaxis/master/fancyaxis.R")
## SHA-1 hash of file is 1eaf278aced5a1206f1c0bffab087d26f44e3f73
x <- faithful$waiting
y <- faithful$eruptions

#generate ggplot2 vis
p <- ggplot(faithful, aes(waiting, eruptions)) + 
  geom_point() + 
  theme_tufte(ticks=F)
#use p within ggmarginal to create maeginal histogram
ggMarginal(p, type = "histogram", fill="transparent")

#my customization
p <- ggplot(faithful, aes(waiting, eruptions)) + 
  #edit points color
  geom_point(color = "#00008b", alpha = 0.6) + 
  #edit theme, remove background and keep grid lines
  theme(panel.background = element_blank(),
        panel.grid.major = element_line(color = "lightgrey"),
        panel.grid.minor = element_line(color = "lightgrey")) +
  #X-Axis Marks Scale (0-100 every 5)
  scale_x_continuous(breaks=seq(0,100,5)) +
  #better labels
  xlab("Waiting") +
  ylab("Eruptions")
ggMarginal(p, fill = "lightblue", type = "histogram", fill="transparent")