簡體   English   中英

旋轉葉叢樹狀圖中的葉子標簽

[英]Rotate leaf labels in pvclust dendrogram plot

我在R中使用pvclust軟件包執行引導式分層群集。 然后將輸出繪制為具有一些額外功能(不同的默認標題,節點的p值)的群集對象。 我在這里的一個地塊上附加了一個鏈接。

除了我需要葉子標簽水平顯示而不是垂直顯示之外,此圖正是我想要的。 據我所知,plot.hclust中沒有旋轉葉子標簽的選項。 我可以將hclust對象繪制為樹狀圖

(即plot(as.dendrogram(example$hclust), leaflab="textlike")代替plot(example)

但是葉子標簽然后打印在我似乎無法刪除的框中,並且hclust對象中節點的高度丟失了。 我在此處附加了樹狀圖的鏈接。

制作與標准plot.pvclust()輸出盡可能相似但帶有水平葉子標簽的圖的最佳方法是什么?

一種以所需方式獲取文本的方法是讓plot.dendrogram打印任何內容,而只是自己添加標簽。 由於您不提供數據,因此我將介紹一些內置數據。 默認情況下,該圖沒有為標簽留出足夠的空間,因此我將ylim設置為允許多余的空間。

set.seed(1234)
HC = hclust(dist(iris[sample(150,6),1:4]))

plot(as.dendrogram(HC), leaflab="none", ylim=c(-0.2, max(HC$height)))
text(x=seq_along(HC$labels), y=-0.2, labels=HC$labels)

具有水平標簽的樹狀圖

我編寫了一個函數,該函數繪制帶有空字符串的標准pvclust圖作為葉標簽,然后分別繪制葉標簽。

plot.pvclust2 <- function(clust, x_adj_val, y_adj_val, ...){
  # Assign the labels in the hclust object to x_labels,
  # then replace x$hclust$labels with empty strings.
  # The pvclust object will be plotted as usual, but without
  # any leaf labels.
  clust_labels <- clust$hclust$labels
  clust$hclust$labels <- rep("", length(clust_labels))

  clust_merge <- clust$hclust$merge #For shorter commands

  # Create empty vector for the y_heights and populate with height vals
  y_heights <- numeric(length = length(clust_labels))
  for(i in 1:nrow(clust_merge)){
    # For i-th merge
    singletons <- clust_merge[i,] < 0 #negative entries in merge indicate
                                      #agglomerations of singletons, and 
                                      #positive entries indicate agglomerations
                                      #of non-singletons.
    y_index <- - clust_merge[i, singletons]
    y_heights[y_index] <- clust$hclust$height[i] - y_adj_val
  }

  # Horizontal text can be cutoff by the margins, so the x_adjust moves values
  # on the left of a cluster to the right, and values on the right of a cluster
  # are moved to the left
  x_adjust <- numeric(length = length(clust_labels))
  # Values in column 1 of clust_merge are on the left of a cluster, column 2
  # holds the right-hand values
  x_adjust[-clust_merge[clust_merge[ ,1] < 0, 1]] <- 1 * x_adj_val
  x_adjust[-clust_merge[clust_merge[ ,2] < 0, 2]] <- -1 * x_adj_val

  # Plot the pvclust object with empty labels, then plot horizontal labels
  plot(clust, ...)
  text(x = seq(1, length(clust_labels)) +
         x_adjust[clust$hclust$order],
       y = y_heights[clust$hclust$order],
       labels = clust_labels[clust$hclust$order])
}

暫無
暫無

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

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