How to put two heatmaps in row in R

Viewed 119

I want to plot two heatmaps in a row. I use par for that. It works fine for plot but it does not for heatmap function, why?

This works as expected:

> par(mfrow=c(1,2))
> plot(iris$Sepal.Length,iris$Sepal.Width)
> plot(iris$Sepal.Length,iris$Petal.Width)

This not:

> par(mfrow=c(1,2))
> heatmap(as.matrix(iris[1:3,1:3]))
> heatmap(as.matrix(iris[2:4,1:3]))

Result: only one heatmap is plotted. Expected result: two heatmaps in a row.

1 Answers

According to the documentation ?heatmap:

heatmap() uses layout and draws the image in the lower right corner of a 2x2 layout. Consequentially, it can not be used in a multi column/row layout, i.e., when par(mfrow = *) or (mfcol = *) has been called.

So try:

nf <- layout(matrix(c(1, 2), ncol = 2), 1, 1)

image(as.matrix(iris[1:3,1:3]))
image(as.matrix(iris[2:4,1:3]))

You can inspect the layout with layout.show():

layout.show(nf)

enter image description here

Related