Skip to content

Commit

Permalink
version avant dernière révision
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentBellavance committed Mar 16, 2023
1 parent bfa620f commit 29ddf2d
Show file tree
Hide file tree
Showing 221 changed files with 4,433 additions and 26,632 deletions.
55 changes: 55 additions & 0 deletions R/correlation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' Title
#'
#' Description
#'
#' @param
#'
#' @return

env_occ_corr <- function(species) {

# Species occurrences
sp_occ <- terra::vect(sprintf("data/data-biotic/%s.gpkg", species))

# Import all environmental variables
env_files <- list.files("data/data-abiotic/")
env <- lapply(env_files, function(x) {
terra::rast(sprintf("data/data-abiotic/%s", x))
})
env <- terra::rast(env)

# Extract values of nv for every points
vals_extract <- terra::extract(env,
sp_occ,
ID = FALSE)

# Bind values extracted with presence/absence data
dat <- cbind(sp_occ[,"presence"], vals_extract)
dat <- dat[,-c(16)] # Temporary

# Calculate correlation between presence and every environmental variable
correlations <- sapply(names(dat[,2:ncol(dat)]), function(x) {
tmp <- as.data.frame(dat[,c("presence",x)])
tmp <- tmp[complete.cases(tmp),]
cor(tmp[,1],
tmp[,2],
method = "pearson")
})

return(sort(correlations))
}

dat <- as.data.frame(dat)
dat <- dat[complete.cases(dat),]
coco <- prcomp(dat[,-1], scale = TRUE)
g <- ggbiplot(coco,
obs.scale = 1,
var.scale = 1,
groups = dat[,"presence"],
ellipse = TRUE,
circle = TRUE,
ellipse.prob = 0.68)

env <- rownames(coco$rotation[coco$rotation[,1]*coco$rotation[,2] < 0,])
vif(dat[,env])
env <- env[-which(env == "")];vif(dat[,env])
49 changes: 16 additions & 33 deletions R/fig_abiotic.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,20 @@
# Get global parameters
global_param()

env <- c("arag",
"bathy",
"slope",
"sat",
"bottom_chlorophyll_mean",
"bottom_current_velocity_max",
"bottom_dissolved_oxygen_mean",
"bottom_dissolved_oxygen_range",
"bottom_iron_mean",
"bottom_nitrate_mean",
"bottom_phosphate_mean",
"bottom_phytoplankton_mean",
"bottom_salinity_mean",
"bottom_salinity_range",
"bottom_silicate_mean",
"bottom_temperature_mean",
"bottom_temperature_range",
"mean_salinity",
"mean_temperature",
"sat")

env <- list.files("data/data-abiotic/", pattern = ".tif$") |>
gsub(pattern = ".tif",
replacement = "",
x = _)

#---------- Plot every abiotic data ----------#

lapply(env, function(x) {

# Species name with first capital letter and space instead of underscore
if(x %in% c("mean_salinity", "mean_temperature")) {
dat <- terra::rast(sapply(2011:2020, function(i) {
terra::rast(paste0("data/data-abiotic/",x,"_", i, ".tif"))
})) |>
terra::app(x = _, fun = mean, na.rm = TRUE)
} else {
dat <- terra::rast(paste0("data/data-abiotic/",x,".tif"))
}
# Load data
dat <- terra::rast(sprintf("data/data-abiotic/%s.tif", x))

# Define filename of the figures
filename <- paste0("figures/env_", x, ".png")
filename <- sprintf("figures/env_%s.png", x)

# Save figure in .png
png(filename,
Expand All @@ -68,8 +44,15 @@

# Plot data
terra::plot(dat,
add = TRUE)

add = TRUE,
col = "#6699CC")
legend("bottomright",
"Zones de concentration significative\nde plumes de mer",
col = "#6699CC",
bty = "n",
pch = 16,
cex = 0.8,
pt.cex = 1.3)
plot(sf::st_geometry(aoi),
add = TRUE)
# Cities
Expand Down
2 changes: 0 additions & 2 deletions R/get_basemap.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ get_basemap <- function() {
c("Blanc-Sablon", 51.426883, -57.132590+.04, 0, -1.4, 0.14),
c("Gaspé", 48.831567, -64.487300+.04, 0, -0.8, -0.08),
c("Caraquet", 47.777730, -64.953491+.04, 0, -1, -0.25),
c("La Pocatière", 47.368856, -70.023220+.04, 0, 0.1, -0.1),
c("Tadoussac", 48.154946+.04, -69.725426+.02,.0, -1.2, -0.08),
c("Rimouski", 48.444147-.03, -68.545120+.05, 0, 0.1, -0.1),
c("Matane", 48.847708-.04, -67.527839+.03, 0, 0.1, -0.1),
c("Channel-Port-Aux-Basques", 47.579661, -59.149330, 0, 0.1, 0.12)
Expand Down
Loading

0 comments on commit 29ddf2d

Please sign in to comment.