knitr::opts_chunk$set(echo = TRUE)
#Dendograma
##(https://es.wikipedia.org/wiki/Dendrograma)
library(ggplot2)
str(mpg)
## tibble [234 x 11] (S3: tbl_df/tbl/data.frame)
## $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
## $ model : chr [1:234] "a4" "a4" "a4" "a4" ...
## $ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
## $ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
## $ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
## $ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
## $ drv : chr [1:234] "f" "f" "f" "f" ...
## $ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
## $ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
## $ fl : chr [1:234] "p" "p" "p" "p" ...
## $ class : chr [1:234] "compact" "compact" "compact" "compact" ...
data(mpg)
#plot(mpg)
#grafica <- ggplot(mpg, aes(cty, hwy, color=displ, size=displ)) + geom_point()+scale_color_viridis_c()
grafica <- ggplot(mpg, aes(cty, hwy, color=class, size=displ)) + geom_point()+scale_color_brewer(palette ="Set1")
grafica
mpg2008 <- subset(mpg,mpg$year == 2008)
mpg2008N <- mpg2008
mpg2008N[,c(3,5,8,9)]<- scale(mpg2008[,c(3,5,8,9)])
# crear columna con secuencia de números
mpg2008N$ID <- seq(1,nrow(mpg2008N))
# pegar la marca al numero de sequencia creado
mpg2008N$ID <- do.call(paste0,mpg2008N[c(1,12)])
# renombrar las filas
rownames(mpg2008N) <- mpg2008N$ID
## Warning: Setting row names on a tibble is deprecated.
matrizDistancias <- dist(mpg2008N)
## Warning in dist(mpg2008N): NAs introduced by coercion
#head(data.matrix(matrizDistancias))
#Convertir de WIDE a LONG
library(reshape2)
matrizlong <- melt(data.matrix(matrizDistancias))
#head(matrizlong)
grafica <- ggplot(matrizlong, aes(as.factor(Var1),as.factor(Var2), fill=value))
grafica <- grafica +geom_tile()+scale_fill_viridis_c()
grafica
#Dendrogramas
hc2008 <- hclust(matrizDistancias)
# Construye la gráfica. Si no tiene la librearía instalada instale 'ggdendro'
library(ggdendro)
grafica <- ggdendrogram (hc2008, rotate=TRUE, size=2)
grafica
dhc2008 <- as.dendrogram(hc2008)
# Function to color branches
colbranches <- function(n, col)
{
a <- attributes(n) # Find the attributes of current node
# Color edges with requested color
attr(n, "edgePar") <- c(a$edgePar, list(col=col, lwd=2))
n # Don't forget to return the node!
}
# Color the first sub-branch of the first branch in red,
# the second sub-branch in orange and the second branch in blue
dhc2008[[1]][[1]] = dendrapply(dhc2008[[1]][[1]], colbranches, "red")
dhc2008[[1]][[2]] = dendrapply(dhc2008[[1]][[2]], colbranches, "orange")
dhc2008[[2]][[2]][[1]][[2]][[2]] = dendrapply(dhc2008[[2]][[2]][[1]][[2]][[2]], colbranches, "blue")
# Plot
plot(dhc2008)
# Seleccionar un de los vehículos
azules <-mpg2008[4,]
# agrego los demas
azules <- rbind(azules,
mpg2008[60, ],
mpg2008[4, ],
mpg2008[60, ],
mpg2008[5, ],
mpg2008[87, ],
mpg2008[92, ],
mpg2008[85, ],
mpg2008[90, ])
# mostar el resultado
azules
## # A tibble: 9 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 quat~ 2 2008 4 manual~ 4 20 28 p compa~
## 2 hyundai tiburon 2 2008 4 manual~ f 20 28 r subco~
## 3 audi a4 quat~ 2 2008 4 manual~ 4 20 28 p compa~
## 4 hyundai tiburon 2 2008 4 manual~ f 20 28 r subco~
## 5 audi a4 quat~ 2 2008 4 auto(s~ 4 19 27 p compa~
## 6 subaru foreste~ 2.5 2008 4 auto(l~ 4 20 26 r suv
## 7 subaru impreza~ 2.5 2008 4 manual~ 4 20 27 r compa~
## 8 subaru foreste~ 2.5 2008 4 manual~ 4 20 27 r suv
## 9 subaru impreza~ 2.5 2008 4 auto(s~ 4 20 27 r compa~