简体   繁体   中英

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?

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)

在此输入图像描述

If you want to use cowplot for base-R plots, I highly recommend using the current development version of cowplot. In that version, you can simply turn your image code into a formula (by adding ~ in front) and it will work.

library(cowplot)
#> 
#> 
#> *******************************************************
#> Note: cowplot does not change the default ggplot2 theme
#> anymore. To recover the previous behavior, execute:
#>   theme_set(theme_cowplot())
#> *******************************************************

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

# Align plots
plot_grid(image, image)

Created on 2018-10-27 by the reprex package (v0.2.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM