#BENTOS2006 - RDA----
#####....----
browseURL("https://r.qcbs.ca/workshops/")
##ORGANIZANDO DADOS----
dev.off() #apaga os graficos, se houver algum
rm(list=ls(all=TRUE)) #limpa a memória
cat("\014") #limpa o console
t_grps <- read.csv("t_grps.csv",
sep = ";", dec = ".",
row.names = 1,
header = TRUE,
na.strings = NA)
m_dens <- read.csv("m_dens.csv",
sep = ";", dec = ".",
row.names = 1,
header = TRUE,
na.strings = NA)
library(vegan)
library(tidyverse)
m_m <- read.table("m_m.txt")
m_q <- read.table("m_q.txt")
m_s <- read.table("m_s.txt")
m_h <- read.table("m_h.txt")
##CLASSIFICAÇÃO 1: MATRIZ COMUNITÁRIA----
###DENDROGRAMA E HEATMAP 1----
# Dendrograma
cluster_uas <- hclust(m_dist, method = "average")
plot(cluster_uas, main = "Cluster Dendrogram - Bray-Curtis da Matriz Comunitária",
hang = 0.1) #testar com -.01
rect.hclust(cluster_uas, k = 3, h = NULL) #h = 0.8 fornece os grupos formados na altura h
as.matrix(m_dist)[1:6, 1:6]
# Heatmap
library("gplots")
heatdist <- as.matrix(m_dist)
col <- rev(heat.colors(999)) #rev() reverte as cores do heatmap
heatmap.2(x=(as.matrix(m_dist)), #objetos x objetos
Rowv = as.dendrogram(cluster_uas),
Colv = as.dendrogram(cluster_uas),
key = T, tracecol = NA, revC = T,
col = heat.colors, #dissimilaridade = 1 - similaridade
density.info = "none",
xlab = "UA´s", ylab = "UA´s",
main = "Comunidade",
mar = c(6, 6) + 0.2)
cluster_spp <- hclust((vegdist(t(m_trns), method = "bray",
diag = TRUE,
upper = FALSE)), method = "average")
plot(cluster_spp, main = "Dendrograma dos atributos")
heatmap.2(t(as.matrix(m_trns)), #objetos x atributos
Colv = as.dendrogram(cluster_uas),
Rowv = as.dendrogram(cluster_spp),
key = T, tracecol = NA, revC = T,
col = col,
density.info = "none",
xlab = "Unidades amostrais", ylab = "Espécies",
mar = c(6, 6) + 0.1) #adjust margin size
# Histórico das fusões
library(gt)
merge <- as.data.frame(cluster_uas$merge)
merge[nrow(merge)+1,] = c("0","0")
height <- as.data.frame(round(cluster_uas$height, 2))
height[nrow(height)+1,] = c("1.0")
fusoes <- data.frame(Cluster_uas = merge, Height = height)
colnames(fusoes) <- c("Cluster1", "Cluster2", "Height")
UA <- rownames_to_column(as.data.frame(m_trns[, 0]))
colnames(UA) <- c("UAs")
No.UA <- 1:nrow(fusoes)
fusoes <- cbind(No.UA, UA, fusoes)
fusoes$Histórico <- 1:nrow(fusoes)
#fusoes
gt(fusoes)
##CLASSIFICAÇÃO 2: MATRIZ AMBIENTAL----
###DENDROGRAMA E HEATMAP 2----
# Dendrograma
m_hab_trns <- read.csv("m_hab_trns.csv",
sep = ";", dec = ".",
row.names = 1,
header = TRUE,
na.strings = NA)
m_dist_hab <- vegdist(m_hab_trns, method = "bray",
diag = TRUE,
upper = FALSE)
cluster_uas <- hclust(m_dist_hab, method = "average")
plot(cluster_uas, main = "Cluster Dendrogram - Bray-Curtis para a Matriz Ambiental",
hang = 0.1) #testar com -.01
rect.hclust(cluster_uas, k = 3, h = NULL)
#h = 0.8 fornece os grupos formados na altura h
as.matrix(m_dist_hab)[1:6, 1:6]
# Heatmap
library("gplots")
heatdist <- as.matrix(m_dist_hab)
col <- rev(heat.colors(999)) #rev() reverte as cores do heatmap
heatmap.2(x=(as.matrix(m_dist_hab)), #objetos x objetos
Rowv = as.dendrogram(cluster_uas),
Colv = as.dendrogram(cluster_uas),
key = T, tracecol = NA, revC = T,
col = heat.colors, #dissimilaridade = 1 - similaridade
density.info = "none",
xlab = "UA´s", ylab = "UA´s",
main = "Dados ambientais",
mar = c(6, 6) + 0.2)
cluster_spp <- hclust((vegdist(t(m_hab_trns), method = "bray",
diag = TRUE,
upper = FALSE)), method = "average")
plot(cluster_spp, main = "Dendrograma dos atributos")
heatmap.2(t(as.matrix(m_hab_trns)), #objetos x atributos
Colv = as.dendrogram(cluster_uas),
Rowv = as.dendrogram(cluster_spp),
key = T, tracecol = NA, revC = T,
col = col,
density.info = "none",
xlab = "Unidades amostrais", ylab = "Espécies", main = "Dados ambientais",
mar = c(6, 6) + 0.1) #adjust margin size
# Histórico das fusões
library(gt)
merge <- as.data.frame(cluster_uas$merge)
merge[nrow(merge)+1,] = c("0","0")
height <- as.data.frame(round(cluster_uas$height, 2))
height[nrow(height)+1,] = c("1.0")
fusoes <- data.frame(Cluster_uas = merge, Height = height)
colnames(fusoes) <- c("Cluster1", "Cluster2", "Height")
UA <- rownames_to_column(as.data.frame(m_hab_trns[, 0]))
colnames(UA) <- c("UAs")
No.UA <- 1:nrow(fusoes)
fusoes <- cbind(No.UA, UA, fusoes)
fusoes$Histórico <- 1:nrow(fusoes)
#fusoes
gt(fusoes)
##CLASSIFICAÇÃO 3: Heatmap Matriz comunitária *vs.* Matriz ambiental----
###DENDROGRAMA E HEATMAP 3----
# Dendrograma
library(vegan)
library(gplots)
# Dendrograma para as UA's da matriz comunitária
cluster_uas_com <- hclust(m_dist, method = "average")
plot(cluster_uas_com, main = "Cluster Dendrogram - Bray-Curtis da Matriz Comunitária",
hang = 0.1) #testar com -.01
rect.hclust(cluster_uas_com, k = 3, h = NULL)
#h = 0.8 fornece os grupos formados na altura h
as.matrix(m_dist)[1:6, 1:6]
# Dendrograma para as variáveis particionadas da matriz ambiental
m_trns_hab <- sqrt(m_hab_part)
cluster_mpart_hab <- hclust((vegdist(t(m_trns_hab),
method = "bray",
diag = TRUE,
upper = FALSE)), method = "average")
plot(cluster_mpart_hab, main = "Dendrograma dos atributos")
# Heatmap Comunidade vs. Morfologia
heatmap.2(t(as.matrix(m_trns_hab)), #objetos x atributos
Colv = as.dendrogram(cluster_uas_com),
Rowv = as.dendrogram(cluster_mpart_hab),
key = T, tracecol = NA, revC = T,
col = col,
density.info = "none",
xlab = "Unidades amostrais", ylab = "Varíáveis ambientais",
mar = c(6, 9) + 0.1) #adjust margin size
# Explorando correlações multivariadas da matriz de habitat
#plot(m_trns_a$"m.elev", m_trns_a$"m.river")
#plot(scale(m_trns_a$"m.elev"), scale(m_trns_a$"m.river"))
#plot((m_trns_a$"m.elev" - mean(m_trns_a$"m.elev")) / sd(m_trns_a$"m.elev"))
library(psych)
pairs.panels(m_trns_hab[,],
method = "pearson", # correlation method
scale = FALSE, lm = FALSE,
hist.col = "#00AFBB", pch = 19,
density = TRUE, # show density plots
ellipses = TRUE, # show correlation ellipses
alpha = 0.5
)
#ANÁLISE DE REDUNDÂNCIA----
##TRANFORMAÇÃO DA MATRIZ DE COMUNIDADE COM `hellinger`----
com_hell <- decostand(m_dens, method = "hellinger")
m_trns_hab <- m_m
### FAZENDO A RDA----
rda <- rda(com_hell ~ ., m_trns_hab) #variavel de resposta ~ variavel explanatória
summary(rda)
#str(rda)
modelo_rda <- rda$call
modelo_rda
# Testando a significância dos resultados da RDA
RsquareAdj(rda)
anova.cca(rda) #significância global
anova.cca(rda, by="axis") #sign. dos eixos da RDA
anova.cca(rda, by="terms") #sign. das var. explanatórias
### GRÁFICOS DE RDA----
# Gráfico triplot basico
plot(rda, choices = c(1,2))
plot(rda, display = c("sp","bp"))
# Ajustes no gráfico triplot
model <- ordiplot(rda, type = "none", scaling = 2, xlab = "RDA1", ylab = "RDA2") #type="points" ou "text"
##UAs
points(rda, col = "black", pch = 21, bg = "gray", cex = 1)
text(rda, col = "black", cex = 0.7, pos = 1)
##vetores
points(rda, dis = "bp", col = "red", pch = 21, cex = 1)
text(rda, dis = "bp", col = "red", cex = 0.9, pos = 2)
##espécies
points(rda, dis = "sp", col = "blue", pch = 3, cex = 1)
text(rda, dis = "sp", col = "blue", cex = 0.8, pos = 1)
# Show only the first [min:max] species with higher values
high_species <- order(abs(scores(rda, display = "species")[, 1]), decreasing = TRUE)[1:9]
high_species
# Get the indices of the first 5 species with higher values on the first axis
points(rda, dis = "sp", select = high_species, col = "darkblue", pch = 3, cex = 1)
text(rda, display = "species", select = high_species, col = "darkblue", cex = 0.8, pos = 4) #display the selected species
### AVALIANDO COLINEARIDADE
#(Step1-Remove the worst offender first, Step 2—Recheck VIF, Step 3—Final clean model)
# Função variance inflation factors
vif.cca(rda) #remover valores com colinearidade maior que 20
rda
#rda <- rda(formula = com_hell ~ river_length + depth_hab + depth_max + slope + width, data = m_trns_hab)
### ESCOLHENDO AS PRINCIPAIS VARIÁVEIS DO MODELO DA RDA
#It is important to note that selecting variables ecologically is much more important than performing selection in this way. If a variable of ecological interest is not selected, this does not mean it has to be removed from the RDA ("https://r.qcbs.ca/workshop10/book-en/redundancy-analysis.html").
# GLM
rda1 <- step(rda, scope = formula(rda), scale = 0, direction = "both", steps = 1000, test = "perm") #step refits a new RDA with variables added or removed, based on permutations and AIC
summary(rda1)
modelo_rda1 <- rda1$call #variáveis sugeridas pelo modelo
modelo_rda1
#(matriz de dados comunitária ~ variáveis da matriz ambiental)
rda2 <- rda(formula(rda1), data = m_trns_hab)
#(variáveis de resposta ~ variaveis explanatórias sugeridas pelo modelo stepwise regression)
#rda2 <- rda(formula = hell ~ turb, data = m_trns_hab)
rda2
summary(rda2)
#str(rda2)
# Colinearidade do novo modelo
vif.cca(rda2)
sink("anovas_rda2_s.txt")
# ANOVAS
RsquareAdj(rda2) #total variance explained and it´s significcance
anova.cca(rda2, perm.max = 999)#overall model, do these variables together explain community composition better than random?
anova.cca(rda2, by="axis", perm.max = 999) #axes, are the canonical axes significant?
anova.cca(rda2, by="terms", perm.max = 999) #sequential (Type I) effects, does a given variable explain additional variance beyond the one entered before it in the model?
anova.cca(rda2, by="margin", perm.max = 999) #marginal (Type III) effects, does each variable explain variance after controlling for the other? *REPORT THIS
summary(rda2)
sink()
### INDIVIDUAL FINAL SIMPLIFIED TRIPLOT----
# Getting axes explained variances
eig <- rda2$CCA$eig
var_exp <- eig / sum(rda2$tot.chi) * 100
var_exp
RDA1_perc <- round(var_exp[1], 1)
RDA2_perc <- round(var_exp[2], 1)
png("fig-rda_h.png", units = "in", width = 8, height = 8, res = 500)
simplified_model <- ordiplot(rda2, type = "n", scaling = 2, #ylim = c(-1, 1),
xlab = paste0("RDA1 (", RDA1_perc, "%)"),
ylab = paste0("RDA2 (", RDA2_perc, "%)"))
##UAs
UAs <- t_grps$UA
points(rda2, col = "black", pch = 21, bg = "gray", cex = 1)
text(rda2, labels = UAs, col = "black", cex = 0.8, pos = 2)
##vetores
points(rda2, dis = "bp", col = "red", pch = 21, cex = 1)
text(rda2, dis = "bp", col = "red", cex = 0.9, pos = 1)
##espécies
points(rda2, dis = "sp", col = "red", pch = 3, cex = 0.5)
#text(rda2, dis = "sp", col = "red", pos = 1)
##espécies principais
# Show only the first [min:max] species with higher values
high_species <- order(abs(scores(rda2, display = "species")[, 1]), decreasing = TRUE)[1:9]
high_species
# Get the indices of the first 5 species with higher values on the first axis
#points(rda2, dis = "sp", select = high_species, col = "darkblue", pch = 3, cex = 1)
orditorp(rda2, dis = "sp", select = high_species, col = "darkblue", cex = 0.8, pos = 4, air = 0.5) #display the selected species
dev.off()
##COMBINED FINAL PANEL----
library(png)
library(grid)
library(gridExtra)
imgs <- c("fig-rda_m.png",
"fig-rda_q.png",
"fig-rda_s.png",
"fig-rda_h.png")
# Read images and convert to grobs
img_grobs <- lapply(imgs, function(x) {
rasterGrob(readPNG(x), interpolate = TRUE)
})
labels <- c("A", "B", "C", "D")
img_grobs_labeled <- mapply(
function(img, lab) {
arrangeGrob(
img,
top = textGrob(lab, x = unit(0.15, "npc"), y = unit(-0.8, "npc"), #posição das labels
just = c("left", "top"),
gp = gpar(fontsize = 14, fontface = "bold"))
)
},
img_grobs, labels,
SIMPLIFY = FALSE
)
png("fig-rda_panel.png", units = "in", width = 10, height = 10, res = 500)
grid.arrange(
grobs = img_grobs_labeled,
ncol = 2,
top = textGrob("", gp = gpar(fontsize = 16, fontface = "bold")))
dev.off()
### CROPPING IMAGENS----
library(png)
# Read the image
img <- readPNG("fig-rda_h.png")
plot(as.raster(img))
# Image dimensions
h <- dim(img)[1]
w <- dim(img)[2]
# Crop size in pixels (1 inch at 500 dpi)
crop_px <- 500
# Crop
img_cropped <- img[
(crop_px + 1):(h - crop_px),
(crop_px + 1):(w - crop_px),
,
drop = FALSE
]
plot(as.raster(img_cropped))
# Save cropped image
writePNG(img_cropped, "cropped_rda_h.png")
# Cropping batch
files <- c("fig-rda_m.png", "fig-rda_q.png",
"fig-rda_s.png", "fig-rda_h.png")
for (f in files) {
img <- readPNG(f)
h <- dim(img)[1]
w <- dim(img)[2]
crop_px <- 500
img_cropped <- img[
(crop_px + 1):(h - crop_px),
(crop_px + 1):(w - crop_px),
,
drop = FALSE
]
writePNG(img_cropped, paste0("cropped_", f))
}