title: Heatmap in plotly with defined colors per category in r
body: <p>I am trying to plot a heatmap with specified colors (by category) in plotly. I asked a similar question here: <a href="https://stackoverflow.com/questions/57824941/split-up-by-category-in-plotly">&quot;Split&quot; up by category in plotly</a>.</p>
  
  <p>However, I ran into a new problem while trying a similar thing with a heatmap. My code looks like:</p>
  
  <pre><code># Test DataFrame
  test_df &lt;- data.frame(
    "weekday" = c("Fr", "Sa", "Su"),
    "time" = c("06:00:00", "12:00:00", "18:00:00"),
    "channel" = c("NBC", "CBS Drama", "ABC"),
    "colors" = c("#FCB711", "#162B48", "#AA8002"),
    "views" = c(1200, 1000, 1250)
  )
  
  plot_ly(colors = unique(as.character(test_df$colors)),  type = "heatmap") %&gt;%
    add_trace(test_df,
          x = test_df$weekday,
          y = test_df$time,
          z = test_df$views,
          type = "heatmap")
  </code></pre>
  
  <p>What I get is the following picture:
  <a href="https://i.stack.imgur.com/Vps4D.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vps4D.jpg" alt="enter image description here"></a></p>
  
  <p>The problems I have here are: <br>
  1. The colors are not the colors which I told R to use <br>
  2. I do not want a colorscale, rather the categories split up channels.</p>
  
  <p>I know there is a workaround in ggplot, and I am working on it, but I want to have it in plotly. 
  Here is what it looks like in ggplot and what I want to have in plotly (<em>I am aware of ggplotly, but that still isn't pure plotly</em>):
  <a href="https://i.stack.imgur.com/E4eBi.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E4eBi.jpg" alt="enter image description here"></a></p>
  
  <p>Here is the code for the above picture:</p>
  
  <pre><code>channel_colors &lt;- test_df %&gt;% distinct(colors) %&gt;% pull(colors)
  names(channel_colors) &lt;- test_df %&gt;% distinct(channel) %&gt;% pull(channel)
  
  p &lt;- ggplot(data = test_df,
              aes(
                x = weekday,
                y = time,
                fill = channel)) + 
    geom_tile(aes(alpha = views)) +
    scale_alpha(range = c(0.5, 1)) +
    theme_minimal() +
    scale_fill_manual(values = channel_colors) 
  
  ggplotly(p)
  </code></pre>
  
  <p>I would appreciate any help.</p>