在 2 个树状图中保留标签和图例颜色
Preserving label and legend color across 2 dendograms
Objective:我想在为同一数据集创建的 2 个树状图中保留标签颜色和图例颜色。
我有相同的数据集(40 个观测值),它在 2 个过程(预过滤和过滤)中转换为树状图。但是,标签颜色会根据其聚类方式发生变化(因此树状图中标签的顺序会发生变化)。
这是一个代码片段:
library(dendextend)
small_mtcars <- head(mtcars)
small_mtcars
d1 = small_mtcars %>% select(mpg, cyl, disp) %>% dist() %>% hclust(method = "average") %>% as.dendrogram()
d2 = small_mtcars %>% select(mpg, cyl, disp) %>% dist() %>% hclust(method = "complete") %>% as.dendrogram()
par(mar = c(10,4,4,2) + 0.1)
# Plotting d1
test <- d1 %>%
set("labels_cex",0.7) %>%
plot(main="d1")
legend("topright", legend=unique(rownames(small_mtcars)[order.dendrogram(d1)]), cex=0.75, bty="n",
fill=seq(1,length(unique(rownames(small_mtcars)[order.dendrogram(d1)]))))
# Plotting d2
test2 <- d2 %>%
set("labels_cex",0.7) %>%
plot(main="d2")
legend("topright", legend=unique(rownames(small_mtcars)[order.dendrogram(d2)]), cex=0.75, bty="n",
fill=seq(1,length(unique(rownames(small_mtcars)[order.dendrogram(d2)]))))
d1_dendogram
d2_dendogram
根据上面的代码片段,这是我想要实现的两件事
- 两个树状图的颜色图例应该相同(在所附图像中,Valiant 模型在 d1_dendogram 中为绿色,但在 d2_dendogram 中为紫色)
- 我想用与图例相同的颜色对叶子标签进行颜色编码
提前致谢。
您的代码中有很多事情需要重新做。我已经修好了,现在可以用了。如果您有后续问题,可以 post 作为评论:)
library(dendextend)
library(dplyr)
small_mtcars <- head(mtcars) %>% select(mpg, cyl, disp)
small_mtcars
d1 = small_mtcars %>% dist() %>% hclust(method = "average") %>% as.dendrogram()
d2 = small_mtcars %>% dist() %>% hclust(method = "complete") %>% as.dendrogram()
library(colorspace)
some_colors <- rainbow_hcl(nrow(small_mtcars))
d1_col <- some_colors[order.dendrogram(d1)]
d2_col <- some_colors[order.dendrogram(d2)]
labels_colors(d1) <- d1_col
labels_colors(d2) <- d2_col
par(mfrow = c(1,2))
# Plotting d1
the_labels <- rownames(small_mtcars)
d1 %>%
set("labels_cex",0.7) %>%
plot(main="d1", xlim = c(1,9))
legend("topright", legend=the_labels, cex=0.75, bty="n",
fill=some_colors)
# Plotting d2
d2 %>%
set("labels_cex",0.7) %>%
plot(main="d2", xlim = c(1,9))
legend("topright", legend=the_labels, cex=0.75, bty="n",
fill=some_colors)
输出:
Objective:我想在为同一数据集创建的 2 个树状图中保留标签颜色和图例颜色。
我有相同的数据集(40 个观测值),它在 2 个过程(预过滤和过滤)中转换为树状图。但是,标签颜色会根据其聚类方式发生变化(因此树状图中标签的顺序会发生变化)。
这是一个代码片段:
library(dendextend)
small_mtcars <- head(mtcars)
small_mtcars
d1 = small_mtcars %>% select(mpg, cyl, disp) %>% dist() %>% hclust(method = "average") %>% as.dendrogram()
d2 = small_mtcars %>% select(mpg, cyl, disp) %>% dist() %>% hclust(method = "complete") %>% as.dendrogram()
par(mar = c(10,4,4,2) + 0.1)
# Plotting d1
test <- d1 %>%
set("labels_cex",0.7) %>%
plot(main="d1")
legend("topright", legend=unique(rownames(small_mtcars)[order.dendrogram(d1)]), cex=0.75, bty="n",
fill=seq(1,length(unique(rownames(small_mtcars)[order.dendrogram(d1)]))))
# Plotting d2
test2 <- d2 %>%
set("labels_cex",0.7) %>%
plot(main="d2")
legend("topright", legend=unique(rownames(small_mtcars)[order.dendrogram(d2)]), cex=0.75, bty="n",
fill=seq(1,length(unique(rownames(small_mtcars)[order.dendrogram(d2)]))))
d1_dendogram d2_dendogram
根据上面的代码片段,这是我想要实现的两件事
- 两个树状图的颜色图例应该相同(在所附图像中,Valiant 模型在 d1_dendogram 中为绿色,但在 d2_dendogram 中为紫色)
- 我想用与图例相同的颜色对叶子标签进行颜色编码
提前致谢。
您的代码中有很多事情需要重新做。我已经修好了,现在可以用了。如果您有后续问题,可以 post 作为评论:)
library(dendextend)
library(dplyr)
small_mtcars <- head(mtcars) %>% select(mpg, cyl, disp)
small_mtcars
d1 = small_mtcars %>% dist() %>% hclust(method = "average") %>% as.dendrogram()
d2 = small_mtcars %>% dist() %>% hclust(method = "complete") %>% as.dendrogram()
library(colorspace)
some_colors <- rainbow_hcl(nrow(small_mtcars))
d1_col <- some_colors[order.dendrogram(d1)]
d2_col <- some_colors[order.dendrogram(d2)]
labels_colors(d1) <- d1_col
labels_colors(d2) <- d2_col
par(mfrow = c(1,2))
# Plotting d1
the_labels <- rownames(small_mtcars)
d1 %>%
set("labels_cex",0.7) %>%
plot(main="d1", xlim = c(1,9))
legend("topright", legend=the_labels, cex=0.75, bty="n",
fill=some_colors)
# Plotting d2
d2 %>%
set("labels_cex",0.7) %>%
plot(main="d2", xlim = c(1,9))
legend("topright", legend=the_labels, cex=0.75, bty="n",
fill=some_colors)
输出: