Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aheatmap() color and breaks #12

Closed
slowkow opened this issue Jun 16, 2014 · 4 comments
Closed

aheatmap() color and breaks #12

slowkow opened this issue Jun 16, 2014 · 4 comments

Comments

@slowkow
Copy link

slowkow commented Jun 16, 2014

In the function aheatmap(), the color and breaks parameters are
implemented incorrectly.

My goal is to use the color black, for example, for NA (or 0) values and
palette colors for all other values. Currently, that seems to be impossible
because the color and breaks parameters do not work correctly.

This code works as expected:

mat2 = matrix(runif(15), nrow=5)
aheatmap(mat2, color = 'YlOrBr:3')

This code produces a heatmap with 50 colors, despite my effort to get
3 colors:

aheatmap(mat2,
         color = brewer.pal(n = 3, name = 'YlOrBr'))

This code also fails because only the first 3 of the 50 colors are used:

aheatmap(mat2,
         color = brewer.pal(n = 3, name = 'YlOrBr'),
         breaks = seq(min(mat2), max(mat2), length.out = 3))

(Some of) the code with the incorrect implementation is in colorcode.R:

https://github.com/renozao/NMF/blob/master/R/colorcode.R#L213

Notice the line with if( is_NA(n) ) n <- 50. This ensures that the
expression n <- length(x) will never be executed. This explains
why I see 50 colors instead of the number I want.

@renozao
Copy link
Owner

renozao commented Jul 12, 2014

Thanks for reporting this. This is sort of already fixed in the development version (see below on how to install).
You should be able to get the right number of bins with breaks = 3L:

# force using 3 bins
aheatmap(mat2, color = brewer.pal(n = 3, name = 'YlOrBr'), breaks = 3L)
# or (as you said) slightly different: use 3 bins across full YlOrBr palette
aheatmap(mat2, color = 'YlOrBr:3')

Note that breaks = 3L (integer) means "use 3 bins", which is not equivalent to breaks = 3 (double) that means "center color scale at the numeric value 3".

With respect to using black for NA and 0, this should do the trick, the idea being that you define a black color bin that gathers only zero values:

mat2 = matrix(runif(15), nrow=5)
mat2[1:3] <- NA
mat2[6:8] <- 0
nbins <- 3
aheatmap(mat2, col = c('black', brewer.pal(n = nbins, name = 'YlOrBr')), breaks = c(0, seq(min(mat2, na.rm = TRUE) + .Machine$double.eps, max(mat2, na.rm = TRUE), length.out = nbins+1)), Colv = NA, Rowv = NA)

# for NA values as well
mat2[is.na(mat2)] <- 0
aheatmap(mat2, col = c('black', brewer.pal(n = nbins, name = 'YlOrBr')), breaks = c(0, seq(min(mat2, na.rm = TRUE) + .Machine$double.eps, max(mat2, na.rm = TRUE), length.out = nbins+1)), Colv = NA, Rowv = NA)

rplot

rplot01

Installing from github without Latex:

install_github("renozao/NMF", quick = TRUE)
# or development version
install_github("renozao/NMF", ref = 'devel', quick = TRUE)

@renozao
Copy link
Owner

renozao commented Jul 12, 2014

I added argument na.color to deal with NA values, so you can now do (development version):

mat2 = matrix(runif(15), nrow=5)
mat2[6:8] <- 0
mat2[1:3] <- NA
# No color
aheatmap(mat2, Colv = NA, Rowv = NA)
# NA values only
aheatmap(mat2, na.color = 'black', Colv = NA, Rowv = NA)
# zeros as NA
aheatmap(mat2, na.color = list('black', 0), Colv = NA, Rowv = NA)
# range of values as NA
aheatmap(mat2, na.color = list('black', c(0, .5)), Colv = NA, Rowv = NA)

nmf2
nmf3
nmf4
nmf2

It is still in development stage though, because the scale is not yet adapted to show the na.color.

@renozao
Copy link
Owner

renozao commented Jun 17, 2015

@slowkow is the issue solved and can be closed?
Thanks

@slowkow
Copy link
Author

slowkow commented Jun 17, 2015

Yes, thank you for the code examples! That was very helpful. I appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants