diff --git a/DESCRIPTION b/DESCRIPTION index 01dbf97..c865047 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: tidyHeatmap Title: A Tidy Implementation of Heatmap -Version: 1.10.1 +Version: 1.10.3 Authors@R: c(person(given = "Stefano", family = "Mangiola", diff --git a/R/methods.R b/R/methods.R index e9cac27..aebd9c6 100644 --- a/R/methods.R +++ b/R/methods.R @@ -1,3 +1,6 @@ +# Specify undefined global variables for R CMD check +utils::globalVariables(c("shape", "size", "text", ":=")) + #the class definition InputHeatmap<-setClass( "InputHeatmap", diff --git a/R/utilities.R b/R/utilities.R index c2e3d28..3ddf29c 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -723,7 +723,14 @@ get_top_left_annotation = function(.data_, .column, .row, .abundance, annotation # If it is a list of colors else - colorRampPalette(palette_annotation$discrete[[.y]])(length(unique(.x))) %>% setNames(unique(.x)) + if (is(.x, "factor")) { + color_vector <- palette_annotation$discrete[[.y]] %>% setNames(levels(.x)) + color_vector[!is.na(names(color_vector))] + + } else { + colorRampPalette(palette_annotation$discrete[[.y]])(length(unique(.x))) %>% setNames(unique(.x)) + } + } else if (.x %>% class %in% c("integer", "numerical", "numeric", "double")){ # If is colorRamp diff --git a/tests/testthat/_snaps/tests/grouped-annotated-heatmap-2.svg b/tests/testthat/_snaps/tests/grouped-annotated-heatmap-2.svg index 3b94b1c..40c04e4 100644 --- a/tests/testthat/_snaps/tests/grouped-annotated-heatmap-2.svg +++ b/tests/testthat/_snaps/tests/grouped-annotated-heatmap-2.svg @@ -906,15 +906,15 @@ untreated1 paired-end - - + + single-read - - + + condition Intracellular @@ -1227,6 +1227,6 @@ treated untreated - + diff --git a/tests/testthat/_snaps/tests/grouped-custom-color-both.svg b/tests/testthat/_snaps/tests/grouped-custom-color-both.svg index a081570..c9a681b 100644 --- a/tests/testthat/_snaps/tests/grouped-custom-color-both.svg +++ b/tests/testthat/_snaps/tests/grouped-custom-color-both.svg @@ -911,18 +911,18 @@ - - - - + + + + condition - - - - - - - + + + + + + + type Intracellular @@ -1235,11 +1235,11 @@ treated untreated - + type paired-end single-read - - + + diff --git a/tests/testthat/_snaps/tests/legend.svg b/tests/testthat/_snaps/tests/legend.svg index 209c4a6..d638904 100644 --- a/tests/testthat/_snaps/tests/legend.svg +++ b/tests/testthat/_snaps/tests/legend.svg @@ -812,46 +812,46 @@ UBR - - + + - - - - - - - - - + + + + + + + + + - - - - + + + + - + - - - - - - - + + + + + + + - - - - - - + + + + + + - - - - + + + + Cell type diff --git a/tests/testthat/_snaps/tests/multi-type.svg b/tests/testthat/_snaps/tests/multi-type.svg index 73bf03d..acf29f5 100644 --- a/tests/testthat/_snaps/tests/multi-type.svg +++ b/tests/testthat/_snaps/tests/multi-type.svg @@ -912,10 +912,10 @@ treated3 treated2 treated1 - - - - + + + + @@ -1333,6 +1333,6 @@ treated untreated - + diff --git a/tests/testthat/_snaps/tests/show-heatmap-legend.svg b/tests/testthat/_snaps/tests/show-heatmap-legend.svg index 7085813..0eba09d 100644 --- a/tests/testthat/_snaps/tests/show-heatmap-legend.svg +++ b/tests/testthat/_snaps/tests/show-heatmap-legend.svg @@ -911,18 +911,18 @@ - - - - + + + + condition - - - - - - - + + + + + + + type Intracellular @@ -1120,11 +1120,11 @@ treated untreated - + type paired-end single-read - - + + diff --git a/tests/testthat/_snaps/tests/size-annotation.svg b/tests/testthat/_snaps/tests/size-annotation.svg index 5a942ac..60871f2 100644 --- a/tests/testthat/_snaps/tests/size-annotation.svg +++ b/tests/testthat/_snaps/tests/size-annotation.svg @@ -812,46 +812,46 @@ UBR - - + + - - - - - - - - - + + + + + + + + + - - - - + + + + - + - - - - - - - + + + + + + + - - - - - - + + + + + + - - - - + + + + Cell type Cell type E @@ -859,9 +859,9 @@ M T - - - + + + UBR 11400 11420 diff --git a/tests/testthat/_snaps/tests/tile-factor-custom-palette.svg b/tests/testthat/_snaps/tests/tile-factor-custom-palette.svg new file mode 100644 index 0000000..edaddf7 --- /dev/null +++ b/tests/testthat/_snaps/tests/tile-factor-custom-palette.svg @@ -0,0 +1,1048 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +sample +symbol + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Npc2b +CR33318 +Pgant9 +Pvf1 +Ama +Cyp6w1 +stet +nemy +firl +Sema2a +Src64B +CG9119 +CG5895 +CG32335 +NimC4 +CG9503 +bou +LpR2 +Ant2 +Cht6 +SP1173 +CG6356 +CG10013 +CG5418 +Kaz-m1 +ppk10 +Pkd2 +CG12880 +CG8785 +Tsp68C +CG1124 +dpr9 +CG31909 +Slob +Glut1 +snoRNA:Psi28S-3342 +snoRNA:Or-CD8 +Prx2540-1 +CG9782 +Acbp2 +CG30116 +CG18128 +mthl6 +Cyp9b1 +Pde6 +comm3 +CG12116 +NimC2 +CG9164 +CG34330 +CG14625 +CAHbeta +Cyt-b5-r +TwdlF +SPARC +Sox100B +TwdlU +sesB +CG3770 +Kal1 +CG15695 +MtnA +CG8500 +Picot +Hml +gas +CG3168 +CG1544 +Rgk1 +CG32407 +CG16713 +twz +treated1 +treated3 +treated2 +untreated3 +untreated4 +untreated2 +untreated1 + + + + + + + +group +count normalised adjusted log +-2 +-1 +0 +1 +2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +group +first_level +second_level +third_level +fourth_level + + + + + + diff --git a/tests/testthat/_snaps/tests/wrap-heatmap.svg b/tests/testthat/_snaps/tests/wrap-heatmap.svg index af3527c..00cfca6 100644 --- a/tests/testthat/_snaps/tests/wrap-heatmap.svg +++ b/tests/testthat/_snaps/tests/wrap-heatmap.svg @@ -40,6 +40,15 @@ + + + + + + + + + diff --git a/tests/testthat/tests.R b/tests/testthat/tests.R index 699f7e0..f3a59b9 100644 --- a/tests/testthat/tests.R +++ b/tests/testthat/tests.R @@ -665,6 +665,29 @@ test_that("tile colorRamp2 palette",{ }) +test_that("annotation tile factor colour order",{ + + # If factor levels correctly interpreted, colour palette should read in order: + # "first_level", "second_level", "third_level", "fourth_level" + + p = + tidyHeatmap::pasilla |> + distinct(sample) |> + mutate(group = rep(c("first_level", "third_level", "second_level", "fourth_level"), length.out = n())) |> + mutate(group = factor(group, levels = c("first_level", "second_level", "third_level", "fourth_level"))) |> + right_join(tidyHeatmap::pasilla, by = "sample") |> + tidyHeatmap::heatmap( + .column = sample, + .row = symbol, + .value = `count normalised adjusted log`, + scale = "row" + ) |> + annotation_tile(group, c("#FF004B", "#FF00FF", "#9700FF", "#2000FF")) + + vdiffr::expect_doppelganger("tile factor custom palette", p) + +}) + test_that("patchwork padding",{