将R中的多个图另存为.jpg文件,怎么办? [英] save multiple plots in R as a .jpg file, how?

查看:310
本文介绍了将R中的多个图另存为.jpg文件,怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R很陌生,正在将其用于我的概率等级.我在这里搜索了这个问题,但看起来与我想做的不一样. (如果已经有答案,请告诉我.)

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).

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

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)

因此,我在同一图中获得了两个直方图.但是如果我想将其另存为jpeg:

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.

此外,如果有一些不错的文章或教程,介绍如何使用gplot和相关内容进行绘制,将不胜感激.

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设备.

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

这篇关于将R中的多个图另存为.jpg文件,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