簡體   English   中英

如何在樹形圖中標記彩色條

[英]How to label colored bars in a dendrogram

我如何為樹形圖中添加的一些彩色條添加標簽?

該代碼波紋管將顯示兩次嘗試,我瞄准的任務,這是值1鏈接到在彩色欄的標簽顏色紅色和值0,白色做。

# 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"))

生成的圖分別具有以下問題:

  • 用於彩色條的值是二進制,但Status標簽顯示4個不同的值。

  • 休息時間很明確,但與它們相關的顏色是錯誤的

  • 用於彩色條的值是二進制的,但狀態標簽顯示4個不同的值(跳過沒有做它的工作)

情節如下:

在此輸入圖像描述

在此輸入圖像描述

在此輸入圖像描述

該代碼的參考可在 1 2 第一個鏈接顯示如何添加標簽,這對我不起作用,第二個鏈接顯示如何添加顏色條。

以下dendextend小插曲可能會得到你想要的。 我改變了顏色,因為在白色背景上mintcream不好。

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'))

reprex包 (v0.2.0)於2018-03-03創建。

編輯:另見此處現場演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM