R图形覆盖热图

时间:2018-02-22 17:39:08

标签: r plotly r-plotly

我有两个矩阵,A和B,我想用图表制作两个热图,并叠加它们

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

当我试图覆盖它们时,它们确实被夸大了,但是第二个热图完全掩盖了第一个热像图。

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

enter image description here

我可以改变不透明度,以便看到&#39;两个热图

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

enter image description here

但它真的不漂亮,我不能为每个热图设置不同的颜色。

是否有任何优雅的方式来覆盖它们,如下例所示?非常感谢。

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

enter image description here

1 个答案:

答案 0 :(得分:1)

我不确定是否可能,但也许你可以欺骗它。你可以尝试:

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