-1

I have several data with the same structures but I want to show them under each other with the same title. As an example

mat1 = matrix(rnorm(80, 2), 8, 10)
mat1 = rbind(mat1, matrix(rnorm(40, -2), 4, 10))
rownames(mat1) = paste0("R", 1:12)
colnames(mat1) = paste0("C", 1:10)
mat2 = matrix(rnorm(60, 2), 12, 10)
mat2 = rbind(mat2, matrix(rnorm(60, -2), 12, 10))
rownames(mat2) = paste0("RR", 1:24)

They are two data with two sizes. I want to plot them side by side and with the same order or row name

As an example a figure below but please discard the dendogram or classification lines because I want to keep the order to be the same as the row name

enter image description here

for your information I have read this and tried to use info from this post to display two heatmaps in same pdf side by side in R

nik
  • 2,500
  • 5
  • 21
  • 48
  • `mat2` is 24 rows, not 12 (the last line) – De Novo Mar 06 '18 at 21:41
  • also, if you're using a heatmap with a dendrogram, as your image suggests, you will not be able to or want to keep the row order the same. Rows (and columns) are re-ordered according to the hierarchical clustering. – De Novo Mar 06 '18 at 21:43
  • @Dan Hall sure , the first one has 12 row and the second one has 24 rows. This is the problem when you want to plot them side by side – nik Mar 06 '18 at 21:44
  • @Dan Hall great point. I don't care about the dendogram. so discard the dendorgram just the order – nik Mar 06 '18 at 21:44
  • the whole point of the heatmap image though, is to show how the data clusters. Do you just want a color image of your data values? – De Novo Mar 06 '18 at 21:46
  • @Dan Hall yes the color also is showing the intensity of values. Cluster analysis is for when you want to cluster samples or .... together . If you have a solution which uses the clustering too, I don't mind to use that solution and you can have any order you want but two heatmap under each other – nik Mar 06 '18 at 21:49

1 Answers1

0
set.seed(123)
mat1 = matrix(rnorm(80, 2), 8, 10)
mat1 = rbind(mat1, matrix(rnorm(40, -2), 4, 10))
rownames(mat1) = paste0("R", 1:12)
colnames(mat1) = paste0("C", 1:10)
mat2 = matrix(rnorm(60, 2), 12, 10)
mat2 = rbind(mat2, matrix(rnorm(60, -2), 12, 10))
rownames(mat2) = paste0("RR", 1:24)
par(mfrow = c(1, 2))
image(t(mat1)[,nrow(mat1):1], main = "mat1")
image(t(mat2)[,nrow(mat2):1], main = "mat2")

two color images next to eachother

This produces a color image of each matrix. I put them next to eachother, because if you use Rstudio, putting them one on top of eachother makes it impossible to see in the RStudioGD. If you want them on top of eachother, change the call to par to par(mfrow = c(2, 1)).

As far as I was taught, a heatmap is a color image of a data matrix that has undergone hierarchical cluster analysis (both the matrix and the transpose) and has been reordered accordingly, so that you can get a sense for any underlying structure. So this isn't a heatmap in that sense, but from your comments you don't want a heatmap as I understand it.

If you're curious about the usage in image (i.e., why image(t(mat1)[,nrow(mat1):1]), it's because of a very bizarre design decision in the image function that rotates the data 90 degrees before plotting it. So the re-ordering makes it match up to the way we would generally display the matrix on the page.

De Novo
  • 7,120
  • 1
  • 23
  • 39
  • is it possible to add their row names there or some of their row names ? – nik Mar 06 '18 at 22:29
  • Because of the design decision of image, that's a big pain. You can add column numbers easily enough, but it's going to complain if make the y-axis tic labels descending (because it expects them to be ascending). – De Novo Mar 06 '18 at 23:14
  • I hope this answer was helpful to you. – De Novo Mar 06 '18 at 23:14