Can I colour my dendrogram based on site criteria

Viewed 30

I am really struggling to get my code working for real data! (first question, so apologies if it is not up to standard)

I am trying to plot the results of a vegetation survey as a dendrogram, with the leaves/labels coloured by pre-defined criteria, which definitely seems possible: https://cran.r-project.org/web/packages/dendextend/vignettes/FAQ.html My labels are site names, and the colours should be vegetation types/locations (e.g. if half of my sites are in a creek and half aren't, being able to see how these locations have been separated across the dendrogram would be really useful).

My 'dummy' code does what I'm after (3 veg types, four sites)

{library(tidyverse)
  library(vegan)
  library(ggplot2)
  library(cluster)
library(dendextend)}

Site <- c('Q1','Q1','Q1','Q2','Q2','Q2','Q3','Q3','Q3','Q4','Q4','Q4')
Species <- c('Malva','Sida','Corchorus','Tephrosia','Acacia','Triodia',
'Eucalyptus','Acacia','Triodia', 'Acacia', 'Triodia','Eucalyptus')
Presence <- as.numeric(c('1','1','1','1','1','1','1','1','1','1','1','1'))
SiteData <- data.frame(Site, Species, Presence)
Site <- c('Q1','Q2','Q3','Q4')
VegType <- c('VT7', 'VT2', 'VT5','VT5')
VegTypes <- data.frame(Site, VegType)
SiteWide <- pivot_wider(SiteData, names_from = Species, values_from = Presence, values_fill = list(Presence=0))
SiteWide <- SiteWide %>% column_to_rownames(var="Site") %>% as.data.frame()

dend <- as.dendrogram(hclust(dist(SiteWide)))
plot(dend)

#VegType <- rep("Other", length(rownames(VegTypes)))
is_x <- grepl("VT7", rownames(VegTypes))
VegType[is_x] <- "VT7"
is_x <- grepl("VT2", rownames(VegTypes))
VegType[is_x] <- "VT2"
is_x <- grepl("VT5", rownames(VegTypes))
VegType[is_x] <- "VT5"
VegType <- factor(VegType)
n_VegType <- length(unique(VegType))
cols_3 <- colorspace::rainbow_hcl(n_VegType, c = 70, l  = 50)
col_veg_type <- cols_3[VegType]

#color labels by vegetation type:
labels_colors(dend) <- col_veg_type[order.dendrogram(dend)]
plot(dend)

The problem is in my real data (16 veg types, ~100 sites)... I think my issues is right at the end, assigning the 16 colours to the order of the veg types. Any suggestions on how to properly code it would be hugely appreciated! (Also, I think this is one method of many - I have tried so many ways and can't get any to work, so this is my last resort!) :( Thanks in advance!

#Code to load in vegetation types
VegTypes <- read.csv("VegTypeQuads.csv")
VegType <- rep("Other", length(rownames(VegTypes)))
#VegTypes$VegType <- as.factor(VegTypes$VegType)
is_x <- grepl("VT01", rownames(VegTypes))
VegType[is_x] <- "VT01"
is_x <- grepl("VT02", rownames(VegTypes))
VegType[is_x] <- "VT02"
is_x <- grepl("VT03", rownames(VegTypes))
VegType[is_x] <- "VT03"
is_x <- grepl("VT04", rownames(VegTypes))
VegType[is_x] <- "VT04"
is_x <- grepl("VT05", rownames(VegTypes))
VegType[is_x] <- "VT05"
is_x <- grepl("VT06", rownames(VegTypes))
VegType[is_x] <- "VT06"
is_x <- grepl("VT07", rownames(VegTypes))
VegType[is_x] <- "VT07"
is_x <- grepl("VT08", rownames(VegTypes))
VegType[is_x] <- "VT08"
is_x <- grepl("VT09", rownames(VegTypes))
VegType[is_x] <- "VT09"
is_x <- grepl("VT10", rownames(VegTypes))
VegType[is_x] <- "VT10"
is_x <- grepl("VT11", rownames(VegTypes))
VegType[is_x] <- "VT11"
is_x <- grepl("VT12", rownames(VegTypes))
VegType[is_x] <- "VT12"
is_x <- grepl("VT13", rownames(VegTypes))
VegType[is_x] <- "VT13"
is_x <- grepl("VT14", rownames(VegTypes))
VegType[is_x] <- "VT14"
is_x <- grepl("VT15", rownames(VegTypes))
VegType[is_x] <- "VT15"
is_x <- grepl("VT16", rownames(VegTypes))
VegType[is_x] <- "VT16"
VegType <- factor(VegType)
n_VegType <- length(unique(VegType))
cols_16 <- colorspace::rainbow_hcl(n_VegType, c = 70, l  = 50)
col_veg_type <- cols_16[VegType]

labels_colors(dend) <- col_veg_type[order.dendrogram(dend)] #this doesn't seem to do anything to my large data set

cl <- hclust(vegdist(data))
dend = as.dendrogram(cl)
VegTypes$VegType[cl$order]   #this gives me the veg types in the correct order (i.e. order that the corresponding site occurs in the dendrogram) 

plot(dend)

#not sure how to get the veg type colour into the dendrogram though!
1 Answers

I did end up solving the issue. When using real data, it was imported as a data frame, so I had to input it as a vector:

SiteID <- as.vector(VegTypes$SiteID)
VegType <- as.vector(VegTypes$VegType)
Related