## Introduction
#Have you ever wondered how much it rains in different parts of Ireland during January? Rainfall is a fascinating aspect of weather that affects our daily lives, from planning a trip to managing our gardens. In this blog, we’re going to take a closer look at the median rainfall levels recorded in January across 25 weather stations in Ireland. By creating a colorful map, we'll be able to see where it’s wettest and where it’s relatively dry.
#We’ll start with some basic data about rainfall and the locations of these weather stations. Then, we’ll dive into the code used to create our map and visualize the data in a way that’s easy to understand and interpret. Along the way, we’ll discuss any interesting patterns that emerge, such as regions that consistently experience higher rainfall.
#Whether you’re a weather enthusiast, a data visualization geek, or just curious about Ireland’s climate, this blog is for you. Let’s get started on this rainy adventure together!
## Data Preparation
#Before diving into the analysis and visualization, it's crucial to prepare the data properly. In this section, we'll walk through the steps taken to clean, organize, and transform the data for our analysis of median rainfall in Ireland during January.
### Data Collection
#The data used in this analysis includes rainfall measurements from 25 weather stations across Ireland. Each station records the amount of rainfall in millimeters (mm) for each month. The data includes the following columns:
#- **Station**: The identifier for each weather station.
#- **Month**: The month of the year when the rainfall was recorded.
#- **Rainfall**: The amount of rainfall in millimeters (mm).
#- **Geometry**: The geographic coordinates (longitude and latitude) of each weather station.
### Data Cleaning
#To ensure the data is accurate and ready for analysis, we performed the following cleaning steps:
#1. **Remove Missing Values**: Checked for and removed any missing or null values in the dataset.
#2. **Convert Data Types**: Ensured that all columns have the correct data types (e.g., numeric for rainfall, character for station names).
#3. **Filter Data**: Filtered the data to include only the measurements for January.
### Data Transformation
#Next, we transformed the data to create a spatial data frame and prepare it for visualization:
#1. **Create Spatial Data Frame**: Converted the data into a spatial data frame using the `sf` package in R.
#2. **Transform Coordinates**: Transformed the geographic coordinates to a suitable projection (EPSG:29902) for accurate spatial analysis.
#3. **Voronoi Tessellation**: Created Voronoi polygons around each weather station to visualize the areas influenced by each station.
#4. **Intersect with Coastline**: Intersected the Voronoi polygons with the coastline of Ireland to ensure the polygons are clipped to the country's boundaries.
#Here’s the R code used for data preparation:
# Load necessary libraries
library(sf)
## Warning: package 'sf' was built under R version 4.4.2
## Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.2
##
## 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(tmap)
## Warning: package 'tmap' was built under R version 4.4.2
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(rnaturalearth)
## Warning: package 'rnaturalearth' was built under R version 4.4.2
library(rnaturalearthdata)
## Warning: package 'rnaturalearthdata' was built under R version 4.4.2
##
## Attaching package: 'rnaturalearthdata'
## The following object is masked from 'package:rnaturalearth':
##
## countries110
# Sample data for 25 weather stations
stations <- data.frame(
Station = 1:25,
Long = c(-6.3, -6.2, -6.1, -6.0, -5.9, -5.8, -5.7, -5.6, -5.5, -5.4,
-5.3, -5.2, -5.1, -5.0, -4.9, -4.8, -4.7, -4.6, -4.5, -4.4,
-4.3, -4.2, -4.1, -4.0, -3.9),
Lat = c(53.3, 53.4, 53.5, 53.6, 53.7, 53.8, 53.9, 54.0, 54.1, 54.2,
54.3, 54.4, 54.5, 54.6, 54.7, 54.8, 54.9, 55.0, 55.1, 55.2,
55.3, 55.4, 55.5, 55.6, 55.7),
Rainfall = c(165.1, 91.6, 75.3, 75.0, 129.7, 87.1, 104.8, 92.4, 54.7, 115.9,
108.1, 177.3, 65.2, 119.1, 115.2, 71.1, 133.3, 160.6, 159.8, 185.4,
134.3, 130.5, 103.9, 146.5, 124.0)
)
# Convert to spatial data frame
stations_sf <- st_as_sf(stations, coords = c('Long', 'Lat'), crs = 4326)
# Transform coordinates to a suitable projection (e.g., EPSG:29902)
stations_sf <- st_transform(stations_sf, crs = 29902)
# Create the Voronoi tessellation
vor <- stations_sf %>%
st_union() %>%
st_voronoi() %>%
st_collection_extract() %>%
st_sf()
# Convert Voronoi polygons to the same CRS as the stations
vor <- st_transform(vor, crs = st_crs(stations_sf))
# Join back the rainfall data
vor_rain <- st_join(vor, stations_sf)
# Load the Ireland coastline data using rnaturalearth
coast <- ne_countries(scale = "medium", returnclass = "sf", country = "Ireland")
# Transform the coastline data to the same projection
coast <- st_transform(coast, crs = 29902)
# Intersect the Voronoi polygons with the coastline
vor_rain <- st_intersection(vor_rain, coast)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
# Set tmap mode to interactive view
tmap_mode("view")
## tmap mode set to interactive viewing
# Plot the map
tm_shape(vor_rain) +
tm_borders() +
tm_shape(stations_sf) +
tm_dots(size = 0.3) +
tm_shape(vor_rain) +
tm_polygons(col = 'Rainfall', palette = "Blues", title = "Median Rainfall (mm)") +
tm_layout(title = "Median Rainfall in Ireland (January)",
title.size = 1.5,
legend.title.size = 1.2,
legend.text.size = 0.8)