簡體   English   中英

plot 樹狀圖如何按給定值(沉積物芯中的深度)而不是按順序排列?

[英]How do I plot dendrogram leaves by a given value (depth in a sediment core) rather than in sequential order?

我正在處理生態數據(沉積岩芯中不同深度處不同硅藻物種的豐度百分比),並希望將結果與代表分層聚類分析 (CONISS) 結果的樹狀圖一起 plot,我將使用該樹狀圖來拆分核心進入生態區。

這是我試圖實現的事情類型的一個例子:

我已經成功地運行了聚類分析,但我正在努力以我想要的方式 plot 樹狀圖。 每當我 plot 樹狀圖時,它都會按順序繪制樹狀圖的葉子(如此處所示)。 但是,我需要從沉積物核心頂部到 plot 深度的葉子,以便在我檢查過的核心的每個深度都有一片葉子,因此在樹狀圖中我有缺失樣本的間隙(如此處顯示)。 (注意這里沒有顯示 y 軸,因為樹狀圖將連接到圖中的 rest,其中已經包含 y 軸。)

我設法使用 plot.chclust 通過 rioja package 創建了后者plot.chclust並為xvar參數提供葉子的深度。 然而,我已經用 ggplot(以及 tidypaleo 包的幫助)構建了我的圖表的 rest(物種豐度數據、PCA 結果等),然后使用 cowplot 結合了它的各個方面。 我需要能夠通過 ggplot 創建樹狀圖,以便將其添加到我的主圖中。 我已經調查了 ggdendro 和 dendextend 包,但找不到根據深度使用這些樹形圖 plot 的方法。 這可能嗎? 這些軟件包是否有一個我不知道的 function 來執行此操作? 我開始研究這些包以及as.dendrogram的源代碼,看看我是否能找到一種方法將函數修改為 plot 葉子的深度,但這超出了我的技能水平。 我想知道是否有人有一個解決方案可以讓我通過沉積物核心的深度而不是按順序來 plot 我的樹狀圖?

我的數據

這是我在下面的代碼中用來計算距離矩陣的數據。 (為很長的輸入道歉!)

我的代碼 plot 樹狀圖

# Load requirements
library(vegan) # designdist and chclust
library(rioja) # plot.chclust
library(ggplot2)
library(ggdendro) # dendro_data
library(dplyr)
library(tibble)

# Calculate distance matrix
dist_matrix <- designdist(coniss_data, method = "A+B-2*J", terms = "quadratic")

# Run cluster analysis
coniss_results <- chclust(dist_matrix, method = "coniss")


# Plot with rioja ---------------------------------------------------------

# Isolate depths
coniss_depths <- coniss_data %>%
  rownames_to_column("depth") %>%
  mutate(depth = as.numeric(depth)) %>%
  select(., "depth")

# Plot
plot(
  coniss_results,
  hang = -0.01, # make leaves extend to 0 and labels hang down from there
  horiz = TRUE, # rotate plot
  x.rev = TRUE, # reverse depths
  xvar = coniss_depths$depth, # plot leaves by depth of sediment core
  labels = FALSE,
  cex.axis = 0.8,
  tcl = -0.1
)


# Plot with ggdendro ------------------------------------------------------

# Extract dendrogram data
ddata <- dendro_data(coniss_results, type = "rectangle")

# Plot
ggplot(segment(ddata)) +
  geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
  coord_flip() +
  scale_y_continuous(expand = c(0, 0)) +
  scale_x_reverse(breaks = NULL,
                  labels = NULL) +
  labs(x = "",
       y = "Total sum of squares") +
  theme_classic(8)

實際深度映射到ddata$labels中的 x 值,因此您可以將 x 值反向映射到實際深度。 一個方便的方法是使用approxfun

new_x <- approxfun(ddata$labels$x, as.numeric(as.character(ddata$labels$label))) 
ddata$segments$x <- new_x(ddata$segments$x)
ddata$segments$xend <- new_x(ddata$segments$xend)

所以現在使用相同的繪圖代碼:

ggplot(segment(ddata)) +
  geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
  coord_flip() +
  scale_y_continuous(expand = c(0, 0)) +
  scale_x_reverse(breaks = NULL,
                  labels = NULL) +
  labs(x = "",
       y = "Total sum of squares") +
  theme_classic(8)

我們得到:

在此處輸入圖像描述

暫無
暫無

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

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