Introduction

This blog, will create a map of the rainfall across Ireland, focusing on 25 weather stations. The goal is to visually represent the median rainfall levels for January at each station. The map symbols will be colour-coded darkest to lightest, based on the amount of rainfall recorded, illustrating an intuitive representation of regional weather patterns. The final code will illustrate an interactive map that showcases the geographical distribution of rainfall in the month of January.

Set the Working Directory

Firstly, the working directory was set into a reachable file. loading in required data.

setwd("~/Downloads/GY672A2")
load('rainfall.RData')
# stations
# rain

Installing Required Packages

The following packages were installed for the creation of codes and making the final median rainfall map.

install.packages("leaflet", repos = "http://cloud.r-project.org/")
## 
## The downloaded binary packages are in
##  /var/folders/69/fwlw_ckn7_51244gk9vvwfym0000gn/T//RtmpeUItPT/downloaded_packages
install.packages("tmap", repos = "http://cloud.r-project.org/")
## 
## The downloaded binary packages are in
##  /var/folders/69/fwlw_ckn7_51244gk9vvwfym0000gn/T//RtmpeUItPT/downloaded_packages
install.packages("sf", repos = "http://cloud.r-project.org/")
## 
## The downloaded binary packages are in
##  /var/folders/69/fwlw_ckn7_51244gk9vvwfym0000gn/T//RtmpeUItPT/downloaded_packages
install.packages("mapview", repos = "http://cloud.r-project.org/")
## 
## The downloaded binary packages are in
##  /var/folders/69/fwlw_ckn7_51244gk9vvwfym0000gn/T//RtmpeUItPT/downloaded_packages
install.packages("dplyr", repos = "http://cloud.r-project.org/")
## 
## The downloaded binary packages are in
##  /var/folders/69/fwlw_ckn7_51244gk9vvwfym0000gn/T//RtmpeUItPT/downloaded_packages

Packages Explained:

Leaflet- Creates interactive maps using markers, legends, and pop-ups.

Tmap- for creating interactive maps.

SF- Simple Features; for working with spatial data.

Dplyr- Data tool used for filtering, sumarising datasets.

Map view- provides interactive maps with built in spatial data. (Tennekes, 2018)

library(leaflet)
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(leafpop)
library(dplyr)
## 
## 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

Creating the Code:

The pipe operator ‘%>%’ is used to link sequence of analysis steps (Kubota, 2022). The code below grouped the rainfall data by the month of January.

rain %>% group_by(Month) %>%
filter(Month=='Jan')  
## # A tibble: 4,125 × 4
## # Groups:   Month [1]
##     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 
##  7  1856 Jan      152.  Ardara 
##  8  1857 Jan      179.  Ardara 
##  9  1858 Jan      110.  Ardara 
## 10  1859 Jan      158.  Ardara 
## # ℹ 4,115 more rows

Below shows the same code except integrated the function -> to specify names within the code.

rain %>% group_by(Month) %>%
filter(Month=='Jan') -> jan_filter
jan_filter
## # A tibble: 4,125 × 4
## # Groups:   Month [1]
##     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 
##  7  1856 Jan      152.  Ardara 
##  8  1857 Jan      179.  Ardara 
##  9  1858 Jan      110.  Ardara 
## 10  1859 Jan      158.  Ardara 
## # ℹ 4,115 more rows

The code below used the ‘jan_filter’ code that was previously created and grouped the January rainfall data by station. The code now currently has the two required variables. ‘Medrain’ was used -> to call out the median rainfall for each station for the month of January.

jan_filter %>% group_by(Station) %>%
summarise(medrain=median(Rainfall))  ->medrain 
medrain
## # A tibble: 25 × 2
##    Station        medrain
##    <chr>            <dbl>
##  1 Ardara           172. 
##  2 Armagh            75  
##  3 Athboy            87.1
##  4 Belfast          102. 
##  5 Birr              77.5
##  6 Cappoquinn       147. 
##  7 Cork Airport     135. 
##  8 Derry             97.3
##  9 Drumsna           99.1
## 10 Dublin Airport    63  
## # ℹ 15 more rows

The code below specifies a seperate file path for each station, which can be called back out when creating the map.

