-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.Rhistory
More file actions
430 lines (430 loc) · 13.6 KB
/
.Rhistory
File metadata and controls
430 lines (430 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
devtools::install_github("openwashdata/washr")
clear
remove.packages("washr")
library(washr)
devtools::install_github("openwashdata/washr")
library(washr)
setup_rawdata()
# Description ------------------------------------------------------------------
# R script to process uploaded raw data into a tidy, analysis-ready data frame
# Load packages ----------------------------------------------------------------
## Run the following code in console if you don't have the packages
## install.packages(c("usethis", "fs", "here", "readr", "readxl", "openxlsx"))
library(usethis)
library(fs)
library(here)
library(readr)
library(dplyr)
library(readxl)
library(openxlsx)
library(lubridate)
library(ggplot2)
library(maps)
# Load Data --------------------------------------------------------------------
# Load the necessary data from a CSV file
data_in <- readr::read_csv("data-raw/borrehole lab chemistry.csv")
# (Optional) Read and clean the codebook if needed (commented out for now)
# codebook <- readxl::read_excel("data-raw/codebook.xlsx") %>%
# clean_names()
# Tidy data --------------------------------------------------------------------
# Remove rows where the 'latitude' column contains NULL (NA) values
data_in <- data_in %>%
filter(!is.na(latitude))
# Function to check for non-UTF-8 characters in character columns
check_utf8 <- function(df) {
# Identify columns with invalid UTF-8 characters
invalid_cols <- sapply(df, function(column) {
if (!is.character(column)) return(FALSE) # Skip non-character columns
any(sapply(column, function(x) {
if (is.na(x)) return(FALSE) # Ignore NA values
!identical(iconv(x, from = "UTF-8", to = "UTF-8"), x)
}))
})
# Extract the column names with invalid characters
bad_cols <- names(df)[invalid_cols]
# Output a message depending on whether non-UTF-8 characters were found
if (length(bad_cols) > 0) {
message("Non-UTF-8 characters detected in columns: ",
paste(bad_cols, collapse = ", "))
} else {
message("No non-UTF-8 characters found.")
}
}
# Convert character columns from Latin1 encoding to UTF-8, removing problematic
# characters
data_in[] <- lapply(data_in, function(x) {
if (is.character(x)) {
# Convert to UTF-8 and remove problematic characters
iconv(x, from = "latin1", to = "UTF-8", sub = "")
} else {
x
}
})
# Re-check the data for non-UTF-8 characters after the conversion
check_utf8(data_in)
boreholelabdata <- data_in
# Export Data ------------------------------------------------------------------
usethis::use_data(boreholelabdata, overwrite = TRUE)
fs::dir_create(here::here("inst", "extdata"))
readr::write_csv(boreholelabdata,
here::here("inst", "extdata", paste0("boreholelabdata", ".csv")))
openxlsx::write.xlsx(boreholelabdata,
here::here("inst", "extdata", paste0("boreholelabdata",
".xlsx")))
setup_dictionary()
library(washr)
setup_dictionary()
setup_roxygen()
devtools::document()
devtools::check()
devtools::install()
use_author(
given = "Emmanuel",
family = "Mhango",
role = c("aut", "cre"),
email = "emmanuellmhango@gmail.com",
comment = c(ORCID = "0000-0003-3197-6244")
)
use_author(given = "Finlay", family = "Paterson", role = c("aut","ctb"))
use_author(given = "Jamie", family = "Rattray", role = c("aut","ctb"))
update_description()
devtools::document()
devtools::check()
devtools::install()
add_metadata()
update_metadata()
generate_jsonld()
devtools::document()
devtools::check()
devtools::install()
setup_readme()
#========================================
# Load required libraries
library(tidyverse)
library(lubridate)
# Use your existing dataset
df <- boreholelabdata
# Convert sample collection date to Date format
df$date_sample_collected <- as.Date(df$date_sample_collected)
# Extract the month for grouping (rounded to first day of month)
df$month <- floor_date(df$date_sample_collected, "month")
# Reshape to long format for selected parameters
df_long <- df %>%
select(month,
tds_mg_l,
fluoride_mg_l,
nitrate_mg_l) %>%
pivot_longer(cols = -month, names_to = "parameter", values_to = "value") %>%
drop_na()
# ---- Boxplot by Month ----
ggplot(df_long, aes(x = month, y = value, fill = parameter)) +
geom_boxplot(outlier.color = "red", alpha = 0.6) +
facet_wrap(~ parameter, scales = "free_y") +
labs(title = "Distribution of Water Quality Parameters Over Time",
x = "Month",
y = "Concentration (mg/L)",
fill = "Parameter") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# ---- Line Plot of Monthly Median Trends ----
df_median <- df_long %>%
group_by(month, parameter) %>%
summarize(median_value = median(value, na.rm = TRUE), .groups = "drop")
ggplot(df_median, aes(x = month, y = median_value, color = parameter)) +
geom_line(size = 1.2) +
geom_point(size = 2) +
labs(title = "Median Trends of Water Quality Parameters",
x = "Month",
y = "Median Concentration (mg/L)",
color = "Parameter") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# ---- Boxplot by Month ----
ggplot(df_long, aes(x = month, y = value, fill = parameter)) +
geom_boxplot(outlier.color = "red", alpha = 0.6) +
facet_wrap(~ parameter, scales = "free_y") +
labs(title = "Distribution of Water Quality Parameters Over Time",
x = "Month",
y = "Concentration (mg/L)",
fill = "Parameter") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
install.packages("leaflet")
#========================================
# Load libraries
library(leaflet)
library(dplyr)
# Use your dataset
df <- boreholelabdata
# Filter rows that have coordinates and fluoride status
df_map <- df %>%
filter(!is.na(latitude) & !is.na(longitude) & !is.na(fluoride_within_standards)) %>%
mutate(status_color = case_when(
fluoride_within_standards == "Yes" ~ "green",
fluoride_within_standards == "No" ~ "red",
TRUE ~ "gray"
))
# Create popup label
df_map$popup <- paste0(
"<b>Waterpoint:</b> ", df_map$waterpoint_name, "<br>",
"<b>Fluoride (mg/L):</b> ", df_map$fluoride_mg_l, "<br>",
"<b>Within Standards:</b> ", df_map$fluoride_within_standards
)
# Generate the interactive map
leaflet(df_map) %>%
addTiles() %>%
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 6,
color = ~status_color,
stroke = TRUE,
fillOpacity = 0.8,
popup = ~popup
) %>%
addLegend(
position = "bottomright",
colors = c("green", "red", "gray"),
labels = c("Within Standards", "Outside Standards", "Unknown"),
title = "Fluoride Quality"
)
#========================================
# Load required libraries
# Load required libraries
library(leaflet)
library(dplyr)
# Use your data
df <- boreholelabdata
# Filter for points with coordinates and fluoride compliance data
df_map <- df %>%
filter(!is.na(latitude), !is.na(longitude), !is.na(fluoride_within_standards)) %>%
mutate(status_color = case_when(
fluoride_within_standards == "Yes" ~ "green",
fluoride_within_standards == "No" ~ "red",
TRUE ~ "gray"
))
# Create popup label
df_map$popup <- paste0(
"<b>Waterpoint:</b> ", df_map$waterpoint_name, "<br>",
"<b>Fluoride (mg/L):</b> ", df_map$fluoride_mg_l, "<br>",
"<b>Within Standards?:</b> ", df_map$fluoride_within_standards
)
# Create the interactive map
leaflet(df_map) %>%
addTiles() %>%
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 6,
color = ~status_color,
stroke = TRUE,
fillOpacity = 0.8,
popup = ~popup
) %>%
addLegend(
position = "bottomright",
colors = c("green", "red", "gray"),
labels = c("Within Standards", "Outside Standards", "Unknown"),
title = "Fluoride Compliance"
)
#========================================
# Load required libraries
library(map) # Your custom or wrapper map library
library(maps)
#========================================
# Load required libraries
library(maps) # Your custom or wrapper map library
library(leaflet)
library(dplyr)
# Use your data frame
df <- boreholelabdata
# Filter valid coordinates and fluoride status
df_map <- df %>%
filter(!is.na(latitude), !is.na(longitude), !is.na(fluoride_within_standards)) %>%
mutate(status_color = case_when(
fluoride_within_standards == "Yes" ~ "green",
fluoride_within_standards == "No" ~ "red",
TRUE ~ "gray"
))
# Create popup label
df_map$popup <- paste0(
"<b>Waterpoint:</b> ", df_map$waterpoint_name, "<br>",
"<b>Fluoride (mg/L):</b> ", df_map$fluoride_mg_l, "<br>",
"<b>Within Standards?:</b> ", df_map$fluoride_within_standards
)
# Generate the interactive Leaflet map, focused on Malawi
leaflet(df_map) %>%
addTiles() %>%
setView(lng = 34.3, lat = -13.5, zoom = 6.5) %>% # Center on Malawi
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 6,
color = ~status_color,
stroke = TRUE,
fillOpacity = 0.8,
popup = ~popup
) %>%
addLegend(
position = "bottomright",
colors = c("green", "red", "gray"),
labels = c("Within Standards", "Outside Standards", "Unknown"),
title = "Fluoride Compliance"
)
#========================================
# Load required libraries
library(dplyr)
library(tidyr)
library(ggplot2)
# Use your dataset
df <- boreholelabdata
# Select relevant compliance columns and reshape to long format
compliance_long <- df %>%
select(fluoride_within_standards,
nitrate_within_standards,
ph_within_mw_standards) %>%
pivot_longer(cols = everything(),
names_to = "parameter",
values_to = "compliance") %>%
filter(!is.na(compliance)) %>%
mutate(parameter = case_when(
parameter == "fluoride_within_standards" ~ "Fluoride",
parameter == "nitrate_within_standards" ~ "Nitrate",
parameter == "ph_within_mw_standards" ~ "pH",
TRUE ~ parameter
))
# Count compliance status by parameter
compliance_summary <- compliance_long %>%
group_by(parameter, compliance) %>%
summarise(count = n(), .groups = "drop")
# Plot stacked bar chart
ggplot(compliance_summary, aes(x = parameter, y = count, fill = compliance)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("Yes" = "green", "No" = "red")) +
labs(
title = "Water Quality Compliance Summary",
x = "Parameter",
y = "Number of Samples",
fill = "Within Standards"
) +
theme_minimal()
#========================================
# Load required libraries
library(dplyr)
library(tidyr)
library(ggplot2)
# Select relevant compliance columns and reshape to long format
compliance_long <- boreholelabdata %>%
select(fluoride_within_standards,
nitrate_within_standards,
ph_within_mw_standards) %>%
pivot_longer(cols = everything(),
names_to = "parameter",
values_to = "compliance") %>%
filter(!is.na(compliance)) %>%
mutate(parameter = case_when(
parameter == "fluoride_within_standards" ~ "Fluoride",
parameter == "nitrate_within_standards" ~ "Nitrate",
parameter == "ph_within_mw_standards" ~ "pH",
TRUE ~ parameter
))
# Count compliance status by parameter
compliance_summary <- compliance_long %>%
group_by(parameter, compliance) %>%
summarise(count = n(), .groups = "drop")
# Plot stacked bar chart
ggplot(compliance_summary, aes(x = parameter, y = count, fill = compliance)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("Yes" = "green", "No" = "red")) +
labs(
title = "Water Quality Compliance Summary",
x = "Parameter",
y = "Number of Samples",
fill = "Within Standards"
) +
theme_minimal()
devtools::build_readme()
devtools::build_readme()
setup_website()
library(washr)
setup_website()
devtools::document()
devtools::check()
devtools::install()
# Description ------------------------------------------------------------------
# R script to process uploaded raw data into a tidy, analysis-ready data frame
# Load packages ----------------------------------------------------------------
## Run the following code in console if you don't have the packages
## install.packages(c("usethis", "fs", "here", "readr", "readxl", "openxlsx"))
library(usethis)
library(fs)
library(here)
library(readr)
library(dplyr)
library(readxl)
library(openxlsx)
library(lubridate)
library(ggplot2)
library(maps)
# Load Data --------------------------------------------------------------------
# Load the necessary data from a CSV file
data_in <- readr::read_csv("data-raw/borrehole lab chemistry.csv")
# (Optional) Read and clean the codebook if needed (commented out for now)
# codebook <- readxl::read_excel("data-raw/codebook.xlsx") %>%
# clean_names()
# Tidy data --------------------------------------------------------------------
# Remove rows where the 'latitude' column contains NULL (NA) values
data_in <- data_in %>%
filter(!is.na(latitude))
# Function to check for non-UTF-8 characters in character columns
check_utf8 <- function(df) {
# Identify columns with invalid UTF-8 characters
invalid_cols <- sapply(df, function(column) {
if (!is.character(column)) return(FALSE) # Skip non-character columns
any(sapply(column, function(x) {
if (is.na(x)) return(FALSE) # Ignore NA values
!identical(iconv(x, from = "UTF-8", to = "UTF-8"), x)
}))
})
# Extract the column names with invalid characters
bad_cols <- names(df)[invalid_cols]
# Output a message depending on whether non-UTF-8 characters were found
if (length(bad_cols) > 0) {
message("Non-UTF-8 characters detected in columns: ",
paste(bad_cols, collapse = ", "))
} else {
message("No non-UTF-8 characters found.")
}
}
# Convert character columns from Latin1 encoding to UTF-8, removing problematic
# characters
data_in[] <- lapply(data_in, function(x) {
if (is.character(x)) {
# Convert to UTF-8 and remove problematic characters
iconv(x, from = "latin1", to = "UTF-8", sub = "")
} else {
x
}
})
# Re-check the data for non-UTF-8 characters after the conversion
check_utf8(data_in)
boreholelabdata <- data_in
# Export Data ------------------------------------------------------------------
usethis::use_data(boreholelabdata, overwrite = TRUE)
fs::dir_create(here::here("inst", "extdata"))
readr::write_csv(boreholelabdata,
here::here("inst", "extdata", paste0("boreholelabdata", ".csv")))
openxlsx::write.xlsx(boreholelabdata,
here::here("inst", "extdata", paste0("boreholelabdata",
".xlsx")))
library(washr)
devtools::build_readme()
devtools::build_readme()
devtools::document()
devtools::check()
devtools::install()
setup_website()
setup_website()
devtools::document()
devtools::check()
devtools::install()