-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTeam_Lemur_Script.R
61 lines (51 loc) · 2.24 KB
/
Team_Lemur_Script.R
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
library("magrittr")
library("ggplot2")
library("dplyr")
library("stringr")
library("leaflet")
## read in data
data_path <- "hackathon_20180621.xlsx"
sheet_names <- readxl::excel_sheets(data_path) # get sheet names
sheets <- purrr::map(sheet_names, ~ readxl::read_excel(path = data_path, sheet = .x)) # read in all sheets
sheets <- setNames(sheets, sheet_names) # set the names of the sheets
## clean data
mega_data <- dplyr::bind_rows(sheets$Chemistry, sheets$Bacteria, sheets$Nutrients) %>%
mutate(Parameter = tolower(ParameterCode)) %>% # clean parameter code names
mutate(Parameter = gsub("e. coil", "e. coli", Parameter, fixed = TRUE)) %>% # clean parameter code typo
filter(!(Parameter %in% c("duplicate", "do % sat", "temp") | is.na(Parameter))) # remove NA and duplicate parameter codes
## create regulation data
reg <- data.frame(Parameter = unique(mega_data$Parameter),
threshold = c(" < 3000", " > 5", " < 8.5", " < 25", " < 235", " < 10000", " < 61"))
## find exceedence instances
mega_data <- left_join(mega_data, reg) %>%
rowwise() %>%
mutate(Exceed = eval(parse(text = paste0(Result, threshold))))
## get the percent exceedence
mega_data_sum <- mega_data %>%
group_by(ParameterCode, StationID) %>%
summarise(P_Exceed = sum(Exceed, na.rm = TRUE),
Count = length(Exceed),
Percent_Exceed = P_Exceed/Count * 100)
## prepare data for mapping
mega_data_map <- mega_data_sum %>%
left_join(sheets$SiteList %>%
rename(StationID = `Site Code`,
Coord = `Geographic Coordinates (Decimal Degrees)`) %>%
select(StationID, Coord)) %>%
rowwise() %>%
mutate(long = unlist(str_split(Coord, pattern = ","))[1],
lat = unlist(str_split(Coord, pattern = ","))[2])
## Create a continuous palette function
pal <- colorNumeric(
palette = "Reds",
domain = mega_data_map$Percent_Exceed)
## make a map
m <- leaflet(mega_data_map %>%
filter(ParameterCode == "Conductivity")) %>% # select parameter to map
addTiles() %>% # Add default OpenStreetMap map tiles
addCircleMarkers(lng= ~as.numeric(long),
lat= ~as.numeric(lat),
fillColor = ~pal(Percent_Exceed),
radius = 10,
stroke = FALSE)
m # Print the map