2013-10-03 16 views
4

我需要在頁面的極端邊框上的PDF設備上進行書寫。在這個片段:在R中的A4 PDF邊框上書寫文字

pdf('foo.pdf')   #Write next plot to foo.pdf in current dire 
par(mar=c(0, 0, 0, 0)) #Set numbers of lateral blank lines to zero 
par(xaxs='i', yaxs='i') #Does not extend axes by 4 percent for pretty labels 
plot.new()    #Create a blank plot, as we just want to write our text 
text(0, .5, "hello", pos=4, offset=0) #Write to the right with no default 0.5 offset 
dev.off()    #Close device, that is saving for a PDF device 

我得到一個foo.pdfhello對留在頁面的中間,如預期的極端,那就是:

enter image description here

不幸的是,如果我設置的文件輸出paper='a4',那就是:

pdf('foo.pdf', paper='a4') #note the A4 setting 
par(mar=c(0, 0, 0, 0)) 
par(xaxs='i', yaxs='i') 
plot.new() 
text(0, .5, "hello", pos=4, offset=0) 
dev.off() 

hello沒有提上了伯德[R了,但:

enter image description here

回答

2

paper被設置爲"special"別的東西,爭論pagecentre變得相關。如果將其設置爲FALSE,則偏移消失。

pdf('foo.pdf', paper='a4', pagecentre=FALSE) 
par(mar=c(0, 0, 0, 0)) 
par(xaxs='i', yaxs='i') 
plot.new() 
text(0, .5, "hello", pos=4, offset=0) 
dev.off() 
+0

您的方法適用於0邊界,但不適用於1邊界。對於紙張A4,文本(0,1,「hello」,pos = 4,offset = 0)將在頁面高度的大約75%處。最糟糕的是,無論如何,每個出現'y> 1'(或'x> 1')的文本都被認爲是出於邊界而不被打印。 – antonio

1

好了,我可以證實它並沒有爲我工作過;它看起來像一個錯誤恕我直言。

用於您的問題的解決方法是直接在pdf調用中設置的A4紙尺寸:

pdf('foo.pdf', width=8.3, height=11.7) #we set A4 dimensions of 8.3 x 11.7 inches 
par(mar=c(0, 0, 0, 0)) 
par(xaxs='i', yaxs='i') 
plot.new() 
text(0, .5, "hello", pos=4, offset=0) 
dev.off()