Search code examples
rimagecowplot

Use cowplot in R to align image() plots


I would like to align two plots in R, generated with the image() function.

Sample code:

# Load package
library(cowplot)

# Plot sample image
image <- image(matrix(rnorm(1000), 100,100))

# Align plots
plot_grid(image, image)

However, when I do it like this, the plots do not appear. Am I missing something? Or can cowplot not handle plots generated from the image function?


Solution

  • You need to do a little work to store those in your environment. If you check image you'll see it's NULL. So you'll have to record it, then plot it.

    p <- recordPlot()
    plot.new()
    image(matrix(rnorm(1000), 100,100))
    p
    
    plot_grid(p, p, nrow = 2)
    

    enter image description here