Writing text on the border of an A4 PDF in R

▼魔方 西西 提交于 2019-12-10 17:17:11

问题


I need to write on a PDF device on the extreme border of the page. With this snippet:

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

I get a foo.pdf with hello on the extreme left in the middle of the page, as intended,that is:

Unfortunately, if I set the paper output as paper='a4', that is:

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 isn't put on the border anymore, but:


回答1:


When paper is set to something else than "special", argument pagecentre becomes relevant. If you set it to FALSE, then the offset disappears.

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



回答2:


Well, I can confirm it doesn't work for me too; it looks like a bug IMHO.

A workaround for your problem is to set the A4 paper dimensions directly in the pdf call:

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


来源:https://stackoverflow.com/questions/19168471/writing-text-on-the-border-of-an-a4-pdf-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!