# ogdindiar sample analysis
library(devtools)
devtools::install_github("steadyfish/ogdindiar")
## Downloading github repo steadyfish/ogdindiar@master
## Installing ogdindiar
## "C:/PROGRA~1/R/R-32~1.0/bin/x64/R" --vanilla CMD INSTALL  \
##   "C:/Users/DreamerNoob/AppData/Local/Temp/RtmpiukyVu/devtools17b85c4e3279/steadyfish-ogdindiar-980058c"  \
##   --library="C:/Users/pokerface/R/win-library/3.2" --install-tests
# install.packages("dygraphs")
# install.packages("magrittr")
# install.packages("RColorBrewer")
# install.packages("dplyr")
# install.packages("tidyr")
library(ogdindiar)
## Welcome to ogdindiar
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(dygraphs)
library(magrittr)
library(RColorBrewer)
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:xts':
## 
##     first, last
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## 
## Attaching package: 'tidyr'
## 
## The following object is masked from 'package:magrittr':
## 
##     extract
sessionInfo()
## R version 3.2.0 (2015-04-16)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] tidyr_0.2.0          dplyr_0.4.1          RColorBrewer_1.1-2  
## [4] magrittr_1.5         dygraphs_0.4.3       xts_0.9-7           
## [7] zoo_1.7-12           ogdindiar_0.0.0.9002 devtools_1.7.0      
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.11.6     knitr_1.10      lattice_0.20-31 stringr_1.0.0  
##  [5] httr_0.6.1      tools_3.2.0     parallel_3.2.0  grid_3.2.0     
##  [9] DBI_0.3.1       htmltools_0.2.6 yaml_2.1.13     digest_0.6.8   
## [13] assertthat_0.1  formatR_1.2     htmlwidgets_0.4 bitops_1.0-6   
## [17] RCurl_1.95-4.6  evaluate_0.7    rmarkdown_0.6.1 stringi_0.4-1
# get your own api key as described in ogdindiar vignette here - 
# https://github.com/steadyfish/ogdindiar/blob/master/vignettes/basic-usage-vignette.md
mean_temp_ls = fetch_data(res_id = "98fe9271-a59d-4834-b05b-fd5ddb94ac01")
                            
mean_temp_data = mean_temp_ls[[1]]

mean_temp_data %>% names
## [1] "id"        "timestamp" "year"      "annual"    "jan_feb"   "mar_may"  
## [7] "jun_sep"   "oct_dec"
mean_temp_data %>% str
## 'data.frame':    112 obs. of  8 variables:
##  $ id       : chr  "1123" "1423" "1443" "1463" ...
##  $ timestamp: num  1.42e+09 1.42e+09 1.42e+09 1.42e+09 1.42e+09 ...
##  $ year     : num  1957 1972 1973 1974 1975 ...
##  $ annual   : num  23 24 24 24 23 24 24 24 24 24 ...
##  $ jan_feb  : num  18 18 19 18 18 18 19 18 19 19 ...
##  $ mar_may  : num  25 25 26 26 25 25 26 25 25 26 ...
##  $ jun_sep  : num  27 27 27 27 26 26 27 27 27 27 ...
##  $ oct_dec  : num  21 21 21 21 21 22 22 22 22 22 ...
mean_temp_long = mean_temp_data %>%
  gather(key = quarter, value = seasonal, select = jan_feb:oct_dec) %>%
  mutate(quarter_num = ifelse(quarter %in% "jan_feb", "Q1",
                              ifelse(quarter %in% "mar_may", "Q2",
                                     ifelse(quarter %in% "jun_sep", "Q3", "Q4"))))

mean_temp_long$yr_qr = paste(mean_temp_long$year, mean_temp_long$quarter_num)

mean_temp_long %<>% 
  select(yr_qr, annual, seasonal)

mean_temp_long %>% names
## [1] "yr_qr"    "annual"   "seasonal"
mean_temp_xts = xts(x = mean_temp_long[ , c(2:3)], order.by = as.yearqtr(mean_temp_long[ , 1]))

clr = RColorBrewer::brewer.pal(3, "Set1")
dygraph(mean_temp_xts, main = "India Mean Temperatures", ylab = "Temp (C)") %>%
  dyOptions(colors = clr[1:2], 
            includeZero = TRUE) %>%
  dyRangeSelector(dateWindow = c("1980-01-01", "2012-01-01"))