Как добавить строковые переменные в дендрограмму с метками, окрашенными по уровню фактора? - PullRequest
0 голосов
/ 26 ноября 2018

Один из ответов на на этот вопрос Цвет кодирует метки дендограммы для подмножества набора данных радужной оболочки.То, что я хотел бы сделать, это сохранить названия строк для ярлыков, чтобы они называли setosa, virginica и т. Д. Вместе со своими цветами.

Вот код

# install.packages("dendextend")
library(dendextend)

small_iris <- iris[c(1, 51, 101, 2, 52, 102), ]
dend <- as.dendrogram(hclust(dist(small_iris[,-5])))
# Like: 
# dend <- small_iris[,-5] %>% dist %>% hclust %>% as.dendrogram

# By default, the dend has no colors to the labels
labels_colors(dend)
par(mfrow = c(1,2))
plot(dend, main = "Original dend")

# let's add some color:
colors_to_use <- as.numeric(small_iris[,5])
colors_to_use
# But sort them based on their order in dend:
colors_to_use <- colors_to_use[order.dendrogram(dend)]
colors_to_use
# Now we can use them
labels_colors(dend) <- colors_to_use
# Now each state has a color
labels_colors(dend) 
plot(dend, main = "A color for every Species")

1 Ответ

0 голосов
/ 30 ноября 2018

Вам необходимо обновить метки непосредственно перед нанесением.Например, используя labels(dend) <- small_iris[,5][order.dendrogram(dend)]

Полный код и вывод:

# install.packages("dendextend")
library(dendextend)

small_iris <- iris[c(1, 51, 101, 2, 52, 102), ]
dend <- as.dendrogram(hclust(dist(small_iris[,-5])))
# Like: 
# dend <- small_iris[,-5] %>% dist %>% hclust %>% as.dendrogram

# By default, the dend has no colors to the labels
labels_colors(dend)
par(mfrow = c(1,2))
plot(dend, main = "Original dend")

# let's add some color:
colors_to_use <- as.numeric(small_iris[,5])
colors_to_use
# But sort them based on their order in dend:
colors_to_use <- colors_to_use[order.dendrogram(dend)]
colors_to_use
# Now we can use them
labels_colors(dend) <- colors_to_use
# Now each state has a color
labels_colors(dend) 

### UPDATE <--------------------------------
labels(dend) <- small_iris[,5][order.dendrogram(dend)]


plot(dend, main = "A color for every Species")

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...