R - 将几个热图叠加在彼此之上

时间:2016-07-13 11:14:09

标签: r ggplot2 heatmap

我想把几张热图堆叠在一起, 有点像这样的情节:

enter image description here

所以,我有两个看起来像这样的发行版: enter image description here

我从这些发行版创建了两个热图:

library(dplyr) 
library(ggplot2) 

dt %>% ggplot(aes(thours, dom)) + geom_tile(aes(fill = d), colour = "white") + 
  scale_fill_gradient(low = "white", high = "blue") + theme_minimal() 

dt %>% ggplot(aes(thours, pay)) + geom_tile(aes(fill = p), colour = "white") + 
  scale_fill_gradient(low = "white", high = "red") + theme_minimal() 

First distribution

Second distribution

知道如何堆叠这两个Heatmap以及如何给它们3d方面吗?

数据:

 dt = structure(list(thours = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24), p =     c(0.0265858993774188, 
0.0514050143025408, 0.10911997307757, 0.24238600033653, 0.396096247686354, 
0.472572774692916, 0.495961635537607, 0.486118122160525, 0.394750126198889, 
0.379606259464917, 0.466851758371193, 0.45557799091368, 0.402742722530708, 
0.286050816086152, 0.16128218071681, 0.0923775870772337, 0.0760558640417298, 
0.0657075551068484, 0.0538448594985697, 0.0371866060911997,     0.0289416119804812, 
0.02221100454316, 0.0205283526838297, 0.0202759549049302), d = c(0.00370183409052667, 
0.0108531044926805, 0.044253743900387, 0.0971731448763251, 0.124432104997476, 
0.141595153962645, 0.165404677772169, 0.164310954063604, 0.144708059902406, 
0.137725054686185, 0.145381120646138, 0.151522799932694, 0.16464748443547, 
0.193757361601885, 0.200235571260306, 0.171546357058725, 0.110381961972068, 
0.0588086824835941, 0.0334006394077065, 0.0133770822816759, 0.00563688372875652, 
0.00286050816086152, 0.00193504963822985, 0.0021874474171294), 
dom = c("dom", "dom", "dom", "dom", "dom", "dom", "dom", 
"dom", "dom", "dom", "dom", "dom", "dom", "dom", "dom", "dom", 
"dom", "dom", "dom", "dom", "dom", "dom", "dom", "dom"), 
pay = c("pay", "pay", "pay", "pay", "pay", "pay", "pay", 
"pay", "pay", "pay", "pay", "pay", "pay", "pay", "pay", "pay", 
"pay", "pay", "pay", "pay", "pay", "pay", "pay", "pay")), class = "data.frame", .Names = c("thours", 
"p", "d", "dom", "pay"), row.names = c(NA, -24L))

1 个答案:

答案 0 :(得分:3)

使用情节:

z1 = matrix(df$thours, nrow = 24) %*% matrix(df$d, nrow = 1)
z2 = matrix(df$thours, nrow = 24) %*% matrix(df$p, nrow = 1)

library(plotly)
plot_ly(z = z1, type = "surface", showscale = FALSE) %>%
  add_trace(z = z2, type = "surface", showscale = FALSE, opacity = 0.98)

enter image description here