r-我可以根据网站标准为树状图上色吗



我真的很难让我的代码为真实数据工作!(第一个问题,如果不符合标准,请道歉(

我正试图将植被调查的结果绘制成树状图,并根据预定义的标准对树叶/标签进行着色,这似乎是可能的: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','Q1','Q2','Q2','Q2','Q3','Q3','Q3','Q4','Q4','Q4')
Species <- c('Malva','Sida','Corchorus','Tephrosia','Acacia','Triodia',
'Eucalyptus','Acacia','Triodia', 'Acacia', 'Triodia','Eucalyptus')
Presence <- as.numeric(c('1','1','1','1','1','1','1','1','1','1','1','1'))
SiteData <- data.frame(Site, Species, Presence)
Site <- c('Q1','Q2','Q3','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, c = 70, 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)

相关内容

最新更新