簡體   English   中英

使用 plot function 和 as.dendrogram object 的邊距問題

[英]Problem with margins using plot function with as.dendrogram object

我正在嘗試使用基本 R 函數和 package“dendextend”來自定義集群 plot。 首先,我使用公共 hclust() function 生成一個集群。然后我使用“dendextend”為 k=groups 定義的分支着色。 然后我使用 plot()、points() 和 text() 進行最終自定義。 要使用“deextend”的 function set() 為分支着色,集群的 class 應從“hclust”更改為“as.dendrogram”。 當我將 plot() 與 as.dendrogram 一起使用時,圖中的 plot 區域似乎縮小了,主要在 x 軸下方。 我試圖改變很多 par() arguments 但它不起作用。 盡管 package "dendextend" 提供了很多自定義,但它不允許旋轉集群對象的標簽。 下面是一些代碼和圖形。 我的母語不是英語,所以請忽略語法錯誤。

我在“Doubs.RData”中使用數據集“env”,可以在下面的鏈接中下載: enter link description here

env_scaled <- apply(env, 2, scale)

dist_env <- dist(env_scaled)

cluster_env <- hclust(dist_env, method = "ward.D2")

cluster <- as.dendrogram(cluster_env) %>% 
  set("branches_k_color", color1, k=3) %>% 
  set("labels", "") %>% 
  set("hang", -0.1) %>% 
  set("branches_lwd", 1.5

color_cluster <- c(rep("blue4", 4), rep("forestgreen", 17), rep("firebrick", 9))

plot(cluster, ylab="Euclidean distance", xlab="", sub="", main= "Cluster (Ward)")
points(seq(1:30), rep(0, 30), col=alpha(color_cluster, 0.8), 
       pch=c(rep(19, 4), rep(15,17), rep(17, 9)), cex=1.5)
text(seq(1:30), rep(-0.5,30), labels=cluster_env$order, cex=0.8, 
     col=color_cluster, font=2)

定制集群

data(USArrest)可用於重現上述示例。

dend <- USArrests %>%
  dist() %>%
  hclust(method = "ave") %>%
  as.dendrogram() %>% 
  set("labels", "")
d2 <- color_branches(dend, 5)
plot(d2)
text(seq(1:nrow(USArrests)), rep(-5, nrow(USArrests)), labels= 1:nrow(USArrests), cex=0.8, font=2)

請注意,當我們使用text() function 將標簽設置為 -5.0(y 軸)時,它不會完全顯示。

您需要在par中使用mar參數。

例如:

library(dendextend)
# ?color_branches
par(mar=c(5 + 5,4,4,2) + 0.1)

dend <- USArrests %>%
  dist() %>%
  hclust(method = "ave") %>%
  as.dendrogram()
d2 <- color_branches(dend, 5)
plot(d2)

在此處輸入圖像描述

將 +5 添加到 mar 的第一個參數會使 plot 的底部有更多空間。

暫無
暫無

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

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