giac
giac

Reputation: 4299

R - stack several Heatmap on top of each others

I would like to stack several heatmap on top of each others, a bit like this kind of plot :

enter image description here

So, I have two distributions who look like this : enter image description here

I created two heatmap from these distributions :

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

Any idea how I could stack these two Heatmap and how to give them a 3d aspect ?

data :

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

Upvotes: 2

Views: 474

Answers (1)

Steven Beaupré
Steven Beaupré

Reputation: 21621

Using plotly:

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

Upvotes: 3

Related Questions