This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
###set working directory where the data will be taken from and saved from
setwd('C:/Users/27736/Desktop/R data/Class Materials-20241230')
###load library (sf) to read simple feauture ### load and read the data
load ('Rainfall.Rdata')
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
station <- read_sf ('weather_stations.geojson')
###check the structure of the data
str (station)
## sf [49,500 Ă— 12] (S3: sf/tbl_df/tbl/data.frame)
## $ Year : num [1:49500] 1850 1851 1852 1853 1854 ...
## $ Month : chr [1:49500] "Jan" "Jan" "Jan" "Jan" ...
## $ Rainfall : num [1:49500] 169 236 250 209 188 ...
## $ Station : chr [1:49500] "Ardara" "Ardara" "Ardara" "Ardara" ...
## $ Elevation : int [1:49500] 15 15 15 15 15 15 15 15 15 15 ...
## $ Easting : num [1:49500] 180788 180788 180788 180788 180788 ...
## $ Northing : num [1:49500] 394679 394679 394679 394679 394679 ...
## $ County : chr [1:49500] "Donegal" "Donegal" "Donegal" "Donegal" ...
## $ Abbreviation: chr [1:49500] "AR" "AR" "AR" "AR" ...
## $ Source : chr [1:49500] "Briffa" "Briffa" "Briffa" "Briffa" ...
## $ coast_dist : num [1:49500] 17.4 17.4 17.4 17.4 17.4 ...
## $ geometry :sfc_POINT of length 49500; first list element: 'XY' num [1:2] 181347 393613
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
## ..- attr(*, "names")= chr [1:11] "Year" "Month" "Rainfall" "Station" ...
library (dplyr) #####this is done in order to compute the median by grouping
## 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 (tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.2
## Warning: package 'ggplot2' was built under R version 4.4.2
## Warning: package 'readr' was built under R version 4.4.2
## Warning: package 'lubridate' was built under R version 4.4.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.5.1 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── 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
###Calculating median for the 25 station. library (dplyr) library (tidyverse)
Rain_med <- rain %>% filter(Month == "Jan" & Year >= 1850 & Year <= 2014) %>% group_by(Station) %>% summarise(medrainfall = median(Rainfall, na.rm = TRUE))
###visual representation to se how the median distributions differs from station to station from january 1850 to 2014.
barplot(Rain_med$medrainfall,names= Rain_med$Station, las= 3, col = "blue")
###the next step is to join median rainfall obtained to station in order to get coordinates for mapping purpose.
station %>% left_join(Rain_med)-> station_median
## Joining with `by = join_by(Station)`
###the next step will be to represent the different by first exporting the map of ireland from and inserting its coordinate for the map to be drawn on R
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(sf)
### Convert to sf object
stations_sf <- station_median %>%
st_as_sf(coords = c('Long', 'Lat'), crs = 4326)
### Set tmap to view mode
tmap_mode('view')
## tmap mode set to interactive viewing
### Create the map
tm_shape(stations_sf) +
tm_bubbles(size = "medrainfall",
col = "medrainfall",
palette = "viridis",
title.size = "Median Rainfall",
title.col = "Median Rainfall") +
tm_layout(title = "Median Rainfall by Station",
legend.outside = TRUE)
## Legend for symbol sizes not available in view mode.