R 彩色侧栏树状图

R 彩色侧栏树状图,r,dendrogram,dendextend,R,Dendrogram,Dendextend,最初,我试图使用Dendestend的彩色条将水平彩色侧栏添加到树状图(而不是整个热图) 下面的代码(感谢您的帮助!)运行得非常好。剩下的唯一问题是如何控制条与叶子标签的距离和条的宽度 下面是一个示例,数据和代码 数据(4个变量,5个案例) df这可以通过使用来实现 首先要安装您可以使用的最新Dendestend版本: install.packages(dendextend) 以下是使用mtcars的示例: ## mtcars example # Create the dend: dend

最初,我试图使用Dendestend的彩色条将水平彩色侧栏添加到树状图(而不是整个热图)

下面的代码(感谢您的帮助!)运行得非常好。剩下的唯一问题是如何控制条与叶子标签的距离和条的宽度

下面是一个示例,数据和代码
数据(4个变量,5个案例)


df这可以通过使用来实现

首先要安装您可以使用的最新Dendestend版本:

install.packages(dendextend)
以下是使用mtcars的示例:

## mtcars example

# Create the dend:
dend <- as.dendrogram(hclust(dist(mtcars)))

# Create a vector giving a color for each car to which company it belongs to
car_type <- rep("Other", length(rownames(mtcars)))
is_x <- grepl("Merc", rownames(mtcars))
car_type[is_x] <- "Mercedes"
is_x <- grepl("Mazda", rownames(mtcars))
car_type[is_x] <- "Mazda"
is_x <- grepl("Toyota", rownames(mtcars))
car_type[is_x] <- "Toyota"
car_type <- factor(car_type)
n_car_types <- length(unique(car_type))
cols_4 <- colorspace::rainbow_hcl(n_car_types, c = 70, l  = 50)
col_car_type <- cols_4[car_type]

# extra: showing the various clusters cuts 
k234 <- cutree(dend, k = 2:4)

# color labels by car company:
labels_colors(dend) <- col_car_type[order.dendrogram(dend)]
# color branches based on cutting the tree into 4 clusters:
dend <- color_branches(dend, k = 4)

### plots
par(mar = c(12,4,1,1))
plot(dend)
colored_bars(cbind(k234[,3:1], col_car_type), dend, rowLabels = c(paste0("k = ", 4:2), "Car Type"))

# horiz version:
par(mar = c(4,1,1,12))
plot(dend, horiz = TRUE)
colored_bars(cbind(k234[,3:1], col_car_type), dend, rowLabels = c(paste0("k = ", 4:2), "Car Type"), horiz = TRUE)
legend("topleft", legend = levels(car_type), fill = cols_4)
##mtcars示例
#创建dend:

你好,安迪,谢谢你的来信。我现在看到函数有一个问题,我将在下周修复。同时,能否请您用一个独立的树状图和组的小示例更新您的问题,以便在功能正常后为您演示该功能?谢谢。提前谢谢你!我会尽量保持简单,并使用并行,因为我的数据集有700多个变量和200多个案例。让我们使用mtcars数据。我想做的是为所有车型(特殊车型,如Merc280C、Merc240D等)创建mpg、qsec、cyl、齿轮的树状图(而不是热图),然后在下面为梅赛德斯、马自达、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰、奔驰,丰田等。因此,您可以快速看到,例如,所有梅赛德斯汽车聚集在一起。我知道我可以创建带有梅赛德斯、马自达、丰田标签的列,但将其应用于树状图,但不允许快速评估(请记住,我有200多个案例(“汽车”)。如果这不完全是您所期望的,我深表歉意!我已修复该函数以更好地工作。如果有帮助,请让我知道:)您不是在谈论mtcars(该数据没有您提到的列)。你可能在这里谈论汽车数据:但是它们与数据的链接被破坏了,所以我不能重现你的例子。如果你想让我帮你调试-我必须有一个完全可复制的例子。(请将其包含在原始问题中,而不是评论中)我知道这是一个老问题。。。但是,如何为特定类别分配特定颜色(例如马自达黄色、梅赛德斯蓝色、其他绿色、丰田红色)。我可能有一个手艺的时刻,但不知道怎么做。。。提前谢谢你@Aussieand您可以先创建一个带有代码的矢量
col\u code=c(“马自达”=“黄色”、“梅赛德斯”=“蓝色”、“其他”=“绿色”、“丰田”=“红色”)
,然后将带有汽车类型的矢量映射到这些颜色代码,执行
col\u vec=col\u code[汽车类型]
## mtcars example

# Create the dend:
dend <- as.dendrogram(hclust(dist(mtcars)))

# Create a vector giving a color for each car to which company it belongs to
car_type <- rep("Other", length(rownames(mtcars)))
is_x <- grepl("Merc", rownames(mtcars))
car_type[is_x] <- "Mercedes"
is_x <- grepl("Mazda", rownames(mtcars))
car_type[is_x] <- "Mazda"
is_x <- grepl("Toyota", rownames(mtcars))
car_type[is_x] <- "Toyota"
car_type <- factor(car_type)
n_car_types <- length(unique(car_type))
cols_4 <- colorspace::rainbow_hcl(n_car_types, c = 70, l  = 50)
col_car_type <- cols_4[car_type]

# extra: showing the various clusters cuts 
k234 <- cutree(dend, k = 2:4)

# color labels by car company:
labels_colors(dend) <- col_car_type[order.dendrogram(dend)]
# color branches based on cutting the tree into 4 clusters:
dend <- color_branches(dend, k = 4)

### plots
par(mar = c(12,4,1,1))
plot(dend)
colored_bars(cbind(k234[,3:1], col_car_type), dend, rowLabels = c(paste0("k = ", 4:2), "Car Type"))

# horiz version:
par(mar = c(4,1,1,12))
plot(dend, horiz = TRUE)
colored_bars(cbind(k234[,3:1], col_car_type), dend, rowLabels = c(paste0("k = ", 4:2), "Car Type"), horiz = TRUE)
legend("topleft", legend = levels(car_type), fill = cols_4)