pa7 / heatmap.js

🔥 JavaScript Library for HTML5 canvas based heatmaps
https://www.patrick-wied.at/static/heatmapjs/
MIT License
6.23k stars 1.32k forks source link

Adding a character matrix to heatmapr's cellmate and ggdend objects to Rowv and Colv rather than dendrograms #265

Closed ndrubins closed 7 years ago

ndrubins commented 7 years ago

Hi, I was wondering if it will be possible to create a heatmapr object where cellnote is a character matrix rather than a numerical one?

In addition, there are situations where a use might like to manipulate the dendrogram originally obtained from hierarchically clustering the matrix on which the heatmap was built, hence passing ggdend objects to Rowv and Colv - post the user's manipulation, rather than dendrograms can be useful.

Here's an example, and how I plot everything with plotly: library(ggplot2) library(dendextend) library(plotly)

dendogram data

my.mat <- matrix(rnorm(10*100),nrow=100,ncol=10,dimnames = list(paste("g",1:100,sep=""),paste("s",1:10,sep=""))) x <- as.matrix(scale(my.mat)) dd.col <- as.dendrogram(hclust(dist(x))) dd.row <- as.dendrogram(hclust(dist(t(x))))

hover over data (cellnote)

my.hover.mat <- matrix(paste(paste(rownames(my.mat),paste("description",1:100,sep=" "),sep=":"),colnames(my.mat),signif(my.mat,3),sep="'
'"),nrow=100,ncol=10)

here I cut the row dendrogram, convert it to a ggdend object, and manipulate that

dd.col <- cut(dd.col,h=6)$upper
ggdend.col <- as.ggdend(dd.col)
leaf.heights <- dplyr::filter(ggdend.col$nodes,!is.na(leaf))$height
leaf.seqments.idx <- which(ggdend.col$segments$yend %in% leaf.heights)
ggdend.col$segments$yend[leaf.seqments.idx] <- max(ggdend.col$segments$yend[leaf.seqments.idx])
ggdend.col$segments$col[leaf.seqments.idx] <- "black"
ggdend.col$labels$label <- 1:nrow(ggdend.col$labels)

ggdend.col$labels$y <- max(ggdend.col$segments$yend[leaf.seqments.idx])
ggdend.col$labels$x <- ggdend.col$segments$x[leaf.seqments.idx]
ggdend.col$labels$col <- "black"
ggdend.col$segments$lwd <- 0.5

I also convert the column dendrogram although not cutting it

ggdend.row <- dendro_data(dd.row)
ggdend.row$labels$label <- ""
ggdend.row$labels$col <- "black"
ggdend.row$segments$lwd <- 0.5

here I create the ggplot figures for these 2 ggdend objects

py <- ggplot()+geom_segment(data=ggdend.col$segments,aes(x=x,y=y,xend=xend,yend=yend))+coord_flip()+annotate("text",size=4,hjust=1,x=ggdend.col$label$x,y=ggdend.col$label$y,label=ggdend.col$label$label,colour=ggdend.col$label$col)+labs(x="",y="")+
  theme_minimal()+theme(axis.text=element_blank(),axis.ticks=element_blank(),panel.grid=element_blank())
px <- ggplot()+geom_segment(data=ggdend.row$segments,aes(x=x,y=y,xend=xend,yend=yend))+annotate("text",size=4,hjust=1,x=ggdend.row$label$x,y=ggdend.row$label$y,label=ggdend.row$label$label,colour=ggdend.row$label$col)+labs(x="",y="")+
  labs(x="",y="")+theme_minimal()+theme(axis.text=element_blank(),axis.ticks=element_blank(),panel.grid=element_blank())

and finally plotting it all together

col.ord <- order.dendrogram(dd.col)
row.ord <- order.dendrogram(dd.row)
my.mat <- my.mat[col.ord,row.ord]
my.hover.mat <- my.hover.mat[col.ord,row.ord]

heatmap.plotly <- plot_ly() %>% add_heatmap(z=~my.mat,x=factor(colnames(my.mat),lev=colnames(my.mat)),y=factor(rownames(my.mat),lev=rownames(my.mat)),hoverinfo='text',text=my.hover.mat)
eaxis <- list(showticklabels = FALSE,showgrid = FALSE,zeroline = FALSE)
p_empty <- plot_ly(filename="r-docs/dendrogram") %>% layout(margin = list(l = 200),xaxis = eaxis,yaxis = eaxis)
subplot(px, p_empty, heatmap.plotly, py, nrows = 2, margin = 0.01)

I guess the equivalent attempt with heatmaply would be to create a heatmapr object like this:

heatmapr.obj <- heatmapr(x=my.mat,Colv=ggdend.row,Rowv=ggdend.col,cellnote=my.hover.mat)

pa7 commented 7 years ago

hi @ndrubins , are you sure this is the right repository for this issue? this is heatmap.js not heatmapr

ndrubins commented 7 years ago

Sorry about about that @pa7. I meant to post it on https://github.com/talgalili/heatmaply/issues.

Is it possible to delete this issue?

pa7 commented 7 years ago

@ndrubins no worries, I just closed it