setwd("G:/R assignment Chris")
getwd()
## [1] "G:/R assignment Chris"
load("Rainfall.RData")
head(stations)
## Station Elevation Easting Northing Lat Long County
## 1 Athboy 87 270400 261700 53.60 -6.93 Meath
## 2 Foulksmills 71 284100 118400 52.30 -6.77 Wexford
## 3 Mullingar 112 241780 247765 53.47 -7.37 Westmeath
## 4 Portlaw 8 246600 115200 52.28 -7.31 Waterford
## 5 Rathdrum 131 319700 186000 52.91 -6.22 Wicklow
## 6 Strokestown 49 194500 279100 53.75 -8.10 Roscommon
## Abbreviation Source
## 1 AB Met Eireann
## 2 F Met Eireann
## 3 M Met Eireann
## 4 P Met Eireann
## 5 RD Met Eireann
## 6 S Met Eireann
head(rain)
## Year Month Rainfall Station
## 1 1850 Jan 169.0 Ardara
## 2 1851 Jan 236.4 Ardara
## 3 1852 Jan 249.7 Ardara
## 4 1853 Jan 209.1 Ardara
## 5 1854 Jan 188.5 Ardara
## 6 1855 Jan 32.3 Ardara
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.4.3
library(dygraphs)
## Warning: package 'dygraphs' was built under R version 3.4.3
leaflet(data = stations,height = 430,width = 390) %>% addTiles %>% setView(-8,53.5,6) %>%
addCircleMarkers
## Assuming 'Long' and 'Lat' are longitude and latitude, respectively
leaflet(data = stations,height = 430,width = 390) %>% addProviderTiles("CartoDB.Positron") %>%
setView(-8,53.5,6) %>% addCircleMarkers
## Assuming 'Long' and 'Lat' are longitude and latitude, respectively
load("Maps.RData")
leaflet(data = counties.spdf,height = 430,width=390) %>% addTiles %>%
addPolygons(fillOpacity = 0.4,weight = 1,color = "black")
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
rain %>% group_by(Station) %>%
summarise(mrain=median(Rainfall)) -> rain_summary
## Warning: package 'bindrcpp' was built under R version 3.4.3
head(rain_summary)
## # A tibble: 6 x 2
## Station mrain
## <chr> <dbl>
## 1 Ardara 132
## 2 Armagh 65.4
## 3 Athboy 70.7
## 4 Belfast 82.1
## 5 Birr 66.2
## 6 Cappoquinn 113
rain %>% group_by(Month) %>%
summarise(mrain=median(Rainfall)) -> rain_months
head(rain_months)
## # A tibble: 6 x 2
## Month mrain
## <fctr> <dbl>
## 1 Jan 105
## 2 Feb 74.4
## 3 Mar 73.1
## 4 Apr 62.5
## 5 May 65.5
## 6 Jun 65.2
rain %>% group_by(Month,Station) %>%
summarise(median_rain=median(Rainfall)) -> rain_month_station
head(rain_month_station)
## # A tibble: 6 x 3
## # Groups: Month [1]
## Month Station median_rain
## <fctr> <chr> <dbl>
## 1 Jan Ardara 172
## 2 Jan Armagh 75.0
## 3 Jan Athboy 87.1
## 4 Jan Belfast 102
## 5 Jan Birr 77.5
## 6 Jan Cappoquinn 147
January <- rain_month_station[1:25, ]
January %>% group_by(Station) %>%
summarise(mrain=median(median_rain)) -> rain_summary
January %>% group_by(Month) %>%
summarise(mrain=median(median_rain)) -> rain_months
January %>% group_by(Month,Station) %>%
summarise(median_rain=median(median_rain)) -> rain_month_station
rain_summary %>% left_join(stations) -> Station_medians
## Joining, by = "Station"
colour_fun <- colorNumeric('Blues',Station_medians$mrain)
previewColors (colour_fun,fivenum(Station_medians$mrain))
colour_fun
fivenum(Station_medians$mrain)
| 63 | |
| 92.9 | |
| 105.7 | |
| 126.4 | |
| 177.7 |
leaflet(data=Station_medians,height=430,width=600) %>% addProviderTiles('CartoDB.Positron') %>%
setView(-8,53.5,6) %>% addCircleMarkers(fillColor=~colour_fun(mrain),weight=0,fillOpacity = 0.85) %>%
addLegend(pal=colour_fun,values=~mrain,title="Jan. median rainfall",position='bottomleft')
## Assuming 'Long' and 'Lat' are longitude and latitude, respectively
rain_month_station$median_rain %>% ts(freq=1,start = 1850) -> Jan_rain_ts
View(Jan_rain_ts)
####(Jan_rain_ts = time series object).
Jan_rain_ts %>%
monthplot(col='dodgerblue',col.base='indianred',lwd.base=3)
local_monthplot <- function(station,raindata) {
raindata %>% filter(Station == station) -> local_rd
local_rd$Rainfall %>% ts(freq=12,start =1850) -> rain_ts
rain_ts %>% monthplot(col="dodgerblue",col.base = "indianred",lwd.base = 3)
}
stations %>% mutate(Filename=paste0('mp',Station,'.png')) ->files
files %>% select(Station,Filename) %>% View
leaflet(data=counties.spdf,height=430,width=600) %>%
addTiles %>% addPolygons(fillOpacity=0.4,weight=1,color='black',popup='<img src="test.png">')
rain %>% arrange(Year,Month) -> rain2
for (i in 1:nrow(files))
with(files, {
png(Filename[i],width=400,height=300)
local_monthplot(Station[i],rain2)
dev.off()} )
files %>% mutate(Popup=paste0('<img src="',Filename,'">')) -> files
files %>% select(Station,Popup) %>% head
## # A tibble: 6 x 2
## Station Popup
## <chr> <chr>
## 1 Athboy "<img src=\"mpAthboy.png\">"
## 2 Foulksmills "<img src=\"mpFoulksmills.png\">"
## 3 Mullingar "<img src=\"mpMullingar.png\">"
## 4 Portlaw "<img src=\"mpPortlaw.png\">"
## 5 Rathdrum "<img src=\"mpRathdrum.png\">"
## 6 Strokestown "<img src=\"mpStrokestown.png\">"
rain %>% group_by(Station) %>% summarise(mrain=median(Rainfall)) %>% left_join(files) %>%
select(Long,Lat,mrain,Popup) -> station_medians_2
## Joining, by = "Station"
color_fun <- colorNumeric('Blues',station_medians_2$mrain)
leaflet(data=Station_medians,height=430,width=600) %>% addProviderTiles('CartoDB.Positron') %>%
setView(-8,53.5,6) %>%
addCircleMarkers(fillColor=color_fun(Station_medians$mrain),weight=0,
fillOpacity = 0.85,popup=station_medians_2$Popup) %>%
addLegend(pal=color_fun,values=Station_medians$mrain,title="Rainfall",position='bottomleft')
## Warning in color_fun(Station_medians$mrain): Some values were outside the
## color scale and will be treated as NA
## Assuming 'Long' and 'Lat' are longitude and latitude, respectively
## Warning in pal(c(r[1], cuts, r[2])): Some values were outside the color
## scale and will be treated as NA
rain %>% group_by(Year,Month) %>% summarise(rf=sum(Rainfall)) -> monthly_total
monthly_total$rf %>% ts(freq=12,start = 1850) -> rain_ts ####(rain_ts = time series object).
rain_ts %>% dygraph
rain_ts %>% window(c(1850,1),c(2014,12)) %>% dygraph(width=800,height=300)
rain_ts %>% dygraph(width=800,height=300) %>% dyRangeSelector
rain %>% group_by(Year,Month) %>% filter(Station=="Dublin Airport") %>%
summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
ts(start=c(1850,1),freq=12) -> dub_ts
rain %>% group_by(Year,Month) %>% filter(Station=="Belfast") %>%
summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
ts(start=c(1850,1),freq=12) -> bel_ts
rain %>% group_by(Year,Month) %>% filter(Station=="University College Galway") %>%
summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
ts(start=c(1850,1),freq=12) -> UCG_ts
rain %>% group_by(Year,Month) %>% filter(Station=="Cork Airport") %>%
summarise(Rainfall=sum(Rainfall)) %>% ungroup %>% transmute(Rainfall) %>%
ts(start=c(1850,1),freq=12) -> Cork_ts
dub_ts %>% dygraph(width=800,height=170,group="dub_belf_Cork_UCG",main="Dublin")
bel_ts %>% dygraph(width=800,height=170,group="dub_belf_Cork_UCG",main="Belfast")
Cork_ts %>% dygraph(width = 800,height = 170,group = "dub_belf_Cork_UCG", main = "Cork")
UCG_ts %>% dygraph(width = 800,height = 170,group = "dub_belf_Cork_UCG", main = "UCG") %>%
dyRangeSelector