This blog post describes the process of creating a map using R of the rainfall in Ireland for the 25 different weather stations and colour coding the symbol for each station according to its median rainfall level in January.
To plot the median rainfall we will be using the following library packages, which may need to be installed on your machine.
install.packages("sf")
install.packages("tidyverse")
install.packages("tmap")
First we load the counties data using the following code.
library("sf")
counties <- st_read('counties.geojson')
## Reading layer `counties' from data source
## `C:\Users\conno\OneDrive\Desktop\COLLEGE\Topics Geocomputation\ass2\counties.geojson'
## using driver `GeoJSON'
## Simple feature collection with 40 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 17528.59 ymin: 19537.25 xmax: 366403.6 ymax: 466923.2
## Projected CRS: TM65 / Irish Grid
head(counties)
## Simple feature collection with 6 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 68614.63 ymin: 68718.06 xmax: 348847.8 ymax: 453198.8
## Projected CRS: TM65 / Irish Grid
## County mean_rain geometry
## 1 Antrim 77.71545 MULTIPOLYGON (((324297.7 42...
## 2 Armagh 77.71545 MULTIPOLYGON (((306664.9 36...
## 3 Carlow County 92.14104 MULTIPOLYGON (((286891.9 18...
## 4 Cavan County 72.24133 MULTIPOLYGON (((205022.8 33...
## 5 Clare County 100.73245 MULTIPOLYGON (((127094 2124...
## 6 Cork City 100.00000 MULTIPOLYGON (((167246.6 75...
To load the counties data we use the st_read function from the sf (simple features) package to load a geojson file called counties.geojson into a R dataframe called counties. A geojson file is a format for encoding a variety of geographic data structures.
The counties geojson data file has the following information:
We will use the counties geometry to draw the county borders which are stored as a multipolygons on to the map. Multipolygons are useful for representing complex geographical shapes or regions that are composed of multiple distinct areas.
Next we load the weather stations data using the following code.
rainfalls <- st_read('weather_stations.geojson')
## Reading layer `weather_stations' from data source
## `C:\Users\conno\OneDrive\Desktop\COLLEGE\Topics Geocomputation\ass2\weather_stations.geojson'
## using driver `GeoJSON'
## Simple feature collection with 49500 features and 11 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 46633.44 ymin: 59756.08 xmax: 330201.3 ymax: 457227.9
## Projected CRS: TM65 / Irish Grid
head(rainfalls)
## Simple feature collection with 6 features and 11 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 181346.8 ymin: 393612.6 xmax: 181346.8 ymax: 393612.6
## Projected CRS: TM65 / Irish Grid
## Year Month Rainfall Station Elevation Easting Northing County Abbreviation
## 1 1850 Jan 169.0 Ardara 15 180787.7 394679 Donegal AR
## 2 1851 Jan 236.4 Ardara 15 180787.7 394679 Donegal AR
## 3 1852 Jan 249.7 Ardara 15 180787.7 394679 Donegal AR
## 4 1853 Jan 209.1 Ardara 15 180787.7 394679 Donegal AR
## 5 1854 Jan 188.5 Ardara 15 180787.7 394679 Donegal AR
## 6 1855 Jan 32.3 Ardara 15 180787.7 394679 Donegal AR
## Source coast_dist geometry
## 1 Briffa 17.36709 POINT (181346.8 393612.6)
## 2 Briffa 17.36709 POINT (181346.8 393612.6)
## 3 Briffa 17.36709 POINT (181346.8 393612.6)
## 4 Briffa 17.36709 POINT (181346.8 393612.6)
## 5 Briffa 17.36709 POINT (181346.8 393612.6)
## 6 Briffa 17.36709 POINT (181346.8 393612.6)
The weather station data set contains information about rainfall at the 25 different Irish weather stations. These measurements were performed monthly every year from 1850 to 2015. We again use st_read to load a geojson file called weather_stations.geojson into a R dataframe called rainfalls.
The weather stations data file has the following information:
To calculate the median rainfall for January of all stations we use the following code:
library(tidyverse)
# Pick out jan 1980 and compute median rainfall for each station
rf <- rainfalls %>%
dplyr::filter(Month == 'Jan') %>%
group_by(Station) %>%
summarise(median = median(Rainfall))
#head(rf)
First we include the tidyverse library, which also has the dplyr package in it. The dplyr package provides functions which are designed to easily allow us to manipulate data in R when compared to base R functions.
We will make use of pipe operations written as %>% in R code to perform operations or computations to data and then use the pipe to pass this data to the next stage and so on, until we get the resultant data we want.
We pass our Rainfall data stored in the rainfalls r dataframe to the filter function by using a pipe %>% in the code above. The filter command is used to extract a subset of the Rainfall data where the Month is January. This subset of the data is then passed again using a pipe to the group_by function which groups the data together based on its Station value. The grouped data is then sent to the sumamrise function which calculates the median Rainfall for each group. We then assign the result to a variable called rf.
To plot the median rainfall on the map the following code is used:
library(tmap)
tmap_mode('plot')
tm_shape(counties) +
tm_borders() +
tm_shape(rf) +
tm_dots(col='median', size=0.7, style='cont')
To draw a map we use the tmap library which allows us to create thematic maps in R. The tmap mode is set to plot which sets the plotting device o the default plotting device, usually the R plotting window or a file if set as an output.
To draw the outline of the counties border we use tm_shape(counties) + tm_borders(). This sets up a layer in the map using the counties object and plots counties borders using tm_borders(). The counties borders are stored in the geometry variable of counties as a sfc_MULTIPOLYGON.
To draw the weather stations with their associated median rainfall calculated for January. We use tm_shape(rf) + tm_dots(col=‘median’, size=0.7, style=‘cont’) This adds another layer to the map, which will be used to display the weather stations at their location with a color code for their median rainfall value. It uses the previously calculated rf data set which contains the median rainfall for all the stations, and their locations. The location is represented by a sfc_POINT type, stored in a geometry field. We plot dots on the map representing a station. The dots are colored based on the ‘median’ column/value from the rf data set, with a specified size and continuous style. The style=“cont”, means we want to use a continuous style for the legend.
We can see from the rainfall data when we plot it on the map that the observed rainfall is higher the nearer the station is to the coast line, It decreases as we move inland further from the coast.