#required libraries
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
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.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(RColorBrewer)
library(sp)
library(leaflet)
The dataset contains rainfall data from 25 weather stations across
Ireland, including columns such as Year,
Month, Rainfall, Station,
Elevation, Easting, Northing,
County, Abbreviation, Source,
coast_dist, and geometry. It is used to
analyze and map the median rainfall levels for January, highlighting
variations in rainfall across the country.
ws= st_read("C:/Users/saiye/Downloads/weather_stations.geojson",quiet=TRUE)
File used in this analysis: weather_stations.geojson
Data Cleaning:
To ensure the dataset is accurate and ready for analysis, it is essential to remove any rows containing missing values. Missing data can lead to errors or biases in the analysis, and removing these rows ensures consistency and reliability in the results.
gj <- na.omit(ws)
#Filter January data
jan_data <- gj %>% filter(Month == "Jan")
# Compute median rainfall for each station
median_rainfall <- jan_data %>%
group_by(Station) %>%
summarize(median_rainfall = median(Rainfall, na.rm = TRUE)) %>%
ungroup()
# Join median rainfall back to geometry
gj <- gj %>%
st_join(median_rainfall, join = st_intersects)
station_means <- gj %>% select(Station.x, median_rainfall, geometry)
# Create interactive map
tmap_mode("view")
## tmap mode set to interactive viewing
1. Filter January Data:
jan_data <- gj %>% filter(Month == "Jan") Filters the
dataset gj to include only the rows where the month is January,
isolating the relevant data for analysis.
2. Compute Median Rainfall for Each Station: Groups
the January data by each station and calculates the median rainfall for
each station. The resulting dataset, median_rainfall,
contains the station names and their corresponding median rainfall
levels.
3. Join Median Rainfall with Geometry: Combines the
median_rainfall data with the spatial geometry of the weather stations
from gj. This step associates the median rainfall data with
the spatial locations for mapping.
4. Create a Simplified Dataset for Mapping: Selects
the relevant columns (Station.x,
median_rainfall, and geometry) from the joined
dataset to create a simplified dataset, station_means,
specifically for mapping.
5. Set Map Mode to Interactive:
tmap_mode("view") Switches the tmap package into
interactive mode, enabling the creation of an interactive map where
users can explore the data visually.
tm_start <- tm_shape(station_means) +
tm_dots(
col = "median_rainfall",
palette = "viridis", # a colorblind-friendly palette
size = 0.3,
popup.vars = c("Station Name" = "Station.x", "Median Rainfall (mm)" = "median_rainfall"),
title = "Rainfall Intensity (Median)"
) +
tm_layout(
title = "Rainfall Distribution in January across Ireland",
legend.outside = TRUE,
legend.outside.position = "right",
frame = TRUE, #border around the map for aesthetics
title.size = 1.5
)
# Convert to interactive leaflet map
interactive_map <- tmap_leaflet(tm_start) %>%
addScaleBar(position = "bottomright") %>% #scale bar
addMiniMap(zoomLevelOffset = -3, toggleDisplay = TRUE, minimized = FALSE)
# View the map
interactive_map