简体   繁体   English

获取plot()边界框值

[英]Get plot() bounding box values

I'm generating numerous plots with xlim and ylim values that I'm calculating on a per-plot basis. 我正在生成许多具有xlimylim值的图,我在每个图的基础上计算。 I want to put my legend outside the plot area (just above the box around the actual plot), but I can't figure out how to get the maximum y-value of the box around my plot area. 我想把我的传说放在情节区域之外(就在实际情节周围的方框上方),但我无法弄清楚如何获得我的情节区域周围框的最大y值。

Is there a method for even doing this? 有没有办法做到这一点? I can move the legend where I want it by manually changing the legend() x and y values, but this takes a LONG time for the amount of graphs I'm creating. 我可以通过手动更改legend() x和y值来移动我想要的legend() ,但这需要很长的时间才能创建我正在创建的图表数量。

Thanks! 谢谢!

-JM -JM

Here's a basic example illustrating what I think you're looking for using one of the code examples from ?legend . 这是一个基本的例子,说明我认为你正在寻找的东西使用?legend一个代码示例。

#Construct some data and start the plot
x <- 0:64/64
y <- sin(3*pi*x)
plot(x, y, type="l", col="blue")
points(x, y, pch=21, bg="white")

#Grab the plotting region dimensions
rng <- par("usr")

#Call your legend with plot = FALSE to get its dimensions
lg <- legend(rng[1],rng[2], "sin(c x)", pch=21, 
            pt.bg="white", lty=1, col = "blue",plot = FALSE)

#Once you have the dimensions in lg, use them to adjust
# the legend position
#Note the use of xpd = NA to allow plotting outside plotting region             
legend(rng[1],rng[4] + lg$rect$h, "sin(c x)", pch=21, 
            pt.bg="white", lty=1, col = "blue",plot = TRUE, xpd = NA)

在此输入图像描述

The command par('usr') will return the coordinates of the bounding box, but you can also use the grconvertX and grconvertY functions. 命令par('usr')将返回边界框的坐标,但您也可以使用grconvertXgrconvertY函数。 A simple example: 一个简单的例子:

plot(1:10)
par(xpd=NA)
legend(par('usr')[1], par('usr')[4], yjust=0, legend='anything', pch=1)
legend( grconvertX(1, from='npc'), grconvertY(1, from='npc'), yjust=0,
 xjust=1, legend='something', lty=1)

The oma, omd, and omi arguments of par() control boundaries and margins of plots - they can be queried using par()$omd (etc). par()的oma,omd和omi参数控制图的边界和边距 - 可以使用par()$omd (etc)查询它们。 and set (if needed) using par(oma=c()) (where the vector can have up to 4 values - see ?par) 并使用par(oma=c())设置(如果需要)(其中向量最多可以有4个值 - 参见?par)

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

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