Home > other >  Can we plot heatmaps on top of the other in R?
Can we plot heatmaps on top of the other in R?

Time:04-02

I have the following empty ggplot in R, lets call that plot as (***)

a = 1:10
b = 1:10
data = data.frame(a,b)
ggplot(data, aes(a, b))

Now let us assume that we have also the following three data sets, with the corresponding heatplots

first one,

x_1 = 2:6
x_2 = 1:3
x = expand.grid(x_1,x_2)
x_values = runif(15,0,1)
data_1 = data.frame(x_1 = x[,1], x_2 = x[,2],x_values)
ggplot(data_1, aes(x_1, x_2))                             
  geom_tile(aes(fill = x_values))

second one,

y_1 = 4:7
y_2 = 2:9
y = expand.grid(y_1,y_2)
y_values = runif(32,0,1)
data_2 = data.frame(y_1 = y[,1], y_2 = y[,2],y_values)
ggplot(data_2, aes(y_1, y_2))                             
  geom_tile(aes(fill = y_values))

third one,

z_1 = 3:6
z_2 = 8:10
z = expand.grid(z_1,z_2)
z_values = runif(12,0,1)
data_3 = data.frame(z_1 = z[,1], z_2 = z[,2],z_values)
ggplot(data_3, aes(z_1, z_2))                             
  geom_tile(aes(fill = z_values))

Is there a way to plot all the three heatmaps on top of (***)? I would like to create a plot with x-axis spanning as a and y-axis spanning as b, and then plot the heatmap for the first data set, on the subspaces with x-axis spanning as x_1 and y-axis spanning as x_2, and on top of that add then add the heatmap of the second data set on the subspace with x-axis spanning as y_1 and y-axis spanning as y_2 and do that also for the third data set and to generalize that to much more many data sets.

CodePudding user response:

Maybe this is what your are looking for:

set.seed(123)

library(ggplot2)

ggplot()                           
  geom_tile(aes(x_1, x_2, fill = x_values), data_1)  
  geom_tile(aes(y_1, y_2, fill = y_values), data_2)   
  geom_tile(aes(z_1, z_2, fill = z_values), data_3)  
  scale_x_continuous(limits = range(a))  
  scale_y_continuous(limits = range(b))

  •  Tags:  
  • r
  • Related