Browse code

fixed heatmap label colors

Pierre-Luc Germain authored on 13/05/2020 16:29:35
Showing 3 changed files

... ...
@@ -1,7 +1,7 @@
1 1
 Package: pipeComp
2 2
 Type: Package
3 3
 Title: pipeComp pipeline benchmarking framework
4
-Version: 0.99.34
4
+Version: 0.99.35
5 5
 Depends: R (>= 4.0)
6 6
 Authors@R: c(
7 7
 	person("Pierre-Luc", "Germain", email="[email protected]", role=c("cre","aut"), comment = c(ORCID = "0000-0003-3418-4218")), 
... ...
@@ -34,10 +34,11 @@
34 34
 #' aggregation) can be passed.
35 35
 #' @param show_heatmap_legend Passed to `Heatmap` (default FALSE)
36 36
 #' @param show_column_names Passed to `Heatmap` (default FALSE)
37
-#' @param col Colors for the heatmap. By default, will apply linear mapping (if
37
+#' @param col Colors for the heatmap, or a color-mapping function as produced by
38
+#' `colorRamp2`. If passing a vector of colors and the data is scaled, there 
39
+#' should be an odd number of colors. By default, will apply linear mapping (if
38 40
 #' the data is not scaled) or signed sqrt mapping (if scaled) on the 
39
-#' `viridisLite::inferno` palette. To disable the signed sqrt-transformation,
40
-#' simply pass `col=viridisLite::inferno(100)` or your own palette.
41
+#' `viridisLite::inferno` palette.
41 42
 #' @param font_factor A scaling factor applied to fontsizes (default 1)
42 43
 #' @param value_cols A vector of length 2 indicating the colors of the values
43 44
 #' (above and below the mean), if printed
... ...
@@ -121,7 +122,8 @@ evalHeatmap <- function( res, step=NULL, what, what2=NULL, agg.by=NULL,
121 122
   res2 <- .doscale(res, param=scale)
122 123
   ro <- .dosort(res2, reorder_rows)
123 124
   res2 <- as.matrix(res2)
124
-  cellfn <- .getCellFn(res, res2, value_format, value_cols, font_factor)
125
+  cellfn <- .getCellFn(res, res2, value_format, value_cols, font_factor, 
126
+                       scaled=!(is.logical(scale) && !scale) )
125 127
   if(is.null(title)) title <- gsub("\\.","\n",what)
126 128
   suppressWarnings({
127 129
     if(!tryCatch(is.null(row_split), error=function(e) FALSE)){
... ...
@@ -138,8 +140,9 @@ evalHeatmap <- function( res, step=NULL, what, what2=NULL, agg.by=NULL,
138 140
     }
139 141
   })
140 142
   if(is.null(name)) name <- what
141
-  if(is.null(col))
142
-    col <- .defaultColorMapping(res2, center=!(is.logical(scale) && !scale))
143
+  if(is.null(col) || !is.function(col))
144
+    col <- .defaultColorMapping(res2, center=!(is.logical(scale) && !scale),
145
+                                cols=col)
143 146
   Heatmap( res2, name=name, cluster_rows=FALSE, cluster_columns=FALSE, 
144 147
            show_heatmap_legend=show_heatmap_legend, row_order=ro,
145 148
            bottom_annotation=.ds_anno(colnames(res),anno_legend,font_factor), 
... ...
@@ -148,7 +151,7 @@ evalHeatmap <- function( res, step=NULL, what, what2=NULL, agg.by=NULL,
148 151
            row_title_gp = gpar(fontsize = (14*font_factor)),
149 152
            column_title_gp = gpar(fontsize = (14*font_factor)),
150 153
            row_names_gp = gpar(fontsize = (12*font_factor)),
151
-           column_names_gp = gpar(fontsize = (12*font_factor)), ...)
154
+           column_names_gp = gpar(fontsize = (12*font_factor)), ... )
152 155
 }
153 156
 
154 157
 .doscale <- function(res, param){
... ...
@@ -194,12 +197,13 @@ evalHeatmap <- function( res, step=NULL, what, what2=NULL, agg.by=NULL,
194 197
   rep(0,length(x))
195 198
 }
196 199
 
197
-.defaultColorMapping <- function(x, center=TRUE){
198
-  if(!center) return(viridisLite::inferno(101))
200
+.defaultColorMapping <- function(x, center=TRUE, cols=NULL){
201
+  if(is.null(cols)) cols <- viridisLite::inferno(101)
202
+  if(!center) return(cols)
199 203
   q <- max(abs(range(x, na.rm=TRUE)))
200
-  b <- c( -seq(from=sqrt(q), to=0, length.out=51)^2,
201
-          seq(from=0, to=sqrt(q), length.out=51)[-1]^2 )
202
-  colorRamp2(b, viridisLite::inferno(101))
204
+  b <- c( -seq(from=sqrt(q), to=0, length.out=floor(length(cols)/2)+1)^2,
205
+          seq(from=0, to=sqrt(q), length.out=floor(length(cols)/2)+1)[-1]^2 )
206
+  colorRamp2(b, cols)
203 207
 }
204 208
 
205 209
 #' colCenterScale
... ...
@@ -302,9 +306,14 @@ colCenterScale <- function(x, centerFn=median,
302 306
 
303 307
 #' @importFrom grid grid.text
304 308
 .getCellFn  <- function( res, res2, value_format, cols=c("black","white"), 
305
-                         font_factor=1 ){
306
-  resmid <- range(res2, na.rm=TRUE)
307
-  resmid <- resmid[1]+(resmid[2]-resmid[1])/2
309
+                         font_factor=1, scaled=TRUE ){
310
+  if(scaled){
311
+    resmid <- median(res2, na.rm=TRUE)
312
+  }else{
313
+    resmid <- range(res2, na.rm=TRUE)
314
+    resmid <- resmid[1]+(resmid[2]-resmid[1])/2
315
+  }
316
+  if(is.na(resmid)) resmid <- Inf
308 317
   function(j, i, x, y, width, height, fill){
309 318
     if(value_format=="" || is.null(value_format) || is.na(value_format))
310 319
       return(NULL)
... ...
@@ -315,7 +324,7 @@ colCenterScale <- function(x, centerFn=median,
315 324
     } 
316 325
     lab <- gsub("^1.00$","1",lab)
317 326
     lab <- gsub("^.00$","0",lab)
318
-    cols <- ifelse(res2[i,j]>resmid,cols[1],cols[2])
327
+    cols <- ifelse(res2[i,j]>=resmid,cols[1],cols[2])
319 328
     grid.text(lab, x, y, gp = gpar(fontsize = 10*font_factor, col=cols))
320 329
   }
321 330
 }
... ...
@@ -65,10 +65,11 @@ names themselves can also be passed to specify an order, or a
65 65
 
66 66
 \item{show_column_names}{Passed to `Heatmap` (default FALSE)}
67 67
 
68
-\item{col}{Colors for the heatmap. By default, will apply linear mapping (if
68
+\item{col}{Colors for the heatmap, or a color-mapping function as produced by
69
+`colorRamp2`. If passing a vector of colors and the data is scaled, there 
70
+should be an odd number of colors. By default, will apply linear mapping (if
69 71
 the data is not scaled) or signed sqrt mapping (if scaled) on the 
70
-`viridisLite::inferno` palette. To disable the signed sqrt-transformation,
71
-simply pass `col=viridisLite::inferno(100)` or your own palette.}
72
+`viridisLite::inferno` palette.}
72 73
 
73 74
 \item{font_factor}{A scaling factor applied to fontsizes (default 1)}
74 75