library(DT)
library(readr)
library(ggplot2)
library(dplyr)
library(lubridate)
library(aqm)
library(plotly)
# The line below is just used to look at the format of the data

# d<-read_lines("https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/hurndata.txt")
# head(d,10)
d<-read.table("https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/hurndata.txt",skip=7)
# str(d)
names(d)<- c("Year", "Month", "tmax", "tmin",  "af","rain", "sun")
# str(d)
str(d)
## 'data.frame':    756 obs. of  7 variables:
##  $ Year : int  1957 1957 1957 1957 1957 1957 1957 1957 1957 1957 ...
##  $ Month: int  1 2 3 4 5 6 7 8 9 10 ...
##  $ tmax : Factor w/ 197 levels "0.4","10.0","10.1",..: 188 193 31 44 62 125 119 109 79 60 ...
##  $ tmin : Factor w/ 157 levels "-0.1","-0.2",..: 83 85 114 103 114 146 70 58 154 124 ...
##  $ af   : Factor w/ 27 levels "0","1","10","10*",..: 25 26 22 14 2 1 1 1 2 14 ...
##  $ rain : Factor w/ 559 levels "0.4","0.5","0.9",..: 452 11 404 341 310 221 469 453 466 388 ...
##  $ sun  : Factor w/ 545 levels "---","101.0",..: 1 1 1 1 1 1 1 1 1 1 ...
DT::datatable(d, filter = "top", extensions = c("Buttons"), 
        options = list(dom = "Blfrtip", buttons = c("copy", "csv", 
            "excel"), colReorder = TRUE))
d$tmin<-clean(d$tmin)
d$tmax<-clean(d$tmax)
d$af<-clean(d$af)
d$rain<-clean(d$rain)
d$sun<-clean(d$sun)
## Warning in clean(d$sun): NAs introduced by coercion
d$Month<- as.factor(d$Month)             ## Change the numeric variable to a factor
d$date<-as.Date( paste(d$Year,d$Month , 15 , sep = "/" )  , format = "%Y/%m/%d" )
d$month<-lubridate:::month(d$date,label=TRUE)
library(ggthemes)

theme_set(theme_bw())
g1<-ggplot(d,aes(x=date,y=tmin)) + geom_line() 
g1

library(dplyr)
d %>% group_by(Year) %>% summarise(tmin=round(mean(tmin),2),tmax=round(mean(tmax),2),rain=sum(rain)) -> yrly
library(ggthemes)
library(bbplot)

g1<-ggplot(yrly,aes(x=Year,y=tmax))
g1 +geom_point() +geom_line() +geom_smooth() + bbc_style()