-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivesite_depths.R
59 lines (48 loc) · 2.24 KB
/
divesite_depths.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
#' Depth variation per divesite
#'
#' Creates a plot with the variation of depths of the logged dives
#'
#' This function creates a boxplot to visualize the depth variation of the logged dives, offering an overview of the depth conditions
#' and opportunities of each divesite
#'
#' @param toofew number of logged dives per divesite under which a divesite is excluded
#' from the boxplot for having too few dives for an appropriate interpretation of the depth variation
#' @param path directory location where the files will be read from or written into
#'
#' @author Ruben Perez Perez
#'
#' @import ggplot2
#' @import dplyr
#' @import sf
#'
#' @return Returns a boxplot.
#'
#' @export
#.........................................................
divesite_depths <- function (toofew = 1, path = getwd()){
if(dir.exists(paste0(path, "/divewatchr_data"))){
load('divewatchr_data/scuba_map.RData')
}
# Drop locations where there is only 1 dive since the point of this plot is showing variation
y <- scuba_map %>% filter (locationID %in% (scuba_map %>%
group_by(locationID) %>%
summarise(n = n()) %>%
st_drop_geometry() %>%
filter (n > toofew) %>%
select (locationID))$locationID) %>%
arrange (rowid)
# Plot location against depth
ggplot(y %>% filter(!is.na(maximumDepthInMeters)),
aes(x = reorder(as.factor(locationID), as.numeric(maximumDepthInMeters), FUN = median), # replace locationID by locality to see it by locality
y = -as.numeric(maximumDepthInMeters),
fill = paste0(locality, ", ", country))) +
# reorder(Species, Sepal.Width, FUN = median)
ggtitle("Divesite depths") +
labs(x = "locationID",
y = "Depth",
fill = "Locality") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_boxplot () # +
# facet_wrap(~country) # Interesting but not yet very useful, need to polish it
# intentar agrupar las boxes por locality , check https://stackoverflow.com/questions/43877663/order-multiple-variables-in-ggplot2
}