talgalili / heatmaply

Interactive Heat Maps for R Using plotly
376 stars 75 forks source link

[FEATURE] Possible to have axis text in multiple colors? #264

Open mcsimenc opened 3 years ago

mcsimenc commented 3 years ago

In ggplot2 I can pass a vector of colors through theme(axis.text.x), and while it gives the warning

Vectorized input to `element_text()` is not officially supported.
Results may be unexpected or may change in future versions of ggplot2.

it still works. I see this warning when passing the ggplot2 theme axis text colors through heatmap_layers but it doesn't seem to change the colors. A single color passed this way works.

Is this possible?

Screenshot from 2021-06-17 14-58-52

Thanks!

alanocallaghan commented 3 years ago

Could you use reprex::reprex to post the code you used here?

alanocallaghan commented 3 years ago

Sorry I nuked your code somehow lol

library(heatmaply) 

# generate data 
datMat = matrix(runif(100), nrow = 10, ncol = 10) 

# try a single color 
heatmaply(datMat, heatmap_layers = theme(axis.text.y = element_text(colour = "blue")), main = "A sin
gle axis text color passed using theme() works") 

# try multiple colors 
axisColors = rep(c("red", "black"), 5) 
heatmaply(datMat, heatmap_layers = theme(axis.text.y = element_text(color = axisColors)), main = "Mu
ltiple colors passed to theme doesn't work") 

library(reshape2) 
# multiple colors works in ggplot2 

datMelt = melt(datMat) 
ggplot(datMelt, aes(x = Var1, y = Var2, fill = value)) +  
    geom_tile() +  
    theme(axis.text.y = element_text(color = c("black", "red", "black", "red", "blue"))) +  
    ggtitle("Multiple axis colors works for ggplot()")
alanocallaghan commented 3 years ago

I think the short answer is no, if ggplotly() called on the last plot doesn't work (it doesn't for me). Would be worth flagging this up in the plotly repo

mcsimenc commented 3 years ago

No, that was me lol. When I first read reprex::reprex it somehow didn't register. I deleted it and started looking into reprex

alanocallaghan commented 3 years ago

Ah fair dues. Turns out it doesn't really like HTML output anyway so I was pointing you in the wrong direction for github issues with HTMLwidgets.

mcsimenc commented 3 years ago

The only way I know how do it with plot_ly is to add multiple overlapping axes of different ranges and colors. It works for me for more than two colors too. Here is my attempt with heatmaply and the resulting error. Any suggestions? EDIT: I guess I should try installing PhantomJS...

library(heatmaply)
#> Loading required package: plotly
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
#> Loading required package: viridis
#> Loading required package: viridisLite
#> 
#> ======================
#> Welcome to heatmaply version 1.2.1
#> 
#> Type citation('heatmaply') for how to cite the package.
#> Type ?heatmaply for the main documentation.
#> 
#> The github page is: https://github.com/talgalili/heatmaply/
#> Please submit your suggestions and bug-reports at: https://github.com/talgalili/heatmaply/issues
#> Or contact: <tal.galili@gmail.com>
#> ======================
library(reprex)

# generate data
datMat = matrix(runif(100), nrow = 10, ncol = 10)

# multiple colors using plot_ly() method
heatmaply(datMat) %>%
  add_trace(yaxis = "y2", showscale = F) %>%
  layout(yaxis = list(range = list(1, 10),
                      tickfont = list(color = "red"),
                      tickvals = as.list(1:5)),
         yaxis2 = list(range = list(1, 10),
                       overlaying = 'y',
                       tickfont = list(color = "blue"),
                       tickvals = as.list(6:10)))
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
#> No scatter mode specifed:
#>   Setting the mode to markers
#>   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
#> Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...): 'x' must be atomic

Created on 2021-06-17 by the reprex package (v2.0.0)

EDIT: Same scatter mode error after PhantomJS was installed.

library(heatmaply)
#> Loading required package: plotly
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
#> Loading required package: viridis
#> Loading required package: viridisLite
#> 
#> ======================
#> Welcome to heatmaply version 1.2.1
#> 
#> Type citation('heatmaply') for how to cite the package.
#> Type ?heatmaply for the main documentation.
#> 
#> The github page is: https://github.com/talgalili/heatmaply/
#> Please submit your suggestions and bug-reports at: https://github.com/talgalili/heatmaply/issues
#> Or contact: <tal.galili@gmail.com>
#> ======================
library(reprex)

# generate data
datMat = matrix(runif(100), nrow = 10, ncol = 10)

# multiple colors using plot_ly() method
heatmaply(datMat) %>%
  add_trace(yaxis = "y2", showscale = F) %>%
  layout(yaxis = list(range = list(1, 10),
                      tickfont = list(color = "red"),
                      tickvals = as.list(1:5)),
         yaxis2 = list(range = list(1, 10),
                       overlaying = 'y',
                       tickfont = list(color = "blue"),
                       tickvals = as.list(6:10)))
#> No scatter mode specifed:
#>   Setting the mode to markers
#>   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
#> Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...): 'x' must be atomic

Created on 2021-06-17 by the reprex package (v2.0.0)