How to label colored bars in a dendrogram

江枫思渺然 提交于 2021-01-27 07:31:58

问题


How could I add a label for some colored bars I've added in a dendrogram plot?

The code bellow will show the two attempts I've done for aiming task, which is linking the value 1 to the color red and value 0 to color white in a label for the colored bars.

# replacing the graphic window parameter so the color bars would fit
par( oma = c(0,1,1,1), mgp = c(1,0.5,0), mar = c(10,2,2,2) )

# load necessary packages
library( squash )
library( dendextend )

# "initializatin"
data("mtcars")
myDend <-  as.dendrogram(hclust(dist(mtcars))) 

# creating the numeric & color matrix used for
# (attempted) labels & colors bars, respectively
myStatus <- cbind(mtcars$vs,mtcars$am)
myColors <- matrix(c("mintcream","firebrick3")[1 + myStatus],ncol = 2)
myColors <- matrix(c("mintcream","firebrick3")[1 + cbind(mtcars$vs,mtcars$am)],
                   ncol = 2)


# default function without trying to force the label to a particular design
plot(myDend)
cmap <- squash::makecmap( myStatus, n = 2,colFn = colorRampPalette(c("mintcream","firebrick3")))
vkey(cmap, "Status")
colored_bars(colors = myColors, dend = myDend, rowLabels = c("VS","AM"))

# >> attempt 1 << trying to force breaks to 0 and 1
plot(myDend)
cmap <- squash::makecmap( myStatus, n = 2,colFn = colorRampPalette(c("mintcream","firebrick3")), breaks = c(0,1))
vkey(cmap, "Status")
colored_bars(colors = myColors, dend = myDend, rowLabels = c("VS","AM"))

# >> attempt 2 << trying to force breaks to 0 and 1
plot(myDend)
cmap <- squash::makecmap( myStatus, n = 2,colFn = colorRampPalette(c("mintcream","firebrick3")))
vkey(cmap, "Status", skip = c(0.5))
colored_bars(colors = myColors, dend = myDend, rowLabels = c("VS","AM"))

The plots generated have the following problems, respectively :

  • The values used for colored bars are binary but the Status label shows 4 different values.

  • The breaks are well defined, but the colors linked to them are wrong

  • The values used for colored bars are binary but the Status label shows 4 different values (skip didn't do it's job)

The plots are these:

The reference for the code are available in 1, 2. First link shows how to add the labels, which didn't work for me and the second one shows how to add the color bars.


回答1:


Following dendextend vignettes will probably get what you want. I change the color a little bit as mintcream is not good on a white background.

library(magrittr)
library(dendextend)

data("mtcars")

# Create the dendrogram, use default options
dend_mtcars <- mtcars %>% 
  dist %>% 
  hclust() %>% 
  as.dendrogram

# Set the plot margin: bottom, left, top & right
par(mar = c(10, 3, 3, 4) + 0.1,
    xpd = NA) # allow content to go into outer margin 

# Plot
plot(dend_mtcars)

# Setup the color bar based on $am & $vs
the_bars_am <- ifelse(mtcars$am, "firebrick3", "beige")
the_bars_vs <- ifelse(mtcars$vs, "firebrick3", "beige")
the_bars <- cbind(the_bars_vs, the_bars_am)
colored_bars(colors = the_bars, dend = dend_mtcars, rowLabels = c("vs", "am"))

# Add the legend manually
legend("topright", legend = c('0', '1'), pch = 15, pt.cex = 3, cex = 1.5, bty = 'n',
       inset = c(-0.1, 0), # place outside
       title = "Status", 
       col = c('beige', 'firebrick3'))

Created on 2018-03-03 by the reprex package (v0.2.0).

Edit: See also here and live demo



来源:https://stackoverflow.com/questions/49088627/how-to-label-colored-bars-in-a-dendrogram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!