| 1 |
context("Coloring tree's labels") |
| 2 |
|
| 3 |
|
| 4 |
test_that("Leaves color is NULL", { |
| 5 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 6 |
dend <- as.dendrogram(hc) |
| 7 |
|
| 8 |
# Defaults: |
| 9 |
expect_equal(labels_colors(dend), NULL) |
| 10 |
}) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
test_that("Setting and extracting leaves color", { |
| 16 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 17 |
dend <- as.dendrogram(hc) |
| 18 |
|
| 19 |
# let's add some color: |
| 20 |
labels_colors(dend) <- 2:4 |
| 21 |
# getting it (without labels) |
| 22 |
expect_equal(labels_colors(dend, labels = FALSE), 2:4) |
| 23 |
# getting it (with labels) |
| 24 |
expect_equal(labels_colors(dend), structure(2:4, .Names = c("Arizona", "Alabama", "Alaska"))) |
| 25 |
|
| 26 |
# if 'value' parameter missing |
| 27 |
dendextend_options("warn", T) |
| 28 |
expect_warning( |
| 29 |
`labels_colors<-`(dend) |
| 30 |
) |
| 31 |
dendextend_options("warn", F) |
| 32 |
|
| 33 |
# if nodePar attribute is empty |
| 34 |
attr(dend[[1]], "nodePar") <- list() |
| 35 |
expect_warning( |
| 36 |
labels_colors(dend) <- c() |
| 37 |
) |
| 38 |
|
| 39 |
}) |
| 40 |
|
| 41 |
|
| 42 |
test_that("Setting and extracting leaves color (some are NA)", { |
| 43 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 44 |
dend <- as.dendrogram(hc) |
| 45 |
|
| 46 |
# let's add some color: |
| 47 |
labels_colors(dend) <- c(2, NA, 4) |
| 48 |
# getting it (without labels) |
| 49 |
expect_equal(labels_colors(dend, labels = FALSE), c(2, NA, 4)) |
| 50 |
# getting it (with labels) |
| 51 |
expect_equal(labels_colors(dend), structure(c(2, NA, 4), .Names = c("Arizona", "Alabama", "Alaska"))) |
| 52 |
}) |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
test_that("Color recycling", { |
| 57 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 58 |
dend <- as.dendrogram(hc) |
| 59 |
|
| 60 |
# changing color to black, see that recycling works |
| 61 |
dendextend_options("warn", TRUE) |
| 62 |
expect_warning(labels_colors(dend) <- 1) |
| 63 |
dendextend_options("warn", FALSE) |
| 64 |
expect_equal(labels_colors(dend, labels = FALSE), rep(1, 3)) |
| 65 |
}) |
| 66 |
|
| 67 |
|
| 68 |
test_that("Color removing", { |
| 69 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 70 |
dend <- as.dendrogram(hc) |
| 71 |
|
| 72 |
labels_colors(dend) <- 2:4 |
| 73 |
|
| 74 |
# removing color (and the nodePar completely - if it has no other attributed but lab.col) |
| 75 |
expect_warning(labels_colors(dend) <- NULL) |
| 76 |
expect_equal(labels_colors(dend), NULL) |
| 77 |
}) |
| 78 |
|
| 79 |
|
| 80 |
test_that("Can't color a non-dendrogram object", { |
| 81 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 82 |
|
| 83 |
expect_error(labels_colors(hc)) |
| 84 |
expect_error(labels_colors(hc) <- NULL) |
| 85 |
}) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
test_that("Can color the labels of super flat trees", { |
| 91 |
dend_flat <- structure(list( |
| 92 |
structure(88L, members = 1L, height = 0, label = "mel Ch m pe", leaf = TRUE, value = 83), |
| 93 |
structure(list( |
| 94 |
structure(92L, members = 1L, height = 0, label = "mel Ch f fo", leaf = TRUE, value = 84), |
| 95 |
structure(list( |
| 96 |
structure(90L, members = 1L, height = 0, label = "mel Ch f he", leaf = TRUE, value = 85), |
| 97 |
structure(list( |
| 98 |
structure(84L, label = "mel Ch m he", members = 1L, height = 0, leaf = TRUE, value = 86), |
| 99 |
structure(86L, label = "mel Ch m fo", members = 1L, height = 0, leaf = TRUE, value = 87) |
| 100 |
), members = 2L, midpoint = 0.5, height = 0, value = 86.5) |
| 101 |
), members = 3L, midpoint = 0.75, height = 0, value = 85.75) |
| 102 |
), members = 4L, midpoint = 0.875, height = 0, value = 84.875) |
| 103 |
), members = 5L, midpoint = 0.9375, height = 0.0697674418604651, value = 83.9375, class = "dendrogram") |
| 104 |
### didn't work in dendextend 0.17.2 |
| 105 |
# dend_flat %>% color_labels %>% plot |
| 106 |
# dend_flat %>% color_labels(col = 1:2) %>% plot # doesn't works |
| 107 |
|
| 108 |
# no colors by default |
| 109 |
expect_null(dend_flat %>% labels_colors()) |
| 110 |
# but now we do get colors: |
| 111 |
dend_col_1 <- dend_flat %>% |
| 112 |
color_labels() %>% |
| 113 |
labels_colors() %>% |
| 114 |
unname() |
| 115 |
expect_identical( |
| 116 |
dend_col_1, |
| 117 |
c("#CC476B", "#917600", "#009232", "#008FB7", "#A352D1") |
| 118 |
) |
| 119 |
dend_col_2 <- dend_flat %>% |
| 120 |
color_labels(col = 1:2) %>% |
| 121 |
labels_colors() %>% |
| 122 |
unname() |
| 123 |
expect_identical( |
| 124 |
dend_col_2, |
| 125 |
c(1L, 2L, 1L, 2L, 1L) |
| 126 |
) |
| 127 |
# dput(dend_col_2) |
| 128 |
# dput(dend_col_2) |
| 129 |
}) |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
test_that("Using set leaves_col still allows for set labels_colors", { |
| 135 |
dend <- USArrests[1:5, ] %>% |
| 136 |
dist() %>% |
| 137 |
hclust("ave") %>% |
| 138 |
as.dendrogram() |
| 139 |
dend <- dend %>% |
| 140 |
# set("leaves_pch", c(4, 1)) %>% |
| 141 |
set("leaves_col", c(5, 1)) %>% |
| 142 |
set("labels_colors", c(3, 1)) |
| 143 |
|
| 144 |
# plot(dend) |
| 145 |
# str(unclass(dend)) |
| 146 |
# dput(unname(labels_colors(dend))) |
| 147 |
|
| 148 |
expect_identical(unname(labels_colors(dend)), c(3, 1, 3, 1, 3)) |
| 149 |
}) |
| 150 |
|
| 151 |
|
| 152 |
test_that("labels_cex<- works", { |
| 153 |
|
| 154 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 155 |
dend <- as.dendrogram(hc) |
| 156 |
|
| 157 |
labels_cex(dend) <- 1.5 |
| 158 |
expect_identical( |
| 159 |
attributes(dend[[1]])$nodePar$lab.cex, |
| 160 |
1.5 |
| 161 |
) |
| 162 |
|
| 163 |
# if non dend object passed in |
| 164 |
expect_error( |
| 165 |
labels_cex(hc) <- 1.5 |
| 166 |
) |
| 167 |
|
| 168 |
}) |
| 169 |
|
| 170 |
|
| 171 |
test_that("color_unique_labels works", { |
| 172 |
|
| 173 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 174 |
dend <- as.dendrogram(hc) |
| 175 |
|
| 176 |
capture.output( |
| 177 |
result <- color_unique_labels(dend) |
| 178 |
) |
| 179 |
expect_identical( |
| 180 |
attributes(result[[1]])$nodePar$lab.col, |
| 181 |
"#7DB0DD" |
| 182 |
) |
| 183 |
|
| 184 |
}) |
| 185 |
|
| 186 |
|
| 187 |
# library(testthat) |