Browse code

adjust the order of legend labels

Zuguang Gu authored on 20/10/2019 09:37:25
Showing3 changed files

... ...
@@ -2,6 +2,7 @@ CHANGES in VERSION 2.1.1
2 2
 
3 3
 * `Heatmap()`: give error when heatmap has empty string as its name.
4 4
 * `anno_mark()`: text positions are correctly calculated now with rotations.
5
+* The order of legend labels are ordered by either `sort` or `levels`.
5 6
 
6 7
 ========================
7 8
 
... ...
@@ -350,8 +350,12 @@ Heatmap = function(matrix, col, name,
350 350
         warning_wrap("The input is a data frame, convert it to the matrix.")
351 351
         matrix = as.matrix(matrix)
352 352
     }
353
+    fa_level = NULL
353 354
     if(!is.matrix(matrix)) {
354 355
         if(is.atomic(matrix)) {
356
+            if(is.factor(matrix)) {
357
+                fa_level = levels(matrix)
358
+            }
355 359
             rn = names(matrix)
356 360
             matrix = matrix(matrix, ncol = 1)
357 361
             if(!is.null(rn)) rownames(matrix) = rn
... ...
@@ -503,6 +507,9 @@ Heatmap = function(matrix, col, name,
503 507
     if(ncol(matrix) > 0 && nrow(matrix) > 0) {
504 508
         if(missing(col)) {
505 509
             col = default_col(matrix, main_matrix = TRUE)
510
+            if(!is.null(fa_level)) {
511
+                col = col[fa_level]
512
+            }
506 513
             if(verbose) qqcat("color is not specified, use randomly generated colors\n")
507 514
         }
508 515
         if(is.function(col)) {
... ...
@@ -511,7 +518,11 @@ Heatmap = function(matrix, col, name,
511 518
         } else {
512 519
             if(is.null(names(col))) {
513 520
                 if(length(col) == length(unique(as.vector(matrix)))) {
514
-                    names(col) = sort(unique(as.vector(matrix)))
521
+                    if(is.null(fa_level)) {
522
+                        names(col) = sort(unique(as.vector(matrix)))
523
+                    } else {
524
+                        names(col) = fa_level
525
+                    }
515 526
                     .Object@matrix_color_mapping = ColorMapping(colors = col, name = name, na_col = na_col)
516 527
                     if(verbose) qqcat("input color is a vector with no names, treat it as discrete color mapping\n")
517 528
                 } else if(is.numeric(matrix)) {
... ...
@@ -523,7 +534,11 @@ Heatmap = function(matrix, col, name,
523 534
                     stop_wrap("`col` should have names to map to values in `mat`.")
524 535
                 }
525 536
             } else {
526
-                col = col[intersect(c(names(col), "_NA_"), as.character(matrix))]
537
+                if(is.null(fa_level)) {
538
+                    col = col[intersect(c(names(col), "_NA_"), as.character(matrix))]
539
+                } else {
540
+                    col = col[intersect(c(fa_level, "_NA_"), names(col))]
541
+                }
527 542
                 .Object@matrix_color_mapping = ColorMapping(colors = col, name = name, na_col = na_col)
528 543
                 if(verbose) qqcat("input color is a named vector\n")
529 544
             }
... ...
@@ -198,7 +198,10 @@ SingleAnnotation = function(name, value, col, fun,
198 198
 
199 199
     .Object@is_anno_matrix = FALSE
200 200
     use_mat_column_names = FALSE
201
+            
201 202
     if(!missing(value)) {
203
+        value2 = value
204
+    
202 205
         if(verbose) qqcat("@{name}: annotation value is vector/matrix\n")
203 206
         if(is.logical(value)) {
204 207
             if(is.matrix(value)) {
... ...
@@ -437,13 +440,14 @@ SingleAnnotation = function(name, value, col, fun,
437 440
             color_is_random = TRUE
438 441
             if(verbose) qqcat("@{name}: use randomly generated colors\n")
439 442
     	}
443
+
440 444
     	if(is.atomic(col)) {
441 445
     	    if(is.null(names(col))) {
442
-                if(is.factor(value)) {
443
-                    names(col) = levels(value)
446
+                if(is.factor(value2)) {
447
+                    names(col) = levels(value2)
444 448
                     if(verbose) qqcat("@{names}: add names for discrete color mapping\n")
445 449
                 } else if(length(col) == length(unique(value))) {
446
-                    names(col) = unique(value)
450
+                    names(col) = sort(unique(value))
447 451
                     if(verbose) qqcat("@{names}: add names for discrete color mapping\n")
448 452
                 } else if(is.numeric(value)) {
449 453
                     col = colorRamp2(seq(min(value, na.rm = TRUE), max(value, na.rm = TRUE), length = length(col)), col)
... ...
@@ -453,7 +457,11 @@ SingleAnnotation = function(name, value, col, fun,
453 457
             if(is.function(col)) {
454 458
                 color_mapping = ColorMapping(name = name, col_fun = col, na_col = na_col)
455 459
             } else {
456
-                col = col[intersect(c(names(col), "_NA_"), as.character(value))]
460
+                if(is.factor(value2)) {
461
+                    col = col[intersect(c(levels(value2), "_NA_"), names(col))]
462
+                } else {
463
+                    col = col[intersect(c(sort(names(col)), "_NA_"), as.character(value))]
464
+                }
457 465
         		if("_NA_" %in% names(col)) {
458 466
         			na_col = col["_NA_"]
459 467
         			col = col[names(col) != "_NA_"]