Save multiple charts in R as a .jpg file, how?

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

The problem is that I want to save several histogram plots in one file. For example, if I do this at 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 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 with only one plot ... Why? How can i fix this? Thanks.

In addition, if there is a good article or tutorial on how to build a graph with gplot and related material, this will be appreciated, thanks.

+4
source share
2 answers

Reorder 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() 

This way you open the jpeg device before doing anything related to plotting.

+5
source

You can also look at the layout function. In this case, you can organize plots more freely. This example gives you a 2-column layout of graphs with 3 rows.

The first line is occupied by one plot, the second line - by two plots, and the third line - by one plot. This may come in handy.

 x <- rnorm(1000) jpeg("normdist.jpg") layout(mat=matrix(c(1,1,2,3,4,4),nrow=3,ncol=2,byrow=T)) boxplot(x, horizontal=T) hist(x) plot(density(x)) plot(x) dev.off() 

Check ?layout , how the matrix mat is interpreted ( layout first argument).

0
source

Source: https://habr.com/ru/post/1500239/


All Articles