ropensci / iheatmapr

Complex, interactive heatmaps in R
https://docs.ropensci.org/iheatmapr
Other
267 stars 36 forks source link

feature request: discrete heatmap colorscheme #5

Open mschilli87 opened 7 years ago

mschilli87 commented 7 years ago

Dear Alicia,

I came across a roadblock using iheatmapr and would like to ~request~ suggest a feature:


Consider the following example:

############
# packages #
############

require(magrittr)
require(reshape2)
require(ggplot2)
require(viridis)

########
# data #
########

cor.dat <-
  mtcars %>%
  scale %>%
  cor %>%
  melt(varnames = c("x", "y"), value.name = "cor")

str(cor.dat)

# 'data.frame':   121 obs. of  3 variables:                                                                              
#  $ x  : Factor w/ 11 levels "mpg","cyl","disp",..: 1 2 3 4 5 6 7 8 9 10 ...                                            
#  $ y  : Factor w/ 11 levels "mpg","cyl","disp",..: 1 1 1 1 1 1 1 1 1 1 ...                                             
#  $ cor: num  1 -0.852 -0.848 -0.776 0.681 ...

####################
# continuous scale #
####################

cor.dat %>%
  ggplot(aes(x, y, fill = cor)) +
    geom_tile() +
    scale_fill_viridis("r") +
    theme_bw(base_size = 18) +
    ggtitle("continuous scale", subtitle = "mtcars (unclustered)")

##################
# discrete scale #
##################

cor.dat %>%
  ggplot(aes(x, y, fill = cut(cor, 3))) +
#-------------------------^^^^...^^^^
    geom_tile() +
    scale_fill_viridis("r", discrete = TRUE) +
#-------------------------^^^^^^^^^^^^^^^^^
    theme_bw(base_size = 18) +
    ggtitle("discrete scale", subtitle = "mtcars (unclustered)")
#------------^^^^^^^^

While I can use iheatmapr to generate a heatmap using a continuous scale colorscheme, I did not manage to make it color the heatmap in a discrete fashion.


In many cases, using a more 'rough' colorscheme can greatly improve the visibility of clusters/groups (in this case correlated, un-correlated and anti-correlated).


The best I could come up with for iheatmapr would be to round/discretize the input data, but this would affect the clustering and not just the visualization. Also, I want to see the real (unrounded) values on mouseover.


As I show above, this is easy to achive in basic ggplot2 by using cut. So I assume it shouldn't be too hard to add this (IMHO very useful) functionality to iheatmapr.


So what are chances of seeing this implemented in iheatmapr?

With some hints on where to start, I would be willing to assist and try to get a pull request but I don't really know the internals of the classes implementing iheatmaprs modular system.

Cheers, Marcel

AliciaSchep commented 7 years ago

Hi Marcel,

I think this would be a great additional feature. I have been thinking that it would be good to have the possibility of a discrete colorscale when you have discrete input data for the main heatmap, but hadn't thought of having a discrete colorscale for continuous data (while preserving continuous nature of data). I'm pretty swamped right now, so not sure whether I have the time to implement this myself anytime soon. I'll try to get back to you with some additional thoughts for how to implement this though, as I would welcome a pull request, but for now some initial thoughts:

there currently are two classes, "DiscreteColorbar" and "ContinuousColorbar" that are used for making colorbars, and a number of methods for those classes. I am not sure if "DiscreteColorbar" could be adapted for use with continuous data, I think it might be necessary to add a third class "DiscreteContinuousColorbar" for discrete colorbars for continuous data (probably inheriting from "ContinuousColorbar"). The "colorscale" method in colors.R would do something different for this new class than for ContinuousColorbar.

Cheers, Alicia

alanocallaghan commented 7 years ago

fyi https://github.com/plotly/plotly.js/issues/1747