0

I have two matrices, A and B, I want to make two heatmaps with plotly, and overlay them

library(plotly)
A = matrix(c(1:8, rep(0, 8)), ncol = 4)
B = matrix(c(rep(0, 8), 1:8), ncol = 4)
PA <- plot_ly(z = A, type = "heatmap", colors = colorRamp(c("white", "green")))
PB <- plot_ly(z = B, type = "heatmap", colors = colorRamp(c("white", "red")))

When I try to overlay them, they are indeed overplayed, but the second heatmap totally masked the first one.

PA %>% add_trace(z = B, type = "heatmap")

enter image description here

I could change the opacity in order to 'see' both heatmaps

PA %>% add_trace(z = B, opacity = 0.5, type = "heatmap")

enter image description here

But it is really not beautiful, and I cannot set different colours for each heatmap.

Is there any elegant way to overlay them like the following example? thanks a lot.

p = plot_ly(x = rnorm(500), opacity = 0.6, type = "histogram") %>%
add_trace(x = rnorm(500)+1) %>%
layout(barmode="overlay")

enter image description here

Wang
  • 1,314
  • 14
  • 21

1 Answers1

2

I am not sure if it is possible, but maybe you could trick it. You could try:

ay <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE
)

PB <- PB %>% layout(yaxis = ay, xaxis = list(range = c(1.5, 3.5), dtick = 1))
PA <- PA %>% layout(yaxis = list(dtick = 1), xaxis = list(range = c(-0.5, 1.5), dtick = 1))
subplot(PA, PB, nrows = 1, shareX = TRUE, shareY = FALSE) 

enter image description here

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • 1
    Thanks a lot @MLavoie, this is not what I exactly expected. My real dataset is far more complicated. Anyway, many thanks for your kind help. – Wang Feb 23 '18 at 21:16