简体   繁体   English

R:使用具有通用XY绘图功能的cm单位定位图形元素

[英]R: Positioning graph elements using cm units with generic X-Y Plotting functions

I am sure this is not new for the R community, but is new to me and can't find a clear answer. 我确信这对于R社区来说并不是新事物,但对我来说却是新事物,因此找不到明确的答案。 Assuming this example: 假设这个例子:

plot(1:10, xlab="", xaxt="n") # supress OX axis
title(xlab="How can I use cm?", line=2.5)
axis(side=1, at=1:10, line=0.2) 

在此处输入图片说明

Here I used line argument in function title() to place a label at 2,5 lines of text "outwards from the plot edge" (as described in ?title help). 在这里,我在函数title()使用了line参数,以在“从绘图边缘向外”的2.5行文本处放置标签(如?title帮助中所述)。 Is there any argument that can take cm, or a way to use cm? 是否有任何可以占用cm的参数,或者使用cm的方法? Also, how can I find out how many cm does a line of text contains (if there is no other way around)? 另外,如何确定一行文本包含多少厘米(如果没有其他方法)?

Would also be great to know/set the margins in cm and not only like par("mar") [lines of text] or par("mai") [inches]. 知道/设置以cm为单位的边距,不仅会像par("mar") [文本行]或par("mai") [inches]一样,也很棒。 Is there a way to do that? 有没有办法做到这一点?

Using the line2user function from this answer you can convert centimeters to a "line" then convert the line to user coordinates and add things to the plot using xpd = TRUE : 使用此答案中line2user函数,您可以将厘米转换为“线”,然后将线转换为用户坐标,并使用xpd = TRUE将其添加到绘图中:

cm2line <- function(x) {
  lh <- par('cin')[2] * par('cex') * par('lheight')
  inch <- x/2.54
  inch/lh
}

par(mai = rep(5/2.54, 4))
plot.new() 
box()
mtext("hello", side = 3, line = cm2line(2))
abline(h = line2user(cm2line(1:5), side = 4), xpd = TRUE)
abline(h = line2user(cm2line(1:5), side = 1), xpd = TRUE)
abline(v = line2user(cm2line(1:5), side = 2), xpd = TRUE)
abline(v = line2user(cm2line(1:5), side = 3), xpd = TRUE)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM