我正在处理生态数据(不同硅藻物种在沉积物岩心不同深度的丰度百分比),并希望将结果与树状图一起绘制,该树状图表示层次聚类分析(CONISS)的结果,我将使用该树状图将岩心划分为生态区。
这是我试图实现的事情类型的一个例子:

我已经成功地运行了聚类分析,但正在努力按照我想要的方式绘制树状图。每当我绘制树状图时,它都会按顺序(as shown here)绘制树状图的叶子。但是,我需要从沉积核心顶部按深度绘制树叶,以便在我检查过的每个核心深度都有一片树叶,并且在树状图中有缺失样本的间隙(as shown here)。(请注意,此处没有显示y轴,因为树状图将连接到已包含y轴的图表的其余部分。)
我设法通过rioja包创建了后一个图,它使用plot.chclust并为xvar参数提供了叶子的深度。然而,我已经构建了我图表的其余部分(物种丰度数据,PCA结果等)。使用ggplot (以及tidypaleo包的帮助),然后使用cowplot组合它的各个方面。我需要能够通过ggplot创建树状图,以便将其添加到我的主图中。我已经研究了ggdendro和dendextend包,但找不到使用它们根据深度绘制树状图的方法。这个是可能的吗?这些包有没有我不知道的做这件事的功能?我开始研究这些包以及as.dendrogram的源代码,看看是否可以找到一种方法来修改函数,以按深度绘制树叶,但这超出了我的技能水平。我想知道是否有人有一个解决方案,使我能够绘制我的树状图深度在我的沉积核心,而不是按顺序?
我的数据
This is the data that I have used to calculate the distance matrix in the code below. (很抱歉用了这么长的dput!)
我绘制树状图的代码
# 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)发布于 2020-12-09 05:43:43
实际深度映射到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)我们得到:

https://stackoverflow.com/questions/65207033
复制相似问题