简体   繁体   English

R图例放置在图中

[英]R legend placement in a plot

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. 如果我手动输入ylim()参数来扩展大小然后给出我希望传说所在位置的确切坐标,我可以让它工作,但我更喜欢有一个更灵活的方法来做这个,因为它是一个数据库查询的前端和数据级别可能具有非常不同的级别。

在此输入图像描述

Edit 2017: 编辑2017:

use ggplot and theme(legend.position = ""): 使用ggplot和theme(legend.position =“”):

library(ggplot2)
library(reshape2)

set.seed(121)
a=sample(1:100,5)
b=sample(1:100,5)
c=sample(1:100,5)

df = data.frame(number = 1:5,a,b,c)
df_long <- melt(df,id.vars = "number")
ggplot(data=df_long,aes(x = number,y=value, colour=variable)) +geom_line() +
theme(legend.position="bottom")

Original answer 2012: Put the legend on the bottom: 原始答案2012:将图例放在底部:

set.seed(121)
a=sample(1:100,5)
b=sample(1:100,5)
c=sample(1:100,5)

dev.off()

layout(rbind(1,2), heights=c(7,1))  # put legend on bottom 1/8th of the chart

plot(a,type='l',ylim=c(min(c(a,b,c)),max(c(a,b,c))))
lines(b,lty=2)
lines(c,lty=3,col='blue')

# setup for no margins on the legend
par(mar=c(0, 0, 0, 0))
# c(bottom, left, top, right)
plot.new()
legend('center','groups',c("A","B","C"), lty = c(1,2,3),
       col=c('black','black','blue'),ncol=3,bty ="n")

在此输入图像描述

You have to add the size of the legend box to the ylim range 您必须将图例框的大小添加到ylim范围

#Plot an empty graph and legend to get the size of the legend
x <-1:10
y <-11:20
plot(x,y,type="n", xaxt="n", yaxt="n")
my.legend.size <-legend("topright",c("Series1","Series2","Series3"),plot = FALSE)

#custom ylim. Add the height of legend to upper bound of the range
my.range <- range(y)
my.range[2] <- 1.04*(my.range[2]+my.legend.size$rect$h)

#draw the plot with custom ylim
plot(x,y,ylim=my.range, type="l")
my.legend.size <-legend("topright",c("Series1","Series2","Series3"))

在此输入图像描述

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 the legend xy coordinates. 在@ P-Lapointe解决方案的基础上,使其非常简单,您可以使用max()使用数据中的max() ,然后重新使用这些最大值来设置legend xy坐标。 To make sure you don't get beyond the borders, you set up ylim slightly over the maximum values. 为确保不超出边界,请将ylim设置ylim略高于最大值。

a=c(rnorm(1000))
b=c(rnorm(1000))
par(mfrow=c(1,2))
plot(a,ylim=c(0,max(a)+1))
legend(x=max(a)+0.5,legend="a",pch=1)
plot(a,b,ylim=c(0,max(b)+1),pch=2)
legend(x=max(b)-1.5,y=max(b)+1,legend="b",pch=2)

在此输入图像描述

?legend will tell you: ?legend会告诉你:

Arguments 参数

x , y xy
the x and y co-ordinates to be used to position the legend. xy坐标用于定位图例。 They can be specified by keyword or in any way which is accepted by xy.coords : See 'Details'. 它们可以通过关键字或以xy.coords接受的任何方式指定:请参阅“详细信息”。

Details: 细节:

Arguments x , y , legend are interpreted in a non-standard way to allow the coordinates to be specified via one or two arguments. 参数xy ,legend以非标准方式解释,以允许通过一个或两个参数指定坐标。 If legend is missing and y is not numeric, it is assumed that the second argument is intended to be legend and that the first argument specifies the coordinates. 如果缺少图例且y不是数字,则假定第二个参数是图例,第一个参数指定坐标。

The coordinates can be specified in any way which is accepted by xy.coords . 可以以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 list bottomright , bottom , bottomleft , left , topleft , top , topright , right and center . 的位置也可以通过设置指定x从列表中单个关键字bottomrightbottombottomleftlefttoplefttoptoprightrightcenter 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. 可选的inset参数指定图例从绘图边距插入的距离。 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. 如果给出两个值,则第一个用于x距离,第二个用于y距离。

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

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