微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以根据现场条件为树状图着色吗

如何解决我可以根据现场条件为树状图着色吗

我真的很难使我的代码用于真实数据! (第一个问题,如果不符合标准,请您道歉)

我正在尝试将植被调查的结果绘制为树状图,并用预先定义的标准对叶子/标签进行着色,这似乎是可行的: https://cran.r-project.org/web/packages/dendextend/vignettes/FAQ.html 我的标签站点名称,颜色应该是植被类型/位置(例如,如果我的一半站点位于小溪中,而另一半不在小溪中,则能够查看这些位置在整个树状图上是如何分隔的,这将非常有用)

我的“虚拟”代码完成了我想做的事情(3种蔬菜类型,4个站点

{library(tidyverse)
  library(vegan)
  library(ggplot2)
  library(cluster)
library(dendextend)}

Site <- c('Q1','Q1','Q2','Q3','Q4','Q4')
Species <- c('Malva','Sida','Corchorus','Tephrosia','Acacia','Triodia','Eucalyptus','Eucalyptus')
Presence <- as.numeric(c('1','1','1'))
SiteData <- data.frame(Site,Species,Presence)
Site <- c('Q1','Q4')
VegType <- c('VT7','VT2','VT5','VT5')
VegTypes <- data.frame(Site,VegType)
SiteWide <- pivot_wider(SiteData,names_from = Species,values_from = Presence,values_fill = list(Presence=0))
SiteWide <- SiteWide %>% column_to_rownames(var="Site") %>% as.data.frame()

dend <- as.dendrogram(hclust(dist(SiteWide)))
plot(dend)

#VegType <- rep("Other",length(rownames(VegTypes)))
is_x <- grepl("VT7",rownames(VegTypes))
VegType[is_x] <- "VT7"
is_x <- grepl("VT2",rownames(VegTypes))
VegType[is_x] <- "VT2"
is_x <- grepl("VT5",rownames(VegTypes))
VegType[is_x] <- "VT5"
VegType <- factor(VegType)
n_VegType <- length(unique(VegType))
cols_3 <- colorspace::rainbow_hcl(n_VegType,c = 70,l  = 50)
col_veg_type <- cols_3[VegType]

#color labels by vegetation type:
labels_colors(dend) <- col_veg_type[order.dendrogram(dend)]
plot(dend)

问题出在我的真实数据中(16种蔬菜类型,约100个站点)...我认为我的问题在最后,将16种颜色分配给蔬菜类型的顺序是正确的。关于如何正确编码的任何建议将不胜感激! (另外,我认为这是很多方法中的一种-我尝试了很多方法,但没有任何效果,所以这是我的最后选择!):(预先感谢!

#Code to load in vegetation types
VegTypes <- read.csv("VegTypeQuads.csv")
VegType <- rep("Other",length(rownames(VegTypes)))
#VegTypes$VegType <- as.factor(VegTypes$VegType)
is_x <- grepl("VT01",rownames(VegTypes))
VegType[is_x] <- "VT01"
is_x <- grepl("VT02",rownames(VegTypes))
VegType[is_x] <- "VT02"
is_x <- grepl("VT03",rownames(VegTypes))
VegType[is_x] <- "VT03"
is_x <- grepl("VT04",rownames(VegTypes))
VegType[is_x] <- "VT04"
is_x <- grepl("VT05",rownames(VegTypes))
VegType[is_x] <- "VT05"
is_x <- grepl("VT06",rownames(VegTypes))
VegType[is_x] <- "VT06"
is_x <- grepl("VT07",rownames(VegTypes))
VegType[is_x] <- "VT07"
is_x <- grepl("VT08",rownames(VegTypes))
VegType[is_x] <- "VT08"
is_x <- grepl("VT09",rownames(VegTypes))
VegType[is_x] <- "VT09"
is_x <- grepl("VT10",rownames(VegTypes))
VegType[is_x] <- "VT10"
is_x <- grepl("VT11",rownames(VegTypes))
VegType[is_x] <- "VT11"
is_x <- grepl("VT12",rownames(VegTypes))
VegType[is_x] <- "VT12"
is_x <- grepl("VT13",rownames(VegTypes))
VegType[is_x] <- "VT13"
is_x <- grepl("VT14",rownames(VegTypes))
VegType[is_x] <- "VT14"
is_x <- grepl("VT15",rownames(VegTypes))
VegType[is_x] <- "VT15"
is_x <- grepl("VT16",rownames(VegTypes))
VegType[is_x] <- "VT16"
VegType <- factor(VegType)
n_VegType <- length(unique(VegType))
cols_16 <- colorspace::rainbow_hcl(n_VegType,l  = 50)
col_veg_type <- cols_16[VegType]

labels_colors(dend) <- col_veg_type[order.dendrogram(dend)] #this doesn't seem to do anything to my large data set

cl <- hclust(vegdist(data))
dend = as.dendrogram(cl)
VegTypes$VegType[cl$order]   #this gives me the veg types in the correct order (i.e. order that the corresponding site occurs in the dendrogram) 

plot(dend)

#not sure how to get the veg type colour into the dendrogram though!

解决方法

我确实解决了这个问题。使用真实数据时,它是作为数据框导入的,因此我必须将其作为矢量输入:

SiteID <- as.vector(VegTypes$SiteID)
VegType <- as.vector(VegTypes$VegType)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。