| 1 |
# library(testthat) |
| 2 |
# library(dendextend) |
| 3 |
|
| 4 |
|
| 5 |
context("colored_bars") |
| 6 |
|
| 7 |
|
| 8 |
test_that("colored_bars works", { |
| 9 |
|
| 10 |
dend <- as.dendrogram(hclust(dist(mtcars))) |
| 11 |
|
| 12 |
comp_names <- unlist(lapply(rownames(mtcars), function(x) strsplit(x, " ")[[1]][[1]])) |
| 13 |
top_three <- sort(table(comp_names), decreasing = TRUE)[1:3] |
| 14 |
top_three <- sapply(names(top_three), function(x) grepl(x, labels(dend))) |
| 15 |
top_three <- as.data.frame(top_three) |
| 16 |
|
| 17 |
colorblind_friendly <- c("#1b9e77", "#d95f02", "#7570b3") |
| 18 |
colored_dataframe <- top_three |
| 19 |
|
| 20 |
for (i in 1:3) { |
| 21 |
colored_dataframe[top_three[, i], i] <- colorblind_friendly[[i]] |
| 22 |
colored_dataframe[!top_three[, i], i] <- "#00000000" |
| 23 |
} |
| 24 |
colored_dataframe_2 <- colored_dataframe |
| 25 |
colnames(colored_dataframe_2) <- NULL |
| 26 |
|
| 27 |
dend <- color_branches(dend, h = 170) |
| 28 |
dend <- color_labels(dend, h = 170) |
| 29 |
|
| 30 |
# general use |
| 31 |
plot(dend) |
| 32 |
expect_no_error( |
| 33 |
colored_bars(colored_dataframe, as.hclust(dend), horiz = FALSE, add = T, sort_by_labels_order = FALSE) |
| 34 |
) |
| 35 |
expect_no_error( |
| 36 |
colored_bars(colored_dataframe_2, as.hclust(dend), horiz = FALSE, add = T, sort_by_labels_order = FALSE) |
| 37 |
) |
| 38 |
|
| 39 |
# if horizontal |
| 40 |
plot(dend, horiz = T) |
| 41 |
expect_no_error( |
| 42 |
colored_bars(colored_dataframe, add = F, horiz = TRUE) |
| 43 |
) |
| 44 |
expect_no_error( |
| 45 |
colored_bars(colored_dataframe_2, add = F, horiz = TRUE) |
| 46 |
) |
| 47 |
|
| 48 |
# if non-dendrogram object passed in |
| 49 |
plot(dend) |
| 50 |
expect_error( |
| 51 |
colored_bars(colored_dataframe, mtcars) |
| 52 |
) |
| 53 |
# if wrong number of colors passed in |
| 54 |
expect_error( |
| 55 |
colored_bars(colored_dataframe[1:31,], dend) |
| 56 |
) |
| 57 |
|
| 58 |
}) |
| 59 |
|
| 60 |
|