Set the working directory to the folder containing the data to be used:
setwd("C:\\Users\\Aoife\\Desktop\\MSc\\GY672\\CB")
The relevant libraries must be loaded.
library(leaflet) # leaflet
library(dplyr) # group_by, filter, summarise
##
## 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
library(reshape2) # acast
The dataset used here is the rainfall dataset.
load('rainfall.RData')
The above dataset was downloaded from NCG602A Moodle and made available by Conor Murphy and Simon Noone. This dataset contains information about 25 rainfall stations across Ireland.
head(stations)
## # A tibble: 6 x 9
## Station Elevation Easting Northing Lat Long County Abbreviation Sour~
## <chr> <int> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr>
## 1 Athboy 87 270400 261700 53.6 -6.93 Meath AB Met ~
## 2 Foulks~ 71 284100 118400 52.3 -6.77 Wexfo~ F Met ~
## 3 Mullin~ 112 241780 247765 53.5 -7.37 Westm~ M Met ~
## 4 Portlaw 8 246600 115200 52.3 -7.31 Water~ P Met ~
## 5 Rathdr~ 131 319700 186000 52.9 -6.22 Wickl~ RD Met ~
## 6 Stroke~ 49 194500 279100 53.8 -8.1 Rosco~ S Met ~
dim(stations)
## [1] 25 9
It also includes monthly rainfall values for each station from 1850 to 2014.
head(rain)
## # A tibble: 6 x 4
## Year Month Rainfall Station
## <dbl> <fct> <dbl> <chr>
## 1 1850 Jan 169 Ardara
## 2 1851 Jan 236. Ardara
## 3 1852 Jan 250. Ardara
## 4 1853 Jan 209. Ardara
## 5 1854 Jan 188. Ardara
## 6 1855 Jan 32.3 Ardara
tail(rain)
## # A tibble: 6 x 4
## Year Month Rainfall Station
## <dbl> <fct> <dbl> <chr>
## 1 2009 Dec 99.9 Waterford
## 2 2010 Dec 70.2 Waterford
## 3 2011 Dec 80.7 Waterford
## 4 2012 Dec 114. Waterford
## 5 2013 Dec 136. Waterford
## 6 2014 Dec 28.7 Waterford
There is no missing data in the set. The monthly values can be examined and different summary statistics investigated. In the case of this experiment, median rainfall values are examined, particularly for the month of January and these values are mapped. Thus, spatial patterns in the data can be determined.
rain_summary <- rain %>% group_by(Month,Station) %>% summarise(mrain=median(Rainfall))
head(rain_summary)
## # A tibble: 6 x 3
## # Groups: Month [1]
## Month Station mrain
## <fct> <chr> <dbl>
## 1 Jan Ardara 172.
## 2 Jan Armagh 75
## 3 Jan Athboy 87.1
## 4 Jan Belfast 102.
## 5 Jan Birr 77.5
## 6 Jan Cappoquinn 147.
This gives the median rainfall for all months and stations.
rain_summary %>% acast(Station~Month) %>% head
## Using mrain as value column: use value.var to override.
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
## Ardara 171.6 127.5 114.5 95.9 93.7 104.1 121.8 143.7 143.3 169.0 176.9
## Armagh 75.0 51.4 56.3 52.4 54.3 57.1 68.3 82.1 67.6 79.7 71.4
## Athboy 87.1 55.9 63.4 56.8 57.8 61.9 72.4 84.5 71.3 84.4 76.6
## Belfast 102.1 70.4 69.8 62.5 63.5 67.4 82.0 99.1 84.5 104.2 95.8
## Birr 77.5 53.3 58.4 49.2 57.3 56.1 72.1 83.6 68.2 82.0 74.8
## Cappoquinn 147.4 107.1 110.0 89.4 90.5 83.9 97.0 119.3 108.9 141.6 131.9
## Dec
## Ardara 186.2
## Armagh 75.3
## Athboy 82.5
## Belfast 100.6
## Birr 78.0
## Cappoquinn 148.8
This arranges the data into a 2-D display which is useful for creating heatmaps.
rain_summary %>% acast(Station~Month) %>% heatmap(Colv=NA)
## Using mrain as value column: use value.var to override.
This heat map visualises the spatial and seasonal patterns in the data. It can be seen that autumn and winter are the wettest seasons. In most months, including January, stations along the south and west coasts, such as Ardara and Valentia, appear to experience higher levels of rainfall than other stations, such as Dublin Airport. This suggests a possible spatial trend to median monthly rainfall which may be clarified by mapping the data.
The data can be examined for individual rows such as station, month, and year. For this experiment, median rainfall values will be examined for the month of January.
median_Jan <- rain %>% group_by(Station, Month) %>% filter(Month=='Jan') %>% summarise(mrain=median(Rainfall))
head(median_Jan)
## # A tibble: 6 x 3
## # Groups: Station [6]
## Station Month mrain
## <chr> <fct> <dbl>
## 1 Ardara Jan 172.
## 2 Armagh Jan 75
## 3 Athboy Jan 87.1
## 4 Belfast Jan 102.
## 5 Birr Jan 77.5
## 6 Cappoquinn Jan 147.
The dataset has now been sorted by station and filtered so that only median values for the month of January are included. This has formed a new data frame.
In order to map the median rainfall values for January, the data must first be linked to the stations geographical information using the left_join() function.
Jan_rain <- median_Jan %>% left_join(stations)
## Joining, by = "Station"
head(Jan_rain)
## # A tibble: 6 x 11
## # Groups: Station [6]
## Station Month mrain Elevation Easting Northing Lat Long County
## <chr> <fct> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <chr>
## 1 Ardara Jan 172. 15 180788. 394679. 54.8 -8.29 Doneg~
## 2 Armagh Jan 75 62 287831. 345772. 54.4 -6.64 Armagh
## 3 Athboy Jan 87.1 87 270400 261700 53.6 -6.93 Meath
## 4 Belfast Jan 102. 115 329623. 363141. 54.5 -5.99 Antrim
## 5 Birr Jan 77.5 73 208017. 203400. 53.1 -7.88 Offaly
## 6 Cappoq~ Jan 147. 76 213269. 104800. 52.2 -7.8 Water~
## # ... with 2 more variables: Abbreviation <chr>, Source <chr>
color_rain <- colorNumeric('Blues',Jan_rain$mrain)
previewColors(color_rain,fivenum(Jan_rain$mrain))
color_rain
fivenum(Jan_rain$mrain)
| 63 | |
| 92.9 | |
| 105.7 | |
| 126.4 | |
| 177.7 |
This colour scheme can be used to colour code the station symbols on the map by their median rainfall; it has been compiled based off the mrain column in the Jan_rain dataset.
The leaflet() function can be used for interactive mapping.
leaflet(data=Jan_rain,height=430,width=600) %>% addProviderTiles('Stamen.TonerLite') %>%
setView(-8,53.5,6) %>% addCircleMarkers(fillColor=~color_rain(mrain),weight=0,fillOpacity = 0.85) %>%
addLegend(pal=color_rain,values=~mrain,title="Jan Rainfall",position='bottomleft')
## Assuming "Long" and "Lat" are longitude and latitude, respectively
As was suggested by the heatmap of monthly median rainfall, there is a distinct spatial pattern evident in this map. The stations with the highest median rainfall for January are those on the south and west coasts.
There are a number of reasons behind this spatial pattern. Most importantly:
As Ireland’s prevailing winds are south-westerly, they reach the south and west coasts of the island first and deposit much of their moisture there. This has a “rain shadow” effect, as the air flow reaches the midlands and beyond, it has released most of its moisture and cannot regain it over land. Thus, the midlands and north-east receive less precipitation when there are south-westerly winds. As these are the prevailing winds, it can be expected that the south and west coasts often experience greater rainfall than the midlands and north-east.
Futhermore, south-westerly airflows hold more moisture than north-easterly airflows. Air coming from the south is warmer than air coming from the north and, thus, has a greater capacity to hold moisture. South-westerly airflows also have a greater expanse of ocean to travel across i.e. the Atlantic Ocean, than north-easterly flows which generally travel across continental or Northern Europe rather than across any large body of water. Hence, south-westerly airflows generally gather more moisture as they move than north-easterly flows. Therefore, the rain deposited by south-westerly flows on the south and west of the country is usually greater than that deposited in the north and east of the country by north-easterly flows.
There is a clear spatial pattern in median January rainfall across Ireland. This can be seen easily in the map of January rainfall created using leaflet. It was also suggested by the heatmap of median monthly rainfall values. Ireland’s prevailing south-westerly winds have a large impact on this spatial distribution of rainfall, leading to the south and west of the country experiencing heavier rainfall than the midlands, north, and east.