[英]r multiple graphs on a single jpeg
我遇到了以下問題,如果有人可以給我一些意見,我將不勝感激。
我想將多個圖形導出到單個jpeg文件中。 我首先創建一個圖形網格,然后導出。 我的主要問題是它適用於pdf,而不適用於jpeg。 有任何想法嗎?
謝謝
#set the windows of the frames
par(mfcol=c(3,2))
#create the jpeg file
jpeg(filename=names(a1),".jpg",sep=""),
quality=100,
width=1024,
height=768)
#plot 1
plot(a1,b1)
#plot 2
plot(a1,b2)
#plot 3
plot(a1,b3)
#plot 4
plot(a2, c1)
#plot 5
plot(a2, c2)
#plot 6
plot(a2, c3)
#dev.off shuts down the specified (by default the current) graphical device
#here it passes the picture to the file
dev.off()
目前尚不清楚您是否要在單個jpeg文件中獲取多個1024x768圖像-這是否有意義-是否要獲取包含6個圖的單個jpeg圖片。
正如我所說的,與PDF不同,JPEG不是多頁格式。 因此,您可以使R導出到多個JPEG文件,但不能在一個JPEG中包含所有單獨的圖形。
R的設備允許在文件名中使用通配符,因此,如果要將六個圖導出到文件foo001.jpeg
, foo002.jpeg
, foo00x.jpeg
則可以使用以下命令
jpeg(filename = "foo%03d.jpeg", ....)
.... # plotting commands here
dev.off()
如果您在沒有通配符/占位符的情況下進行多個繪圖,會發生什么情況?jpeg
:
If you plot more than one page on one of these devices and do not include something like '%d' for the sequence number in 'file', the file will contain the last page plotted.
設備可以處理多個頁面,因為基礎文件格式允許它在合理的情況下可以將多個圖表放入單個文件中,例如pdf()
和postscript()
。 這些設備具有參數onefile
,該參數可用於指示單個文件中是否需要多個圖。
但是, par(mfcol=c(3,2))
使我認為您希望在同一設備區域中放置3x2的繪圖集。 這是允許的,但是您需要在打開jpeg()
設備之后而不是之前調用par()
。 如圖所示,代碼的作用是將活動設備分成3x2的繪制區域,然后打開一個新設備,該設備將拾取默認參數,而不是在調用jpeg()
之前在活動設備上設置的默認參數。 如下圖所示:
> plot(1:10)
> dev.cur()
X11cairo
2
> op <- par(mfrow = c(3,2))
> jpeg("~/foo.jpg")
> par("mfrow")
[1] 1 1
> dev.off()
X11cairo
2
> par("mfrow")
[1] 3 2
因此,您可能想要以下內容:
jpeg(filename=names(a1),".jpg",sep=""), quality=100,
width=1024, height=768)
op <- par(mfcol=c(3,2))
#plot 1
plot(a1,b1)
#plot 2
plot(a1,b2)
#plot 3
plot(a1,b3)
#plot 4
plot(a2, c1)
#plot 5
plot(a2, c2)
#plot 6
plot(a2, c3)
par(op)
dev.off()
?
更正的代碼
#data
a1<-seq(1,20,by=1)
b1<-seq(31,50,by=1)
b2<-seq(51,70,by=1)
b3<-seq(71,90,by=1)
#create the jpeg file
jpeg(filename="a.jpg",
quality=100,
width=1024,
height=768)
#set the create the frames
par(mfcol=c(3,1))
#plot the graphs
plot(a1,b1)
plot(a1,b2)
plot(a1,b3)
#par(op)
#dev.off shuts down the specified (by default the current) graphical device
#here it passes the picture to the file
dev.off()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.