Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading in multiple png files in order to create a new plot with grid.arrange

Tags:

r

Let's say that I just created 4 histograms in ggplot2. After finishing, I realized that I should have used grid.arrange to combine some of the plots into a single graphics device. There is the png and jpeg packages in CRAN, both of which can be used to overlay graphics on a plot. Besides rerunning the visualizations again, are there packages in R that can be used to import multiple png files and then used to arrange a graphics device with each of them on it?

Here's some sample code, though the visualization are all the same.

df = data.frame(one=c(1,3,5,6,7,3,4,5,2,5,3,1,2))
df

library(ggplot2)
p1 = qplot(one, data=df, geom="histogram")
p2 = qplot(one, data=df, geom="histogram")
p3 = qplot(one, data=df, geom="histogram")
p4 = qplot(one, data=df, geom="histogram")

png("my_viz1.png")
p1
dev.off()

png("my_viz2.png")
p2
dev.off()

png("my_viz3.png")
p3
dev.off()

png("my_viz4.png")
p4
dev.off()
like image 965
amathew Avatar asked Feb 28 '14 22:02

amathew


1 Answers

rl = lapply(sprintf("my_viz%i.png", 1:4), png::readPNG)
gl = lapply(rl, grid::rasterGrob)
gridExtra::grid.arrange(grobs=gl)
like image 56
baptiste Avatar answered Sep 26 '22 18:09

baptiste