## Analysing and Visualizing Water Quality data using R-programming
## Coded by - Debarghya De (203001170001)
## Branch - B.Tech Agriculture_6th Semester
# Load necessary packages
library(tidyverse) # for data manipulation and plotting
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.1.8
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(lubridate) # for handling dates and times
library(leaflet) # for creating interactive maps
## Warning: package 'leaflet' was built under R version 4.2.3
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.2.3
##
## Attaching package: 'reshape2'
##
## The following object is masked from 'package:tidyr':
##
## smiths
library(ggplot2)
# Load and pre-process water quality data from multiple stations
water_data <- read.csv("water_quality_data.csv")
water_data$date <- ymd(water_data$date) # convert date column to proper date format
## Warning: All formats failed to parse. No formats found.
# Calculate descriptive statistics of the water quality parameters
water_stats <- water_data %>%
summarize(mean_pH = mean(pH),
mean_DO = mean(dissolved_oxygen),
mean_turbidity = mean(turbidity),
sd_pH = sd(pH),
sd_DO = sd(dissolved_oxygen),
sd_turbidity = sd(turbidity))
# Plot barplots of the water quality parameters by station and by month
ggplot(water_data, aes(x = station, y = pH)) +
geom_bar(stat = "summary", fun = "mean") +
labs(x = "Station", y = "pH") +
ggtitle("Mean pH by Station") +
theme_bw()

ggplot(water_data, aes(x = station, y = dissolved_oxygen)) +
geom_bar(stat = "summary", fun = "mean") +
labs(x = "Station", y = "Dissolved Oxygen") +
ggtitle("Mean Dissolved Oxygen by Station") +
theme_bw()

ggplot(water_data, aes(x = station, y = turbidity)) +
geom_bar(stat = "summary", fun = "mean") +
labs(x = "Station", y = "Turbidity") +
ggtitle("Mean Turbidity by Station") +
theme_bw()

# Perform correlation analysis to identify relationships between the water quality parameters
cor_data <- water_data %>%
select(pH, dissolved_oxygen, turbidity) %>%
cor()
# Create a heatmap of the correlations
ggplot(data = melt(cor_data), aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low = "blue", mid = "white", high = "red", midpoint = 0) +
labs(title = "Correlation Heatmap of Water Quality Parameters") +
xlab("") +
ylab("") +
theme_bw()

# Create a map of the study area and plot the spatial distribution of the water quality parameters
water_map <- leaflet(water_data) %>%
addProviderTiles("CartoDB.Positron") %>%
addCircleMarkers(lng = ~long, lat = ~lat, weight = 1, radius = 100,
color = ~turbidity, opacity = 0.7, fillOpacity = 0.7,
label = ~paste("Station:", station, "<br>", "Turbidity:", turbidity)) %>%
addLegend(pal = colorNumeric(palette = "YlOrRd", domain = water_data$turbidity),
values = water_data$turbidity,
title = "Turbidity",
opacity = 1)
# Show map
water_map