Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting multiple panels of plots and data to *.png (in the style layout() works within R)

Tags:

plot

r

I'm trying to export multiple panels of data and 1 plot as a single image using either the .png device or .pdf device and I'm not meeting with any success. I can produce the image that I want within R using the R native plotting device, but when I try to produce the same image directly, I get results that I do not anticipate.

Here is my example code

testMat <- matrix(1:20, ncol = 5)##  create data
testMatDF <- as.data.frame(testMat)
names(testMatDF) <- c("Hey there", "Column 2", 
         "Some * Symbols", "And ^ More", 
         "Final Column")
rownames(testMatDF) <- paste("Group", 1:4)

library(gplots) ##  gplots needed for textplot()
layout(matrix(c(1, 1, 2, 3, 3, 3),  2, 3, byrow = TRUE))
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
##  produces what I want within R

layout(matrix(c(1, 1, 2, 3, 3, 3),  2, 3, byrow = TRUE))
png(file='plot1.png')
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
##  only the last function texplot(testMatDF) gets output, not what I anticipated

I've also tried the mfrow() graphical parameter without success.

par(mfrow= c(3, 1))
png(file='plot2.png')
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
##  only the last function texplot(testMatDF) gets output
like image 745
Chris Avatar asked Dec 05 '22 13:12

Chris


1 Answers

If you move your calls to par or layout after you open your graphics device, it should work correctly.

png(file='plot2.png')
par(mfrow= c(3, 1))
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
like image 187
Justin Avatar answered Dec 08 '22 02:12

Justin