简体   繁体   中英

R plotly overlay heatmaps

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")

在此处输入图片说明

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

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

在此处输入图片说明

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")

在此处输入图片说明

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) 

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM