dendrogram 位於 stats 包(package)。 說明
類"dendrogram" 提供處理tree-like 結構的通用函數。它旨在替代分層聚類和分類/回歸樹中的類似函數,以便所有這些都可以使用相同的引擎來繪製或切割樹。
用法
as.dendrogram(object, ...)
## S3 method for class 'hclust'
as.dendrogram(object, hang = -1, check = TRUE, ...)
## S3 method for class 'dendrogram'
as.hclust(x, ...)
## S3 method for class 'dendrogram'
plot(x, type = c("rectangle", "triangle"),
center = FALSE,
edge.root = is.leaf(x) || !is.null(attr(x,"edgetext")),
nodePar = NULL, edgePar = list(),
leaflab = c("perpendicular", "textlike", "none"),
dLeaf = NULL, xlab = "", ylab = "", xaxt = "n", yaxt = "s",
horiz = FALSE, frame.plot = FALSE, xlim, ylim, ...)
## S3 method for class 'dendrogram'
cut(x, h, ...)
## S3 method for class 'dendrogram'
merge(x, y, ..., height,
adjust = c("auto", "add.max", "none"))
## S3 method for class 'dendrogram'
nobs(object, ...)
## S3 method for class 'dendrogram'
print(x, digits, ...)
## S3 method for class 'dendrogram'
rev(x)
## S3 method for class 'dendrogram'
str(object, max.level = NA, digits.d = 3,
give.attr = FALSE, wid = getOption("width"),
nest.lev = 0, indent.str = "",
last.str = getOption("str.dendrogram.last"), stem = "--",
...)
is.leaf(object)
參數
object |
任何R可以成為類之一的對象 |
x, y |
類 |
hang |
數字標量,指示如何根據其父母的高度計算葉子的高度;請參閱 |
check |
邏輯指示是否應檢查 |
type |
情節類型。 |
center |
邏輯性;如果 |
edge.root |
邏輯性;如果為 true,則繪製一條到根節點的邊。 |
nodePar |
用於節點的 |
edgePar |
用於邊 |
leaflab |
指定葉子如何標記的字符串。默認 |
dLeaf |
一個數字,指定用戶坐標中葉尖與其標簽之間的距離。如果默認為 |
horiz |
邏輯指示是否應水平繪製樹狀圖。 |
frame.plot |
邏輯指示是否應在圖周圍繪製一個框,請參閱 |
h |
樹被砍伐的高度。 |
height |
兩個樹狀圖應合並的高度。如果未指定(或 |
adjust |
確定是否應調整葉值的字符串。默認值 |
xlim, ylim |
繪圖的可選 x- 和 y-limits 傳遞給 |
..., xlab, ylab, xaxt, yaxt |
圖形參數或其他方法的參數。 |
digits |
指定打印精度的整數,請參閱 |
max.level, digits.d, give.attr, wid, nest.lev, indent.str |
|
last.str, stem |
用於 |
細節
樹狀圖直接表示為嵌套列表,其中每個組件對應於樹的一個分支。因此,樹 z 的第一個分支是 z[[1]] ,相應子樹的第二個分支是 z[[1]][[2]] ,或更短的 z[[c(1,2)]] 等。樹的每個節點都攜帶有效繪圖或切割所需的一些信息作為屬性,其中隻有葉子的 members 、 height 和 leaf 是必填的:
members-
樹枝上的葉子總數
height-
繪製節點的數字非負高度。
midpoint-
節點距分支左邊界(最左邊的葉子)的數字水平距離(所有葉子之間的單位 1)。這用於
plot(*, center = FALSE)。 label-
特點;節點的標簽
x.member-
對於
cut()$upper,前成員的數量;更一般地,替代用於 ‘horizontal’ 的members組件(當horiz = FALSE時,否則 ‘vertical’)對齊。 edgetext-
特點;通向節點的邊的標簽
nodePar-
指定
points繪圖的 node-specific 屬性的命名列表(長度為 1 的組件),請參閱上麵的nodePar參數。 edgePar-
一個命名列表(長度為 1 的組件),指定
segments繪製通向節點的邊的屬性,以及繪製edgetext(如果可用),請參閱上麵的edgePar參數。 leaf-
邏輯上,如果
TRUE,則該節點是樹的葉子。
cut.dendrogram() 返回一個包含組件 $upper 和 $lower 的列表,第一個是原始樹的截斷版本,也是類 dendrogram ,後者是一個列表,其中包含通過切割樹獲得的分支,每個分支都是 dendrogram 。
"dendrogram" 對象有 [[ 、 print 和 str 方法,其中第一個方法(提取)確保選擇 sub-branches 保留類,即返回樹狀圖,即使隻是葉子。另一方麵,[(單括號)提取返回底層列表結構。
類 "hclust" 的對象可以使用方法 as.dendrogram() 轉換為類 "dendrogram" ,並且從 R 2.13.0 開始,還有一個 as.hclust() 方法作為逆方法。
rev.dendrogram 僅返回具有反轉節點的樹狀圖 x,另請參閱 reorder.dendrogram 。
merge(x, y, ...)方法將兩個或多個樹狀圖合並成一個新的樹狀圖,其中x和y(以及可選的進一步參數)作為分支。請注意,之前R3.1.2、adjust = "none"被隱式使用,當樹狀圖來自時,這是無效的as.dendrogram(hclust(..)).
nobs(object) 返回葉子總數(members 屬性,見上文)。
is.leaf(object) 返回邏輯,指示 object 是否是葉子(最簡單的樹狀圖)。
plotNode() 和 plotNodeLimit() 是輔助函數。
警告
樹形圖上的某些操作(例如merge())利用了遞歸。對於深樹,可能需要增加 options("expressions") :如果這樣做,您可能需要盡可能將 C 堆棧大小 (Cstack_info()[["size"]] ) 設置為大於默認值。
注意
plot():-
使用
type = "triangle"時,center = TRUE通常看起來更好。 str(d):-
如果您確實想查看內部結構,請改用
str(unclass(d))。
例子
require(graphics); require(utils)
hc <- hclust(dist(USArrests), "ave")
(dend1 <- as.dendrogram(hc)) # "print()" method
str(dend1) # "str()" method
str(dend1, max.level = 2, last.str = "'") # only the first two sub-levels
oo <- options(str.dendrogram.last = "\\") # yet another possibility
str(dend1, max.level = 2) # only the first two sub-levels
options(oo) # .. resetting them
op <- par(mfrow = c(2,2), mar = c(5,2,1,4))
plot(dend1)
## "triangle" type and show inner nodes:
plot(dend1, nodePar = list(pch = c(1,NA), cex = 0.8, lab.cex = 0.8),
type = "t", center = TRUE)
plot(dend1, edgePar = list(col = 1:2, lty = 2:3),
dLeaf = 1, edge.root = TRUE)
plot(dend1, nodePar = list(pch = 2:1, cex = .4*2:1, col = 2:3),
horiz = TRUE)
## simple test for as.hclust() as the inverse of as.dendrogram():
stopifnot(identical(as.hclust(dend1)[1:4], hc[1:4]))
dend2 <- cut(dend1, h = 70)
## leaves are wrong horizontally in R 4.0 and earlier:
plot(dend2$upper)
plot(dend2$upper, nodePar = list(pch = c(1,7), col = 2:1))
## dend2$lower is *NOT* a dendrogram, but a list of .. :
plot(dend2$lower[[3]], nodePar = list(col = 4), horiz = TRUE, type = "tr")
## "inner" and "leaf" edges in different type & color :
plot(dend2$lower[[2]], nodePar = list(col = 1), # non empty list
edgePar = list(lty = 1:2, col = 2:1), edge.root = TRUE)
par(op)
d3 <- dend2$lower[[2]][[2]][[1]]
stopifnot(identical(d3, dend2$lower[[2]][[c(2,1)]]))
str(d3, last.str = "'")
## to peek at the inner structure "if you must", use '[..]' indexing :
str(d3[2][[1]]) ## or the full
str(d3[])
## merge() to join dendrograms:
(d13 <- merge(dend2$lower[[1]], dend2$lower[[3]]))
## merge() all parts back (using default 'height' instead of original one):
den.1 <- Reduce(merge, dend2$lower)
## or merge() all four parts at same height --> 4 branches (!)
d. <- merge(dend2$lower[[1]], dend2$lower[[2]], dend2$lower[[3]],
dend2$lower[[4]])
## (with a warning) or the same using do.call :
stopifnot(identical(d., do.call(merge, dend2$lower)))
plot(d., main = "merge(d1, d2, d3, d4) |-> dendrogram with a 4-split")
## "Zoom" in to the first dendrogram :
plot(dend1, xlim = c(1,20), ylim = c(1,50))
nP <- list(col = 3:2, cex = c(2.0, 0.75), pch = 21:22,
bg = c("light blue", "pink"),
lab.cex = 0.75, lab.col = "tomato")
plot(d3, nodePar= nP, edgePar = list(col = "gray", lwd = 2), horiz = TRUE)
addE <- function(n) {
if(!is.leaf(n)) {
attr(n, "edgePar") <- list(p.col = "plum")
attr(n, "edgetext") <- paste(attr(n,"members"),"members")
}
n
}
d3e <- dendrapply(d3, addE)
plot(d3e, nodePar = nP)
plot(d3e, nodePar = nP, leaflab = "textlike")
也可以看看
dendrapply 用於將函數應用於每個節點。 order.dendrogram 和reorder.dendrogram ;進一步,labels方法。
相關用法
- R dendrapply 將函數應用於樹狀圖的所有節點
- R density 核密度估計
- R deriv 簡單表達式的符號和算法導數
- R decompose 移動平均線的經典季節性分解
- R deviance 模型偏差
- R delete.response 修改術語對象
- R df.residual 剩餘自由度
- R dummy.coef 提取原始編碼中的係數
- R dist 距離矩陣計算
- R diffinv 離散積分:差分的逆
- R stlmethods STL 對象的方法
- R medpolish 矩陣的中值波蘭(穩健雙向分解)
- R naprint 調整缺失值
- R summary.nls 總結非線性最小二乘模型擬合
- R summary.manova 多元方差分析的匯總方法
- R formula 模型公式
- R nls.control 控製 nls 中的迭代
- R aggregate 計算數據子集的匯總統計
- R kruskal.test Kruskal-Wallis 秩和檢驗
- R quade.test 四方測試
- R plot.stepfun 繪製階躍函數
- R alias 查找模型中的別名(依賴項)
- R qqnorm 分位數-分位數圖
- R eff.aovlist 多層方差分析的計算效率
- R pairwise.t.test 成對 t 檢驗
注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 General Tree Structures。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
