指定在信纸上的精确绘图位置。

huangapple go评论56阅读模式
英文:

specify exact plot location on letter size paper

问题

以下是翻译好的部分:

我正在使用R,尝试创建一个图,其中的元素精确位于8.5x11页上的特定位置。例如,我需要一个点位于页面左侧0.25英寸处,距页面顶部0.75英寸处。当我将图保存为PDF时,点的位置接近,但不完全准确。我需要调整par()中的哪个设置才能让点和线出现在正确的确切位置?

以下是代码:

pdf(file = "H:/temp/plots/demo.pdf", paper = 'letter', width=8.5, height = 11,
    pagecentre = FALSE)
par(mar = c(0,0,0,0))
par(mai = c(0,0,0,0))
par(mgp = c(0,0,0))
par(oma = c(0,0,0,0))
par(omi = c(0,0,0,0))
par(pin = c(8.5, 11))
par(xaxs = 'i')
par(yaxs = 'i')
par(xpd=TRUE)
par(pty='m')
plot(x=0.25, y=10.75, type = 'p', ylab="", yaxt="n", bty="n",
     xlab="", xaxt = "n",
     xlim = c(0,8.5), ylim = c(0,11))
lines(x=c(0.25,0.25), y=c(0,11))
lines(x=c(0,8.5), y=c(10.75,10.75))
dev.off()

生成了一个图,其中包含垂直线、水平线和一个点。这些线和点应该在页面左侧0.25英寸和页面顶部0.75英寸的位置交叉。它很接近,但如果放大查看,线和点的位置可能会有小数英寸的偏差。

英文:

I am using R and I am trying to create a plot with elements that are at exactly locations on an 8.5x11 page. For example, I need a point at .25 inches from the left of the page and .75 inches from the top of the page. When I save the plot to a pdf, the point is close, but not quite where it should be. What setting in par() do I need to adjust to get the points and lines in the right, exact location?

The following code:


pdf(file = "H:/temp/plots/demo.pdf", paper = 'letter', width=8.5, height = 11,
    pagecentre = FALSE)
par(mar = c(0,0,0,0))
par(mai = c(0,0,0,0))
par(mgp = c(0,0,0))
par(oma = c(0,0,0,0))
par(omi = c(0,0,0,0))
par(pin = c(8.5, 11))
par(xaxs = 'i')
par(yaxs = 'i')
par(xpd=TRUE)
par(pty='m')
plot(x=.25, y=10.75, type = 'p', ylab="", yaxt="n", bty="n",
     xlab="", xaxt = "n",
     xlim = c(0,8.5), ylim = c(0,11))
    lines(x=c(.25,.25), y=c(0,11))
    lines(x=c(0,8.5), y=c(10.75,10.75))
dev.off()

Produces a plot with a vertical line, a horizontal line, and point. The lines and the point should cross at .25 inches from the left and .75 inches from the top. It's close, but zooming in and the lines and point are off by fractions of an inch.

指定在信纸上的精确绘图位置。

答案1

得分: 2

主要的事情是:不要使用paper参数。从?pdf中可以看到:

paper参数设置文件中的‘⁠/MediaBox⁠’项,默认为宽度乘以高度。如果将其设置为除了"special"之外的值,指定大小的设备区域(默认情况下)将位于纸张尺寸所定义的矩形的中心:如果宽度或高度小于0.1或太大以至于无法产生0.5英寸的总边距,它将被重置为相应的纸张尺寸减去0.5。

换句话说,如果你指定paper,就会始终插入一些边距,这会导致问题。

以下是一个足够简单的示例代码,无需设置太多内容:

pdf(file="demo.pdf", width=8.5, height=11)
par(mar=c(0,0,0,0), xaxs='i', yaxs='i')
frame()
plot.window(xlim=c(0, 8.5), ylim=c(0, 11))
points(0.25, 10.75)
lines(x=c(0.25, 0.25), y=c(0, 11))
lines(x=c(0, 8.5), y=c(10.75, 10.75))
dev.off()

指定在信纸上的精确绘图位置。

英文:

The main thing is: don't use the paper argument. From ?pdf:

> The paper argument sets the ‘⁠/MediaBox⁠’ entry in the file, which defaults to width by height. If it is set to something other than "special", a device region of the specified size is (by default) centred on the rectangle given by the paper size: if either width or height is less than 0.1 or too large to give a total margin of 0.5 inch, it is reset to the corresponding paper dimension minus 0.5.

In other words, if you specify paper there will always be inserted some margin that will mess things up.

Here is a minimal code that should be sufficient, no need to set up everything:

pdf(file="demo.pdf", width=8.5, height=11)
par(mar=c(0,0,0,0), xaxs='i', yaxs='i')
frame()
plot.window(xlim=c(0, 8.5), ylim=c(0, 11))
points(.25, 10.75)
lines(x=c(.25, .25), y=c(0, 11))
lines(x=c(0, 8.5), y=c(10.75, 10.75))
dev.off()

指定在信纸上的精确绘图位置。

huangapple
  • 本文由 发表于 2023年2月24日 05:37:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定