The following code was used to create the map below showing the median rainfall in mm for the month of January in 25 weather stations across Ireland.
The first three ‘library’ commands open the packages needed to create the map from the data. The first package ‘sf’ allows the system to read geojson files, the second ‘tmap’ allows the system to plot maps using geojson files and the last package ‘tidyverse’ allows the system to understand a more intuitive sequence of writing code that was easier for the analyst to follow.
The ‘weather_stations.geojson’ and the ‘counties. geojson’ are geojson files, which are file types that contain geographical data to represent features in the location specified with coordinates. In this case, the files contain the geographical location of the Irish counties and the location of the weather stations. When plotted together the counties data file gives context to the weather stations file as you can see where they are located on the island of Ireland. Without the county file, the weather stations would be dotted on a blank surface without context of where they are in the world.
The second part of the code (where the sf package is primarily used) is where the median rainfall for each weather station in January is established from the data. The weather station data contains records of rainfall for all the months of the year for many years. This means, that to create this map the necessary data needed to be isolated from the rest of the data to allow only the necessary data to be plotted. It first isolates the rainfall data, then filters to only include the data for January, and then it commands to calculate the median rainfall by each weather station.
Finally, the last section of code (where the tmap package is primarily used) constitutes how the map is plotted. It calls on the isolated data to be drawn, along with the data containing the counties, which shows the entire island of Ireland as context for the weather stations. This is where the layout of the map is decided. The weather stations were chosen to be represented by dots and colour-coded by the median rainfall for that station. The blue colour was chosen to correspond to the subject matter which was rainfall and the size of the dots was chosen as well.
The style determined how the data was broken up into the sections of the colour ramp. A continuous style meant the colour gradient increased by a smooth continuous amount. This style can smooth over differences in the data and lessen extremes between groups of values as it chooses a gradual increase in its grouping of values. Finally, the title was given to the map.
The results can be seen in the final map. The highest median rainfall for January occurs in the north-west in Donegal and the south-west in Cork, as well as the southern coast. This result aligns with the concept that the west of Ireland gets the most rain as weather systems come in over the Atlantic Ocean and the mountains in the west cause relief rainfall to fall on the west side of the mountains. The midlands therefore remain more protected as a result of this relief rainfall emptying the majority of rain from the clouds in the west of Ireland. This is also prevalent in the map as the midland areas have the smallest median rainfall in January.
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
rainfall <- st_read('weather_stations.geojson')
## Reading layer `weather_stations' from data source
## `C:\R Materials\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
rainfall
## 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
## First 10 features:
## 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
## 7 1856 Jan 151.6 Ardara 15 180787.7 394679 Donegal AR
## 8 1857 Jan 179.1 Ardara 15 180787.7 394679 Donegal AR
## 9 1858 Jan 110.1 Ardara 15 180787.7 394679 Donegal AR
## 10 1859 Jan 157.8 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)
## 7 Briffa 17.36709 POINT (181346.8 393612.6)
## 8 Briffa 17.36709 POINT (181346.8 393612.6)
## 9 Briffa 17.36709 POINT (181346.8 393612.6)
## 10 Briffa 17.36709 POINT (181346.8 393612.6)
counties <- st_read('counties.geojson')
## Reading layer `counties' from data source `C:\R Materials\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
january_data <-rainfall %>% filter(Month == "Jan")
median_rainfall<-january_data%>%group_by(Station)%>%summarise(median_rainfall=median(Rainfall))
View(median_rainfall)
tm_shape(counties) +tm_borders() +tm_shape(median_rainfall,legend.title = "Rainfall (mm)") +tm_dots(col = "median_rainfall", size = 0.7, style = "cont", palette = "Blues") +tm_layout(title = "Median Rainfall in January")
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.