library(ggplot2)
library(ggdendro)
library(dplyr)
library(DT)data_iris <- read.table("data_iris.csv", header = T, row.names = 1, sep=",")
datatable(data_iris, class = "compact")data <- data_iris[,-c(5,6)]
datatable(data, class = "compact")#get dataframe for family
fam=data.frame(row.names(data_iris),data_iris$Family)
colnames(fam) <- c("label","family")
datatable(fam, class = "compact")#analysis hc, dend....
hc <- hclust(dist(data))
dend <- as.dendrogram(hc)
dendata <- dendro_data(dend)ggplot with take label information in dataframe label(dendata). So we need add column family to this dataframe with function inner_join from package dplyr.
#add column family to labs dataframe (look like VLOOKUP in excel)
labs <- label(dendata)
datatable(labs, class = "compact")labs <- inner_join(labs, fam, by = "label")
datatable(labs, class = "compact")#Plot
p <- ggplot(segment(dendata)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) +coord_flip()+scale_y_reverse()
p <- p + geom_text(data=labs,
aes(label=label, x=x, y=-0.5, colour=labs$family)) +
scale_colour_manual(values=c("blue", "red", "darkgreen"))p