1

This is somewhat of a related question to this post:

I have gene expression data for which I'd like to generate a heatmap in plotly.

Data:

require(permute)
set.seed(1)
mat <- rbind(cbind(matrix(rnorm(2500,2,1),nrow=25,ncol=500),matrix(rnorm(2500,-2,1),nrow=25,ncol=500)),
             cbind(matrix(rnorm(2500,-2,1),nrow=25,ncol=500),matrix(rnorm(2500,2,1),nrow=25,ncol=500)))
rownames(mat) <- paste("g",1:50,sep=".")
colnames(mat) <- paste("s",1:1000,sep=".")
hc.col <- hclust(dist(t(mat)))
dd.col <- as.dendrogram(hc.col)
col.order <- order.dendrogram(dd.col)
hc.row <- hclust(dist(mat))
dd.row <- as.dendrogram(hc.row)
row.order <- order.dendrogram(dd.row)
mat <- mat[row.order,col.order]

Color the data by intervals of expression:

require(RColorBrewer)
mat.intervals <- cut(mat,breaks=6)
interval.mat <- matrix(mat.intervals,nrow=50,ncol=1000,dimnames=list(rownames(mat),colnames(mat)))
interval.cols <- brewer.pal(6,"Set2")
names(interval.cols) <- levels(mat.intervals)
require(reshape2)
interval.df <- reshape2::melt(interval.mat,varnames=c("gene","sample"),value.name="expr")
interval.cols2 <- rep(interval.cols, each=ncol(mat))
color.df <- data.frame(range=c(0:(2*length(interval.cols)-1)),colors=c(0:(2*length(interval.cols)-1)))
color.df <- setNames(data.frame(color.df$range,color.df$colors),NULL)
for (i in 1:(2*length(interval.cols))) {
  color.df[[2]][[i]] <- interval.cols[[(i + 1) / 2]]
  color.df[[1]][[i]] <-  i/(2*length(interval.cols))-(i %% 2)/(2*length(interval.cols))
}

Plot with plotly:

require(ggplotly)

heatmap.plotly <- plot_ly(z=c(interval.df$expr),x=interval.df$sample,y=interval.df$gene,colors=interval.cols2,type="heatmap",colorscale=color.df,
        colorbar=list(title="score",tickmode='array',tickvals=c(1:6),ticktext=names(interval.cols),len=0.2,outlinecolor="white",bordercolor="white",borderwidth=5,bgcolor="white"))

Which gives: enter image description here

Now I'm trying to customize the x-axis ticks. Say that interval.df$sample[1:500] correspond to cluster "A" and interval.df$sample[501:1000] to cluster "B". I'd like to have 2 x-axis ticks located at 250 and 750 (the middle of the x-axis range of each cluster), and the tick text to be "A" and "B", respectively.

According to the documentation, I thought this would do it:

heatmap.plotly <- plot_ly(z=c(interval.df$expr),x=interval.df$sample,y=interval.df$gene,colors=interval.cols2,type="heatmap",colorscale=color.df,
                          colorbar=list(title="score",tickmode='array',tickvals=c(1:6),ticktext=names(interval.cols),len=0.2,outlinecolor="white",bordercolor="white",borderwidth=5,bgcolor="white")) %>%
  layout(xaxis=list(title="Cluster",tickmode="array",tickvals=c(250,750),ticktext=c("A","B")))

But I see no ticks on the x-axis.

This:

heatmap.plotly <- plot_ly(z=c(interval.df$expr),x=interval.df$sample,y=interval.df$gene,colors=interval.cols2,type="heatmap",colorscale=color.df,
                              colorbar=list(title="score",tickmode='array',tickvals=c(1:6),ticktext=names(interval.cols),len=0.2,outlinecolor="white",bordercolor="white",borderwidth=5,bgcolor="white")) %>%
      layout(xaxis=list(title="Cluster",tick0=250,dtick=500,nticks=2,ticktext=c("A","B")))

prints interval.df$sample[c(250,750)] rather than c("A","B"), though in the desired location:

enter image description here

Community
  • 1
  • 1
dan
  • 6,048
  • 10
  • 57
  • 125

1 Answers1

2

I think the tickvals argument needs to correspond to the existing x-axis tick labels rather than to their tick locations as this seems to solve my problem:

tick.vals <- c(unique(dplyr::select(dplyr::filter(interval.df,cluster=="A"),sample))$sample[floor(nrow(unique(dplyr::select(dplyr::filter(interval.df,cluster=="A"),sample)))/2)],
               unique(dplyr::select(dplyr::filter(interval.df,cluster=="B"),sample))$sample[floor(nrow(unique(dplyr::select(dplyr::filter(interval.df,cluster=="B"),sample)))/2)])
tick.text <- c("A","B")

> tick.vals
[1] "s.158" "s.655"

Then:

heatmap.plotly <- plot_ly(z=c(interval.df$expr),x=interval.df$sample,y=interval.df$gene,colors=interval.cols2,type="heatmap",colorscale=color.df,
                          colorbar=list(title="score",tickmode="array",tickvals=c(1:6),ticktext=names(interval.cols),len=0.2,outlinecolor="white",bordercolor="white",borderwidth=5,bgcolor="white")) %>%
  layout(xaxis = list(title = 'Cluster',tickmode = 'array',tickvals = tick.vals,ticktext = tick.text))

Gives: enter image description here

dan
  • 6,048
  • 10
  • 57
  • 125