plot - How to color a dendrogram's labels according to defined groups? (in R) -


i have numeric matrix in r 24 rows , 10,000 columns. row names of matrix file names have read data corresponding each of 24 rows. apart have separate factor list 24 entires, specifying group 24 files belong. there 3 groups - alcohols, hydrocarbon , ester. names , corresponding group belong this:

> ms.mz [1] "int-354.19" "int-361.35" "int-368.35" "int-396.38" "int-408.41" "int-410.43" "int-422.43" [8] "int-424.42" "int-436.44" "int-438.46" "int-452.00" "int-480.48" "int-648.64" "int-312.14" [15] "int-676.68" "int-690.62" "int-704.75" "int-312.29" "int-326.09" "int-326.18" "int-326.31" [22] "int-340.21" "int-340.32" "int-352.35"  > ms.groups [1] alcohol     alcohol     alcohol     alcohol     hydrocarbon alcohol     hydrocarbon alcohol     [9] hydrocarbon alcohol     alcohol     alcohol     ester       alcohol     ester       ester       [17] ester       alcohol     alcohol     alcohol     alcohol     alcohol     alcohol     hydrocarbon levels: alcohol ester hydrocarbon 

i wanted generate dendrogram how data in matrix can clustered. so, used following commands:

require(vegan) dist.mat<-vegdist(ms.data.scaled.transposed,method="euclidean") clust.res<-hclust(dist.mat) plot(clust.res) 

and got dendogram. want color file names in dendrogram according group belong i.e alcohol, hydrocarbon or ester. looked @ different examples posted on forum like

label , color leaf dendrogram in r

label , color leaf dendrogram in r using ape package

clustering bootstrapping

, not implement data. not sure how correlate row.names ms.groups colored names in dendrogram.

on generating tree using dendextend (as explained in https://nycdatascience.com/wp-content/uploads/2013/09/dendextend-tutorial.pdf), following tree

enter image description here

here code used generate it:

require(colorspace) d_sims <- dist(firstpointsample5[,-1]) hc_sims <- hclust(d_sims) labels(hc_sims) dend_sims <- as.dendrogram(hc_sims) sims_groups <- rev(levels(firstpointsample5[, 1])) dend_sims <- color_branches(dend_sims, k = 3, grouplabels = sims_groups) is.character(labels(dend_sims))  plot(dend_sims) labels_colors(dend_sims) <- rainbow_hcl(3)[sort_levels_values(as.numeric(firstpointsample5[,1])[order.dendrogram(dend_sims)])] labels(dend_sims) <- paste(as.character(firstpointsample5[, 1])[order.dendrogram(dend_sims)],"(", labels(dend_sims), ")", sep = "") dend_sims <- hang.dendrogram(dend_sims, hang_height = 0.1) dend_sims <- assign_values_to_leaves_nodepar(dend_sims, 0.5,"lab.cex") par(mar = c(3, 3, 3, 7)) plot(dend_sims, main = "clustered sims dataset\n (the labels give true m/z groups)",horiz = true, nodepar = list(cex = 0.007)) legend("topleft", legend = sims_groups, fill = rainbow_hcl(3)) 

i suspect function looking either color_labels or get_leaves_branches_col. first color labels based on cutree (like color_branches do) , second allows colors of branch of each leaf, , use color labels of tree (if use unusual methods coloring branches (as happens when using branches_attr_by_labels). example:

# define dendrogram object play with: hc <- hclust(dist(usarrests[1:5,]), "ave") dend <- as.dendrogram(hc)  library(dendextend) par(mfrow = c(1,2), mar = c(5,2,1,0)) dend <- dend %>%          color_branches(k = 3) %>%          set("branches_lwd", c(2,1,2)) %>%          set("branches_lty", c(1,2,1))  plot(dend)  dend <- color_labels(dend, k = 3) # same as: # labels_colors(dend)  <- get_leaves_branches_col(dend) plot(dend) 

enter image description here

either way, should have @ set function, ideas on can done dendrogram (this saves hassle of remembering different functions names).


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -