R 图例在绘图中的位置
我有一个图,其中的数据进入我想用于图例的区域。有没有办法让绘图自动放入最高数据点上方的标题空间之类的内容以适应图例?
如果我手动输入 ylim() 参数来扩展大小,然后给出我想要图例所在位置的确切坐标,我就可以让它工作,但我更希望有一个更灵活的方法这样做的方法是因为它是数据库查询的前端,并且数据级别可能具有非常不同的级别。
I have a plot that has data that runs into the area I'd like to use for a legend. Is there a way to have the plot automatically put in something like a header space above the highest data points to fit the legend into?
I can get it to work if I manually enter the ylim()
arguments to expand the size and then give the exact coordinates of where I want the legend located, but I'd prefer to have a more flexible means of doing this as it's a front end for a data base query and the data levels could have very different levels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑 2017:
使用 ggplot 和 theme(legend.position = ""):
原始答案 2012:
将图例放在底部:
Edit 2017:
use ggplot and theme(legend.position = ""):
Original answer 2012:
Put the legend on the bottom:
您必须将图例框的大小添加到 ylim 范围
You have to add the size of the legend box to the ylim range
基于 @P-Lapointe 解决方案构建,但使其变得非常简单,您可以使用 max() 来使用数据中的最大值,然后重新使用这些最大值来设置图例 xy 坐标。为了确保不会超出边界,您将
ylim
设置为略高于最大值。Building on @P-Lapointe solution, but making it extremely easy, you could use the maximum values from your data using
max()
and then you re-use those maximum values to set thelegend
xy coordinates. To make sure you don't get beyond the borders, you set upylim
slightly over the maximum values.?legend
会告诉您:参数
x
,y
用于定位图例的
x
和y
坐标。它们可以通过关键字或 xy.coords 接受的任何方式指定:请参阅“详细信息”。详细信息:
参数
x
、y
、legend 以非标准方式解释,以允许通过一个或两个参数指定坐标。如果图例丢失并且 y 不是数字,则假定第二个参数是图例,并且第一个参数指定坐标。坐标可以以
xy.coords
接受的任何方式指定。如果给出一个点的坐标,则将其用作包含图例的矩形的左上角坐标。如果它给出两个点的坐标,则这些点指定矩形的对角(任意一对角,顺序任意)。还可以通过将
x
设置为列表中的单个关键字来指定位置bottomright
、bottom
、bottomleft
、left
、topleft
、top
、topright
、right
和center< /代码>。这会将图例放置在图框内部的给定位置。使用部分参数匹配。可选的 inset 参数指定图例从绘图边距插入的距离。如果给出单个值,则该值将用于两个边距;如果给出两个值,第一个值用于 x 距离,第二个值用于 y 距离。
?legend
will tell you:Arguments
x
,y
the
x
andy
co-ordinates to be used to position the legend. They can be specified by keyword or in any way which is accepted byxy.coords
: See ‘Details’.Details:
Arguments
x
,y
, legend are interpreted in a non-standard way to allow the coordinates to be specified via one or two arguments. If legend is missing andy
is not numeric, it is assumed that the second argument is intended to be legend and that the first argument specifies the coordinates.The coordinates can be specified in any way which is accepted by
xy.coords
. If this gives the coordinates of one point, it is used as the top-left coordinate of the rectangle containing the legend. If it gives the coordinates of two points, these specify opposite corners of the rectangle (either pair of corners, in any order).The location may also be specified by setting
x
to a single keyword from the listbottomright
,bottom
,bottomleft
,left
,topleft
,top
,topright
,right
andcenter
. This places the legend on the inside of the plot frame at the given location. Partial argument matching is used. The optional inset argument specifies how far the legend is inset from the plot margins. If a single value is given, it is used for both margins; if two values are given, the first is used for x- distance, the second for y-distance.