ropensci/iheatmapr

add_col_barplot color legend not showing up

Opened this issue · 1 comments

First, thanks for the great package.
I'm using the package for heatmap with barplot as annotation. I noticed that "showlegend" option seems not functioning for showing color legend when barplot with coloring. Here is an example

library(RColorBrewer)
da = matrix(rnorm(200),nrow=20)
rownames(da) = paste0("A",1:20)
colnames(da) = paste0("B",1:10)

db = data.frame(id=colnames(da), value=runif(10,1,10),group=sample(letters[1:3],10,replace = T), 
            stringsAsFactors = F)
g = db$group
glev = unique(g)
colors = brewer.pal(length(glev), "Paired")
gcolor = factor(g, labels=colors)

main_heatmap(da, name = "Score", x_categorical = TRUE,y_categorical=TRUE) %>%
  add_col_labels(font = list(size = 12)) %>%
  add_row_labels(size = 0.1,font = list(size = 12), side="right")%>%
  add_col_barplot(y=db$value, color=gcolor, tracename='Group',showlegend=TRUE)

One option would be to use add_col_annotation(data.frame("Groups" = g), colors=list("Groups"=colors)) to get the legend. I try to avoid this option. Thanks in advance

Thanks @xsswang for raising the issue. This is actually due to a limitation of plotly (or at least for the version used here). What setting showlegend = TRUE is doing is saying that if you made more than one bar/line plot then those would get added to a legend. See for example:

main_heatmap(da, name = "Score", x_categorical = TRUE,y_categorical=TRUE) %>%
    add_col_labels(font = list(size = 12)) %>%
    add_row_labels(size = 0.1,font = list(size = 12), side="right")%>%
    add_col_barplot(y=db$value, color=as.character(gcolor), tracename='Group',showlegend=TRUE) %>% 
    add_row_barplot(x=1:20, tracename='bah',showlegend=TRUE) 

It doesn't actually refer to making a legend for the colors provided for that trace itself... which isn't great in terms of obvious/desired behavior.