stations %>% mutate(Filename=file.path(getwd(),paste0(Station,'.png'))) -> files
files %>% select(Station,Filename)
## # A tibble: 25 × 2
##    Station                   Filename                                           
##    <chr>                     <chr>                                              
##  1 Athboy                    /Users/aoifeearley/Downloads/GY672A2/Athboy.png    
##  2 Foulksmills               /Users/aoifeearley/Downloads/GY672A2/Foulksmills.p…
##  3 Mullingar                 /Users/aoifeearley/Downloads/GY672A2/Mullingar.png 
##  4 Portlaw                   /Users/aoifeearley/Downloads/GY672A2/Portlaw.png   
##  5 Rathdrum                  /Users/aoifeearley/Downloads/GY672A2/Rathdrum.png  
##  6 Strokestown               /Users/aoifeearley/Downloads/GY672A2/Strokestown.p…
##  7 University College Galway /Users/aoifeearley/Downloads/GY672A2/University Co…
##  8 Drumsna                   /Users/aoifeearley/Downloads/GY672A2/Drumsna.png   
##  9 Ardara                    /Users/aoifeearley/Downloads/GY672A2/Ardara.png    
## 10 Armagh                    /Users/aoifeearley/Downloads/GY672A2/Armagh.png    
## # ℹ 15 more rows

The code below selects the following, Station, Latitude, Longitude, and Filename out of the dataset and sets the co-ordinate system for the Lat long.

jan_filter %>% group_by(Station) %>%  
summarise(mrain=median(Rainfall))  %>% left_join(files)  %>%
select(Station,Long,Lat,mrain,Filename) %>% 
st_as_sf(coords=c('Long','Lat'),crs=4326) -> station_med
## Joining with `by = join_by(Station)`
station_med
## Simple feature collection with 25 features and 3 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -10.23 ymin: 51.79 xmax: -5.99 ymax: 55.36
## Geodetic CRS:  WGS 84
## # A tibble: 25 × 4
##    Station        mrain Filename                            geometry
##  * <chr>          <dbl> <chr>                            <POINT [°]>
##  1 Ardara         172.  /Users/aoifeearley/Downloads/… (-8.29 54.79)
##  2 Armagh          75   /Users/aoifeearley/Downloads/… (-6.64 54.35)
##  3 Athboy          87.1 /Users/aoifeearley/Downloads/…  (-6.93 53.6)
##  4 Belfast        102.  /Users/aoifeearley/Downloads/…  (-5.99 54.5)
##  5 Birr            77.5 /Users/aoifeearley/Downloads/… (-7.88 53.08)
##  6 Cappoquinn     147.  /Users/aoifeearley/Downloads/…  (-7.8 52.19)
##  7 Cork Airport   135.  /Users/aoifeearley/Downloads/… (-8.47 51.83)
##  8 Derry           97.3 /Users/aoifeearley/Downloads/… (-7.58 54.99)
##  9 Drumsna         99.1 /Users/aoifeearley/Downloads/…    (-8 53.91)
## 10 Dublin Airport  63   /Users/aoifeearley/Downloads/…   (-6.2 53.4)
## # ℹ 15 more rows

The code below was used to create the base map with each station colour coded darkest to lightest, highest to lowest respectively by median rainfall. This code used ‘CircleMarkers’ as the symbol on the map. Pop-up was embedded in the map as ‘filename’ i.e. the location of weather station.

tm_start2 <-tm_shape(station_med) + 
tm_dots(col='mrain',popup.vars=FALSE,scale=1.5)
tmap_leaflet(tm_start2) %>%
addCircleMarkers(data=station_med,
stroke = NA,
popup=popupImage(station_med$Filename,embed=TRUE))

Discussion

The final map shows the median rainfall for the month of January across the 25 weather stations in Ireland, with darker colours representing higher rainfall (160–180 mm) and lighter colours representing lower rainfall (60–100 mm). Coastal areas, particularly along the western coast of Ireland, experience the highest rainfall because of their exposure to the Atlantic ocean. In contrast, inland and eastern regions show lower rainfall measures, as they are sheltered from prevailing westerly. Stations on the west coast for example Valentia and Ardara are likely among the highest rainfall locations, while stations in the sheltered east, such as Dublin Airport and Phoenix Park, have lower rainfall measures. This map reflects Ireland’s wet climate, strongly influenced by the Atlantic weather patterns.

Conclusion

In conclusion, after analysing the median rainfall levels at Ireland’s 25 weather stations, there is visibly significant geographical differences in rainfall patterns. The data shows that the median rainfall at stations in the western and coastal regions is generally higher than stations inland and on the eastern coast, therefore local authorities must ensure that these stations are monitored so areas don’ become high risk for future flooding.

Bibliography

Kubota, T. (2022) ‘Diseases maps of spatial epidemiological data by R’, WIREs Computational Statistics, 15(4). doi:10.1002/wics.1604.

Tennekes, M. (2018) ‘tmap: Thematic maps in r’, Journal of Statistical Software, 84(6). doi:10.18637/jss.v084.i06.