Creditos: diegokjkjj
library(ade4)
library(vegan)
## Carregando pacotes exigidos: permute
## Carregando pacotes exigidos: lattice
## This is vegan 2.5-7
library(gclus)
## Carregando pacotes exigidos: cluster
## Registered S3 method overwritten by 'gclus':
## method from
## reorder.hclust vegan
library(cluster)
library(usedist)
library(dendextend)
## Registered S3 method overwritten by 'dendextend':
## method from
## rev.hclust vegan
##
## ---------------------
## Welcome to dendextend version 1.15.2
## Type citation('dendextend') for how to cite the package.
##
## Type browseVignettes(package = 'dendextend') for the package vignette.
## The github page is: https://github.com/talgalili/dendextend/
##
## Suggestions and bug-reports can be submitted at: https://github.com/talgalili/dendextend/issues
## You may ask questions at stackoverflow, use the r and dendextend tags:
## https://stackoverflow.com/questions/tagged/dendextend
##
## To suppress this message use: suppressPackageStartupMessages(library(dendextend))
## ---------------------
##
## Attaching package: 'dendextend'
## The following object is masked from 'package:gclus':
##
## order.hclust
## The following object is masked from 'package:permute':
##
## shuffle
## The following object is masked from 'package:stats':
##
## cutree
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.1.2
## Carregando pacotes exigidos: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(ggplot2)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.0.2 v forcats 0.5.1
## v purrr 0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
setwd("G:/Meu Drive/UFPE/2021.1/Ecologia Numérica")
load("G:/Meu Drive/UFPE/2021.1/Ecologia Numérica/NEwR-2ed_code_data (2)/NEwR-2ed_code_data/NEwR2-Data/Doubs.RData")
SpeAnly<-vegdist(spe, method = "euclidean")
SpeAnly
## 1 2 3 4 5 6 7
## 2 5.385165
## 3 7.416198 2.449490
## 4 7.874008 4.123106 3.000000
## 5 10.816654 10.677078 10.862780 9.219544
## 6 7.348469 4.582576 4.123106 2.828427 8.185353
## 7 6.855655 2.449490 2.000000 3.605551 10.488088 3.605551
## 8 3.000000 7.071068 8.717798 8.774964 10.954451 7.937254 8.246211
## 9 7.810250 8.717798 9.380832 8.774964 9.380832 6.708204 8.485281
## 10 6.708204 5.099020 5.291503 5.000000 9.273618 3.605551 4.472136
## 11 4.472136 3.316625 5.000000 5.477226 10.246951 5.291503 4.795832
## 12 6.708204 3.162278 3.464102 4.582576 11.045361 4.795832 3.162278
## 13 7.071068 4.358899 5.196152 6.164414 11.532563 6.633250 5.385165
## 14 9.110434 6.324555 6.164414 6.557439 11.916375 7.000000 6.324555
## 15 9.899495 7.810250 7.681146 7.483315 10.816654 6.633250 6.855655
## 16 11.090537 9.899495 9.797959 9.327379 10.198039 8.426150 9.055385
## 17 10.630146 9.486833 9.486833 8.774964 9.797959 8.062258 9.055385
## 18 9.848858 9.591663 9.899495 8.660254 9.055385 7.810250 9.380832
## 19 11.704700 11.135529 11.045361 10.148892 9.380832 8.888194 10.770330
## 20 13.453624 14.212670 14.628739 13.453624 10.770330 12.206556 14.212670
## 21 14.456832 15.362291 15.811388 14.525839 11.489125 13.601471 15.556349
## 22 16.643317 17.663522 18.110770 16.763055 13.416408 15.779734 17.663522
## 23 3.872983 7.483315 9.055385 9.000000 10.583005 7.937254 8.485281
## 24 7.071068 9.539392 10.816654 10.583005 11.357817 9.486833 10.246951
## 25 5.291503 8.306624 9.643651 9.273618 9.746794 8.246211 9.110434
## 26 11.135529 12.609520 13.304135 12.409674 10.723805 11.224972 12.922848
## 27 15.362291 16.462078 16.881943 15.811388 13.076697 14.696938 16.583124
## 28 16.522712 17.549929 18.000000 16.941074 14.352700 15.905974 17.606817
## 29 18.761663 19.364917 19.621417 18.384776 15.524175 17.776389 19.364917
## 30 20.396078 21.377558 21.794495 20.542639 17.117243 19.849433 21.517435
## 8 9 10 11 12 13 14
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9 7.211103
## 10 6.480741 6.480741
## 11 5.385165 7.549834 4.582576
## 12 8.124038 8.717798 5.477226 3.872983
## 13 8.426150 10.049876 6.855655 4.000000 3.316625
## 14 10.198039 10.583005 7.615773 6.244998 4.242641 3.316625
## 15 10.630146 9.746794 6.855655 7.745967 6.403124 6.633250 5.000000
## 16 11.489125 10.862780 8.485281 10.049876 9.591663 9.746794 8.944272
## 17 10.770330 9.899495 8.246211 9.219544 9.273618 9.643651 9.380832
## 18 9.695360 8.602325 7.874008 8.774964 9.380832 9.949874 9.797959
## 19 11.313708 9.486833 9.273618 11.000000 11.313708 12.041595 11.832160
## 20 13.114877 11.489125 12.727922 13.527749 14.422205 15.000000 14.966630
## 21 14.142136 13.266499 14.142136 14.662878 15.684387 16.031220 16.062378
## 22 16.370706 15.033296 16.248077 16.941074 17.832555 18.303005 18.165902
## 23 2.449490 6.324555 6.633250 5.744563 8.366600 8.774964 10.392305
## 24 6.403124 7.549834 8.544004 8.124038 10.148892 10.583005 11.789826
## 25 4.358899 7.280110 7.000000 6.782330 9.110434 9.486833 10.723805
## 26 10.723805 10.148892 11.445523 11.747340 13.000000 13.490738 13.892444
## 27 15.066519 13.892444 15.264338 15.748016 16.703293 17.146428 17.117243
## 28 16.248077 14.899664 16.309506 16.822604 17.720045 18.193405 18.220867
## 29 18.681542 17.521415 18.303005 18.761663 19.416488 19.646883 19.313208
## 30 20.174241 19.467922 20.371549 20.736441 21.610183 21.863211 21.931712
## 15 16 17 18 19 20 21
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9
## 10
## 11
## 12
## 13
## 14
## 15
## 16 5.916080
## 17 8.185353 6.164414
## 18 8.660254 7.615773 3.464102
## 19 10.630146 9.165151 6.633250 6.000000
## 20 13.674794 12.649111 9.380832 7.615773 6.324555
## 21 15.066519 13.928388 11.224972 9.380832 7.874008 3.741657
## 22 16.763055 15.556349 13.416408 11.489125 10.583005 6.480741 4.000000
## 23 10.630146 11.489125 10.295630 9.055385 10.488088 11.916375 13.114877
## 24 11.747340 12.449900 10.723805 9.110434 9.746794 10.148892 11.269428
## 25 10.583005 11.180340 10.148892 8.660254 9.539392 10.630146 11.618950
## 26 13.190906 13.076697 10.816654 8.774964 7.810250 5.385165 5.385165
## 27 16.062378 15.329710 13.304135 11.532563 10.246951 6.244998 4.582576
## 28 17.175564 16.370706 14.352700 12.489996 11.489125 7.745967 6.000000
## 29 18.330303 17.058722 14.662878 13.076697 12.767145 9.000000 7.000000
## 30 21.023796 19.621417 17.117243 15.652476 15.000000 11.180340 9.000000
## 22 23 24 25 26 27 28
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9
## 10
## 11
## 12
## 13
## 14
## 15
## 16
## 17
## 18
## 19
## 20
## 21
## 22
## 23 15.362291
## 24 13.304135 4.358899
## 25 13.964240 3.000000 3.741657
## 26 7.549834 9.433981 7.071068 8.000000
## 27 5.000000 14.035669 11.916375 12.649111 5.291503
## 28 5.099020 15.231546 13.076697 13.964240 7.000000 3.872983
## 29 5.567764 17.804494 15.874508 16.431677 9.899495 6.633250 5.567764
## 30 8.185353 19.416488 17.832555 18.055470 11.916375 9.165151 7.280110
## 29
## 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 6.480741
Clust<-hclust(SpeAnly, method = "complete")
Clustgraf<-plot(Clust, hang=-1,main="Distância Euclidiana das Comunidades de Peixes")
Clustgraf
## NULL
fviz_nbclust(spe, kmeans, method = "silhouette")
clust.spe<-hclust(SpeAnly, method = "complete")
spe.dist.graf<-plot(clust.spe, hang=-1)
clust.spe1<-hclust(SpeAnly, method = "ward.D")
spe.dist.graf1<-plot(clust.spe1, hang=-1)
clust.spe2<-hclust(SpeAnly, method = "single")
spe.dist.graf2<-plot(clust.spe2, hang=-1)
clust.spe.complete<-hclust(SpeAnly, method = "complete")
clust.spe.complete.graf<-plot(clust.spe.complete, hang=-1)
grup.spe.complete<-cutree(clust.spe.complete, k=2)
table(grup.spe.complete)
## grup.spe.complete
## 1 2
## 22 8
rect.hclust(clust.spe.complete, k = 2, border = 2:5)
clust.spe.ward.D<-hclust(SpeAnly, method = "ward.D")
clust.spe.ward.D.graf<-plot(clust.spe.ward.D, hang=-1)
grup.spe.ward.D<-cutree(clust.spe.ward.D, k=2)
table(grup.spe.ward.D)
## grup.spe.ward.D
## 1 2
## 22 8
rect.hclust(clust.spe.ward.D, k = 2, border = 2:87)
clust.spe.single<-hclust(SpeAnly, method = "single")
clust.spe.single.graf<-plot(clust.spe.single, hang=-1)
grup.spe.single<-cutree(clust.spe.single, k=2)
table(grup.spe.single)
## grup.spe.single
## 1 2
## 29 1
rect.hclust(clust.spe.single, k = 2, border = 2:87)
k1<-kmeans(spe, centers = 2, nstart=25)
fviz_cluster(k1, data = spe)
k2<-kmeans(env[,-c(1:7,12)], centers = 2, nstart=25)
fviz_cluster(k2, data = env)
SpeAnly<-vegdist(spe, method = "euclidian")
clust.spe.complete<-hclust(SpeAnly, method = "complete")
grup.spe<-cutree(clust.spe.complete, k=2)
spa$cluster<-as.factor(grup.spe)
spa %>%
ggplot(aes(X,Y))+
geom_path(color="red")+
#geom_text(aes(label=env$cluster),nudge_y=1)+
geom_point(size=5,alpha=0.5,aes(color=cluster))+
annotate(geom = "text",x=1,y=37,label="baixo rio",color="red",size=4)+
annotate(geom = "text",x=80,y=20,label="alto rio",color="red",size=4)+
labs(title = "clusters ao longo do rio")+
theme_light()
EnvAnly<-vegdist(env, method = "euclidian")
clust.env.complete<-hclust(EnvAnly, method = "complete")
grup.env<-cutree(clust.env.complete, k=2)
env$cluster<-as.factor(grup.env)
env %>%
ggplot(aes(cluster,nit))+
geom_boxplot(fill="tomato",color="black")+
theme_light()+
labs(title = "Clusters das concentrações de nitrato ao longo do rio",
y="concentração de nitrato",
x="Clusters")
env %>%
ggplot(aes(cluster,amm))+
geom_boxplot(fill="yellow",color="black")+
theme_light()+
labs(title = "Clusters das concentraçõeses de amônia ao longo do rio",
y=" concentraçõeses de amônia",
x="Clusters")
env %>%
ggplot(aes(cluster,oxy))+
geom_boxplot(fill="green",color="black")+
theme_light()+
labs(title = "Clusters das concentrações de oxigênio ao longo do rio",
y=" concentração de oxigênio",
x="Clusters")
env %>%
ggplot(aes(cluster,bod))+
geom_boxplot(fill="cyan",color="black")+
theme_light()+
labs(title = "Clusters da demanda biológica por oxigênio de oxigênio ao longo do rio",
y="demanda biológica por oxigênio",
x="Clusters")