Creating a map of average annual rainfall in Ireland for the 25 weather stations, colour coding the symbol for each station according to its median rainfall level in January.

To start this assignemnt download the rainfall data from https://2017.moodle.maynoothuniversity.ie/mod/resource/view.php?id=122084

Once this file has been downloaded, load it in R and observe the data. What the data contains is two dataframes, one which holds the information about the stations rainfall per month for each year from 1850 to 2014. The other contains station information such as longditude, latitude, elevation, and what county it lies in.

library(leaflet)
library(dplyr)

setwd("~/R/Chris assignment 20_01_2017")
load("maps.RData")
load("rainfall.RData")

head(rain)
# A tibble: 6 × 4
   Year  Month Rainfall Station
  <dbl> <fctr>    <dbl>   <chr>
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
head(stations)
# A tibble: 6 × 9
      Station Elevation Easting Northing   Lat  Long    County
        <chr>     <int>   <dbl>    <dbl> <dbl> <dbl>     <chr>
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
# ... with 2 more variables: Abbreviation <chr>, Source <chr>

In order to complete this assignment, firstly group the rain data by station and then filter for the month of January. Then create the median value for each station and join it to the station data, so that the stations latitude, longditude, and median rainfall in January are all on the same dataframe.

rain %>% group_by(Station) %>% filter(Month=="Jan") %>% summarise(mrain=median(Rainfall))->med_rain
med_rain %>% left_join(stations) -> med_rain
head(med_rain)
# A tibble: 6 × 10
     Station mrain Elevation  Easting Northing   Lat  Long    County
       <chr> <dbl>     <int>    <dbl>    <dbl> <dbl> <dbl>     <chr>
1     Ardara 171.6        15 180787.7 394679.0 54.79 -8.29   Donegal
2     Armagh  75.0        62 287831.3 345772.0 54.35 -6.64    Armagh
3     Athboy  87.1        87 270400.0 261700.0 53.60 -6.93     Meath
4    Belfast 102.1       115 329623.4 363141.3 54.50 -5.99    Antrim
5       Birr  77.5        73 208016.8 203400.5 53.08 -7.88    Offaly
6 Cappoquinn 147.4        76 213268.9 104799.9 52.19 -7.80 Waterford
# ... with 2 more variables: Abbreviation <chr>, Source <chr>

The next step is to make the map. There will be month plots of each station that will pop up on the map and this will be created first. For the efficient use of time a function will be created.

local_monthplot <- function(station,raindata) {
  raindata %>% filter(Station == station) -> local_raindata
  local_raindata$Rainfall %>% ts(freq=12,start=1850) -> rain_time_series
  rain_time_series %>% monthplot(col='dodgerblue',col.base='indianred',lwd.base=3)
}

Next is to create the pop up files.

stations %>% mutate(Filename =paste0('mp',gsub('','',Station),'.png')) -> stations_pop

stations_pop %>% select(Station,Filename) %>% head
# A tibble: 6 × 2
      Station          Filename
        <chr>             <chr>
1      Athboy      mpAthboy.png
2 Foulksmills mpFoulksmills.png
3   Mullingar   mpMullingar.png
4     Portlaw     mpPortlaw.png
5    Rathdrum    mpRathdrum.png
6 Strokestown mpStrokestown.png

As can be seen above there is a new column which wil store the images of the pop up month plots. A loop to create all of the monthplots will be created.

for (i in 1:nrow(stations_pop))
  with(stations_pop, {
    png(Filename[i],width=400,height=300)
    local_monthplot(Station[i],rain)
    dev.off()} )

Check where your working directory, there should be 25 month plots for the weather stations in Ireland. We now want to make the month plots pop ups.

stations_pop %>% mutate(Popup=paste0('<img src="',Filename,'">')) -> stations_pop
stations_pop %>% select(Station,Popup) %>% head
# A tibble: 6 × 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">

Next step is to put all the information on the one dataframe so longditude, latitude, median rain, and the pop ups of the month plots into one single dataframe.

rain %>% group_by(Station) %>%  summarise(mrain=mean(Rainfall))  %>% left_join(stations_pop)  %>% select(Long,Lat,mrain,Popup) -> station_means
station_means %>% head
# A tibble: 6 × 4
   Long   Lat     mrain                        Popup
  <dbl> <dbl>     <dbl>                        <chr>
1 -8.29 54.79 140.36753     <img src="mpArdara.png">
2 -6.64 54.35  68.32096     <img src="mpArmagh.png">
3 -6.93 53.60  74.74356     <img src="mpAthboy.png">
4 -5.99 54.50  87.10995    <img src="mpBelfast.png">
5 -7.88 53.08  70.83498       <img src="mpBirr.png">
6 -7.80 52.19 121.22455 <img src="mpCappoquinn.png">

The map is now ready to be created.

color_fun <- colorNumeric('Blues',med_rain$mrain)

leaflet(data=med_rain,height=500,width=450) %>% addTiles %>% addProviderTiles('Esri.WorldImagery') %>% setView(-8,53.5,6) %>% addCircleMarkers(fillColor=color_fun(med_rain$mrain), weight=3, fillOpacity = 0.8,popup=station_means$Popup) %>% addLegend(pal=color_fun,values=med_rain$mrain,title="Rainfall",position='bottomright')

To ensure what you have done is correct, create a bar plot of the median rainfall in January for the 25 weather stations and compare the high and low values to the map.

barplot(med_rain$mrain,names=med_rain$Station, las=3, cex.names = 0.7, ylim=c(0,200), col='dodgerblue', ylab = "Rainfall (mm)", main = "Median rainfall for January for Irish weather stations")

As you can see, Valentia, Ardara, and Killarney are all above 150mm on the bar chart. It is clear from the map and bar chart that the weather stations located in the west, mainly the north and south of Ireland have recorded higher median levels of rainfall from 1850 to 2014. It can also be said that the Dublin area and midlands recorded lower median rainfall during January over the same time period.