[英]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 我的树状图?
这是我在下面的代码中用来计算距离矩阵的数据。 (为很长的输入道歉!)
# 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.