###chart 1

knitr::opts_chunk$set(echo=TRUE)

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(reshape2)
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:plotly':
## 
##     select
library(weathermetrics)

GB_auto <- raster::getData('GADM', 
                           country="GBR", 
                           level=0, 
                           #set the path to store your data in
                                   path="C:/Users/cex/Documents/Smart Cities and Urban Analytics/GIS/Week 4", 
                           download=TRUE)

GBclim <- raster::getData("worldclim", 
                          res=5, 
                          var="tmean",
                          #set the path to store your data in
                          path="C:/Users/cex/Documents/Smart Cities and Urban Analytics/GIS/Week 4", 
                          download=TRUE)

month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

names(GBclim) <- month
GBtemp <- crop(GBclim, GB_auto)
exactGB <- mask(GBtemp, GB_auto)

#WorldClim data has a scale factor of 10!
exactGB <- exactGB/10

alldf=as.data.frame(exactGB)
squishdata <- melt(alldf, measure.vars=names(alldf))

# split the data for plotly based on month
jan<-subset(squishdata, variable=="Jan", na.rm=TRUE)
jun<-subset(squishdata, variable=="Jun", na.rm=TRUE)

# give axis titles
x <- list (title = "Temperature")
y <- list (title = "Frequency")

# set the bin width
xbinsno<-list(start=-5, end=20, size = 2.5)

# plot the histogram calling all the variables we just set
ihist<-plot_ly(alpha = 0.6) %>%
        add_histogram(x = jan$value,
        xbins=xbinsno, name="January") %>%
        add_histogram(x = jun$value,
        xbins=xbinsno, name="June") %>% 
        layout(barmode = "overlay", xaxis=x, yaxis=y)

ihist

column {data-width=400} ———————————————

###chart 2

Chart 3

library(reshape2)
squishdata <- melt(alldf, measure.vars=names(alldf))
#then subset the data selecting two months
threemonths <- subset(squishdata, variable == "Jan" | variable =="Feb" | variable == "Mar")
#get the mean for each month we selected
library(plyr)
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:plotly':
## 
##     arrange, mutate, rename, summarise
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
meanthreemonths <-  ddply(threemonths,
                          "variable",
                          summarise,
                          grp.mean=mean(value, na.rm=TRUE))
colnames(meanthreemonths)[colnames(meanthreemonths)=="variable"] <- "Month"
head(meanthreemonths)
#select the colour and ill based on the variable
#the intercept is the mean we just calculated, with the lines also based on the column variable

#rename the column from variable to month so it looks nice in the legend of the historgram
colnames(threemonths)[colnames(threemonths)=="variable"] <- "Month"

ggplot(threemonths, aes(x=value, color=Month,fill=Month))+
  geom_histogram(position="identity",alpha=0.5)+
  geom_vline(data=meanthreemonths,
             aes(xintercept=grp.mean,
                 color=Month),
                 lintype="dashed")+
               labs(title="Ggplot histogram of Australian winter temperatures",
                    x="Temperature",
                    y="Frequency")+
               theme_classic()+
               theme(plot.title = element_text(hjust = 0.5))
## Warning: Ignoring unknown parameters: lintype
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 63093 rows containing non-finite values (stat_bin).