setwd("C:/Users/emmil/OneDrive/Documentos/EcoNum")
dados<-read.table("biotico.txt",header=T)
dadost<-log1p(dados)#log(x+1)
matriz<-vegdist(dadost,upper=FALSE, "bray")
d<-hclust(matriz, method="complete")
plot(d)

plot(d, hang=-1,ann=FALSE, cex.axis=1.1,col=3)
title(ylab="Dissimilaridade", main="Estuários",xlab="Amostras",cex.lab=1.2)

cutree(d, 4)
## TST1 TST2 TST3 GST1 GST2 GST3 PST1 PST2 PST3 
##    1    1    1    2    2    2    3    4    4

Comparando os métodos de ligação simples, completa e média:

d<-hclust(matriz, method="single")
e<-hclust(matriz, method="complete")
f<-hclust(matriz, method="average")
plot(d, hang=-1, ylab="Dissimilaridade")
rect.hclust(d, 3)

plot(e, hang=-1,ylab="Dissimilaridade")
rect.hclust(e, 3)

plot(f, hang=-1,ylab="Dissimilaridade")
rect.hclust(f, 3)

#Outras opções de Cluster

d<-hclust(matriz, method="ward.D2")
dp <- as.dendrogram(d)
plot(dp, type = "rectangle", ylab = "Height")

plot(dp, xlim = c(1, 3), ylim = c(0,0.8))

plot(dp, type = "triangle", ylab = "Height")

nodePar <- list(lab.cex = 0.6, pch = c(NA, 19), cex = 0.7, col = "blue")
plot(dp, ylab = "Height", nodePar = nodePar)

plot(dp, xlab = "Height", nodePar = nodePar, horiz=TRUE)

library(factoextra)
## Warning: package 'factoextra' was built under R version 4.1.3
## Carregando pacotes exigidos: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
dados<-read.table("biotico.txt",header=T)
dadost<-log1p(dados)
 matriz<-vegdist(dadost,upper=FALSE, "bray")
result <- hcut(matriz, k = 4, method="complete",stand = TRUE)
fviz_dend(result, rect = TRUE, cex = 0.5, k_colors = c("#00AFBB","#2E9FDF", "#E7B800", "#FC4E07"))
## Registered S3 method overwritten by 'dendextend':
##   method     from 
##   rev.hclust vegan
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.

O agrupamento é independente do método

dados<-read.table("biotico.txt",header=T)
dadost<-log1p(dados)
library(dendextend)
## Warning: package 'dendextend' was built under R version 4.1.3
## 
## ---------------------
## 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:permute':
## 
##     shuffle
## The following object is masked from 'package:stats':
## 
##     cutree
dend <- dadost %>% 
dist %>% # calculando a matriz, 
        hclust(method = "ward.D2") %>% # cluster
        as.dendrogram # transforma o objeto em dendrograma
plot(dend)

dend %>% set("labels", c("P1", "G3", "G1", "G2", "T1","T2","T3","P2","P3")) %>% plot

dend %>% set("labels", c("P1", "G3", "G1", "G2", "T1","T2","T3","P2","P3")) %>% set("labels_col", c(2,3,3,3,4,4,4,2,2)) %>%  set("labels_cex", 1.2) %>% plot

dend %>% set("labels", c("P1", "G3", "G1", "G2", "T1","T2","T3","P2","P3")) %>% set("labels_col", c(2,3,3,3,4,4,4,2,2)) %>%  set("labels_cex", 1.2) %>% plot

library(vegan)
library(clustsig)
usarrests<-USArrests[,c(1,2,4)]
rownames(usarrests)<-state.abb
res <- simprof(data=usarrests,method.distance="braycurtis")
## Warning: This version of the Bray-Curtis index does not use standardization.
## Warning: To use the standardized version, use "actual-braycurtis".
## Warning: See the help documentation for more information.
pl.color <- simprof.plot(res)

kcluster<- kmeans(dadost,centers= 4, iter.max=1000, nstart=10000)
dados<-read.table("base.txt",header=T)
dadost<-sqrt(dados)
matriz<-vegdist(dadost,upper=FALSE, "bray")
mds<-cmdscale(matriz)
grupos<-levels(factor(kcluster$cluster))
ordiplot(mds, type = "n")
## species scores not available

cols <- c("mediumblue", "darkred", "darkgreen", "pink")
library(vegan)
dados<-read.table("abiotic.txt", header=T)
library(BBmisc)
## Warning: package 'BBmisc' was built under R version 4.1.3
## 
## Attaching package: 'BBmisc'
## The following object is masked from 'package:base':
## 
##     isFALSE
dadosn<-normalize(dados, method = "standardize", range = c(0, 1), margin = 1L, on.constant = "quiet")

Matriz de similaridade

matriz<-dist(dadosn) # distancia euclidiana, para dados métricos
## Warning in dist(dadosn): NAs introduzidos por coerção
mds<- cmdscale(matriz,eig=TRUE, k=2)
um<- mds$points[,1]
dois<- mds$points[,2]
plot(um, dois, xlab="Coordenada 1", ylab="Coordenada 2", main="MDS Métrico",type="n")
text(um, dois, labels = row.names(dados), cex=1)