| 1 |
# library(testthat) |
| 2 |
# library(dendextend) |
| 3 |
|
| 4 |
|
| 5 |
context("colored_dots") |
| 6 |
|
| 7 |
|
| 8 |
test_that("zero_range works", { |
| 9 |
|
| 10 |
x <- c(1) |
| 11 |
expect_true( |
| 12 |
zero_range(x) |
| 13 |
) |
| 14 |
x <- c(1, 2, 3) |
| 15 |
expect_error( |
| 16 |
zero_range(x) |
| 17 |
) |
| 18 |
x <- c(1, NA) |
| 19 |
expect_true( |
| 20 |
is.na(zero_range(x)) |
| 21 |
) |
| 22 |
x <- c(1, 1) |
| 23 |
expect_true( |
| 24 |
zero_range(x) |
| 25 |
) |
| 26 |
x <- c(-Inf, Inf) |
| 27 |
expect_false( |
| 28 |
zero_range(x) |
| 29 |
) |
| 30 |
x <- c(0, 1) |
| 31 |
expect_false( |
| 32 |
zero_range(x) |
| 33 |
) |
| 34 |
x <- c(1,2) |
| 35 |
expect_false( |
| 36 |
zero_range(x) |
| 37 |
) |
| 38 |
|
| 39 |
}) |
| 40 |
|
| 41 |
|
| 42 |
test_that("rescale works", { |
| 43 |
|
| 44 |
x <- c(1, 2) |
| 45 |
expect_identical( |
| 46 |
rescale(x), |
| 47 |
c(0, 1) |
| 48 |
) |
| 49 |
x <- c(1, 1) |
| 50 |
expect_identical( |
| 51 |
rescale(x), |
| 52 |
c(0.5, 0.5) |
| 53 |
) |
| 54 |
|
| 55 |
}) |
| 56 |
|
| 57 |
|
| 58 |
test_that("rotated_str_dim works", { |
| 59 |
|
| 60 |
plot.new() |
| 61 |
expect_identical( |
| 62 |
round(rotated_str_dim("input string"), 3), |
| 63 |
c(xh = 0.037, yh = 0.171) |
| 64 |
) |
| 65 |
|
| 66 |
}) |
| 67 |
|
| 68 |
|
| 69 |
test_that("max_labels_height works", { |
| 70 |
|
| 71 |
plot.new() |
| 72 |
expect_identical( |
| 73 |
round(max_labels_height("input string"), 2), |
| 74 |
0.17 |
| 75 |
) |
| 76 |
|
| 77 |
}) |
| 78 |
|
| 79 |
|
| 80 |
test_that("colored_dots works", { |
| 81 |
|
| 82 |
dend <- as.dendrogram(hclust(dist(mtcars))) |
| 83 |
|
| 84 |
comp_names <- unlist(lapply(rownames(mtcars), function(x) strsplit(x, " ")[[1]][[1]])) |
| 85 |
top_three <- sort(table(comp_names), decreasing = TRUE)[1:3] |
| 86 |
top_three <- sapply(names(top_three), function(x) grepl(x, labels(dend))) |
| 87 |
top_three <- as.data.frame(top_three) |
| 88 |
|
| 89 |
colorblind_friendly <- c("#1b9e77", "#d95f02", "#7570b3") |
| 90 |
colored_dataframe <- top_three |
| 91 |
|
| 92 |
for (i in 1:3) { |
| 93 |
colored_dataframe[top_three[, i], i] <- colorblind_friendly[[i]] |
| 94 |
colored_dataframe[!top_three[, i], i] <- "#00000000" |
| 95 |
} |
| 96 |
colored_dataframe_2 <- colored_dataframe |
| 97 |
colnames(colored_dataframe_2) <- NULL |
| 98 |
|
| 99 |
dend <- color_branches(dend, h = 170) |
| 100 |
dend <- color_labels(dend, h = 170) |
| 101 |
|
| 102 |
# general use |
| 103 |
plot(dend) |
| 104 |
expect_no_error( |
| 105 |
colored_dots(colored_dataframe, as.hclust(dend), horiz = FALSE, add = T, sort_by_labels_order = FALSE) |
| 106 |
) |
| 107 |
expect_no_error( |
| 108 |
colored_dots(colored_dataframe_2, as.hclust(dend), horiz = FALSE, add = T, sort_by_labels_order = FALSE) |
| 109 |
) |
| 110 |
|
| 111 |
# if horizontal |
| 112 |
plot(dend, horiz = T) |
| 113 |
expect_no_error( |
| 114 |
colored_dots(colored_dataframe, add = F, horiz = TRUE) |
| 115 |
) |
| 116 |
expect_no_error( |
| 117 |
colored_dots(colored_dataframe_2, add = F, horiz = TRUE) |
| 118 |
) |
| 119 |
|
| 120 |
# if non-dendrogram object passed in |
| 121 |
plot(dend) |
| 122 |
expect_error( |
| 123 |
colored_dots(colored_dataframe, mtcars) |
| 124 |
) |
| 125 |
# if wrong number of colors passed in |
| 126 |
expect_error( |
| 127 |
colored_dots(colored_dataframe[1:31,], dend) |
| 128 |
) |
| 129 |
|
| 130 |
}) |
| 131 |
|
| 132 |
|