Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save multiple plots in R as a .jpg file, how?

Tags:

plot

r

histogram

I am very new to R and I am using it for my probability class. I searched for this question here, but it looks that is not the same as I want to do. (If there is already an answer, please tell me).

The problem is that I want to save multiple plots of histograms in the same file. For example, if I do this in the R prompt, I get what I want:

library(PASWR)
data(Grades)
attach(Grades) # Grade has gpa and sat variables
par(mfrow=c(2,1))
hist(gpa)
hist(sat)

So I get both histograms in the same plot. but if I want to save it as a jpeg:

library(PASWR)
data(Grades)
attach(Grades) # Grades has gpa and sat variables

par(mfrow=c(2,1))
jpeg("hist_gpa_sat.jpg")
hist(gpa)
hist(sat)
dev.off()

It saves the file but just with one plot... Why? How I can fix this? Thanks.

Also, if there is some good article or tutorial about how to plot with gplot and related stuff it will be appreciated, thanks.

like image 983
Edwardo Avatar asked Sep 03 '13 05:09

Edwardo


1 Answers

Swap the order of these two lines:

par(mfrow=c(2,1))
jpeg("hist_gpa_sat.jpg")

so that you have:

jpeg("hist_gpa_sat.jpg")
  par(mfrow=c(2,1))
  hist(gpa)
  hist(sat)
dev.off()

That way you are opening the jpeg device before doing anything related to plotting.

like image 88
thelatemail Avatar answered Oct 26 '22 07:10

thelatemail