将R中的多个图保存为.jpg文件,怎么样?(save multiple plots in R as a .jpg file, how?)

我是R的新手,我将它用于概率课。 我在这里搜索了这个问题,但它看起来和我想做的不一样。 (如果已经有答案,请告诉我)。

问题是我想在同一个文件中保存多个直方图。 例如,如果我在R提示符中执行此操作,我会得到我想要的:

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

所以我在同一个图中得到了两个直方图。 但如果我想将它保存为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()

它保存文件但只有一个图...为什么? 我怎么解决这个问题? 谢谢。

此外,如果有一些关于如何使用gplot和相关内容绘制的好文章或教程,将不胜感激,谢谢。

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.

最满意答案

交换这两行的顺序:

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

这样你就拥有:

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

这样你就可以在做任何与绘图有关的事情之前打开jpeg设备。

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.

更多推荐