#---loading the initial data here that I will need in order to run the data from R---
#Libraries:
require(curl)
## Loading required package: curl
require(jsonlite)
## Loading required package: jsonlite
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:utils':
## 
##     View
require(rgdal)
## Loading required package: rgdal
## Loading required package: sp
## rgdal: version: 0.9-1, (SVN revision 518)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
## Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rgdal/proj
require(plyr)
## Loading required package: plyr
require(zoo)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
require(dygraphs)
## Loading required package: dygraphs
## Warning: package 'dygraphs' was built under R version 3.1.3
library(knitr)
require (DT)
## Loading required package: DT
require(leaflet)
## Loading required package: leaflet
require(sp)

#Loading the dataset
ramphs <-read.csv(curl("https://gist.githubusercontent.com/sfsheath/5c5987269e8aad412416/raw/a12dda2b8a681f1ab01421f68ac237ab591ff0f9/roman-amphitheaters.csv"))
periods <- fromJSON("https://gist.githubusercontent.com/sfsheath/cc082cc6db3ac8343e3a/raw/6e938ed37b9083f393a67bec10b46ba7fc693661/amphitheater-chronogrps.json")
## Warning: Unexpected Content-Type: text/plain
ramphs.sp <- ramphs
join(ramphs.sp, periods, by= "chronogrp") -> ramphs.joined
coordinates(ramphs.joined) <- ~longitude + latitude
 
 
ramphs.noise <- ramphs.joined
 
tmp.length <- length(ramphs.noise$start_date[ramphs.noise$chronogrp=="Imperial"])
tmp.min  <- min(ramphs.noise$start_date[ramphs.noise$chronogrp=="Imperial"])
tmp.max  <- max(ramphs.noise$end_date[ramphs.noise$chronogrp=="Imperial"])
 
 
ramphs.noise$start_date[ramphs.noise$chronogrp=="Imperial"] <- runif(tmp.length,
                                                                     min=tmp.min,
                                                                     max=tmp.max)
round(ramphs.noise$start_date, digits=0) -> ramphs.noise$start_date
#--------------#
#Everything is loaded, time to start:

#First, I am going to plot the relationship between years and cumulative capacity
#I moved all the data back from "ramphs.noise" to the "ramphs.joined" object
ramphs.noise -> ramphs.joined
#I made a copy of that here
ramphs.joined -> ramphs.joined.date
#I added 100 to all of the years in "start_date" so that none of them would be negative
sprintf("%s", ramphs.joined.date$start_date + 100) -> ramphs.joined.date$start_date
#Then I added month and days to those years
ramphs.joined.date$start_date <- sprintf("%s-01-01", ramphs.joined.date$start_date)
#make a zoo x.Date object
x.Date <- as.Date(ramphs.joined.date$start_date)
#make another copy of the main dataset
ramphs.joined -> ramphs.joined.capacity
#asign 0 to all the NA in "capacity"
ramphs.joined.capacity$capacity[is.na(ramphs.joined.capacity$capacity)] <- 0
#plot graph of relationship between years and cumulative capacity 
plot(cumsum(zoo((ramphs.joined.capacity$capacity), x.Date)))

#and -- DYGRAPHS + SELECTORS!
dygraph(cumsum(zoo((ramphs.joined.capacity$capacity), x.Date)))%>%
dyRangeSelector(dateWindow = c("0-01-01", "300-01-01"))
## Warning in zoo((ramphs.joined.capacity$capacity), x.Date): some methods
## for "zoo" objects do not work if the index entries in 'order.by' are not
## unique

###########

#Second, I am going to plot the relationship between years and extmajor which is the major length of the amphitheaters
#I already have the "x.Date" object from before, so:
#make a copy of the original dataset
ramphs.joined -> ramphs.joined.extmajor
#asign 0 to all the NA in "extmajor"
ramphs.joined.extmajor$extmajor[is.na(ramphs.joined.extmajor$extmajor)] <- 0
#plot graph of relationship between years and cumulative capacity 
plot(cumsum(zoo((ramphs.joined.extmajor$extmajor), x.Date)))

#---TRY THIS LATER--plot(cumsum(zoo((ramphs.joined$extmajor[ramphs.joined$modcountry==Italy]), x.Date)))

#and -- DYGRAPHS + SELECTORS!
dygraph(cumsum(zoo((ramphs.joined.extmajor$extmajor), x.Date)))%>%
dyRangeSelector(dateWindow = c("0-01-01", "300-01-01"))
## Warning in zoo((ramphs.joined.extmajor$extmajor), x.Date): some methods
## for "zoo" objects do not work if the index entries in 'order.by' are not
## unique

#Now, more cumulative experiments!
#Let's see the relationship between years and cumulative population:
#make a copy of the original dataset
ramphs.joined -> ramphs.joined.population
#asign 0 to all the NA in "population"
ramphs.joined.population$population[is.na(ramphs.joined.population$population)] <- 0
#plot graph of relationship between years and cumulative capacity 
plot(cumsum(zoo((ramphs.joined.population$population), x.Date)))

#and -- DYGRAPHS + SELECTORS!
dygraph(cumsum(zoo((ramphs.joined.population$population), x.Date)))%>%
dyRangeSelector(dateWindow = c("0-01-01", "300-01-01"))
## Warning in zoo((ramphs.joined.population$population), x.Date): some
## methods for "zoo" objects do not work if the index entries in 'order.by'
## are not unique

#----LEAFLET----#


mapoutline <- readOGR("https://gist.githubusercontent.com/sfsheath/7726c6f5fff9aa45ee15/raw/c828e6a3182bc1058b6cee1788a8e58e2dbc5aad/rome200.geojson", layer="OGRGeoJSON", disambiguateFIDs = T)
## OGR data source with driver: GeoJSON 
## Source: "https://gist.githubusercontent.com/sfsheath/7726c6f5fff9aa45ee15/raw/c828e6a3182bc1058b6cee1788a8e58e2dbc5aad/rome200.geojson", layer: "OGRGeoJSON"
## with 112 features and 8 fields
## Feature type: wkbMultiPolygon with 2 dimensions
plot(mapoutline)
plot(ramphs.joined, add = T) #add the amphitheater data over the mapoutline