简体   繁体   English

在R坐标值中添加文本或图例

[英]Adding text or legend to figure in R - coordinate values

So I have just have a simple line plot. 所以我只有一个简单的线图。 I am trying to add a legend and some text to it. 我正在尝试添加一个图例和一些文字。 However, it doesn't work. 但是,它不起作用。 The legend and text don't show up no matter what coordinate values I give them - the only way to get the legend to show up is to use 'top right' or 'center' (because the command takes text input as well as coordinate). 无论我给出的是什么坐标值,图例和文本都不显示 - 让图例显示的唯一方法是使用'右上角'或'中心'(因为该命令需要文本输入以及坐标)。 However, the x,y coordinate system doesn't work. 但是,x,y坐标系不起作用。 It doesn't give an error, the legend just doesn't show up at all. 它没有给出错误,传说根本没有显示出来。

Anybody have any idea what is going on here? 有人知道这里发生了什么吗?

Here is some sample code: 以下是一些示例代码:

plot(x=d[,1],y=d[,2], type='l', xlab='Minor allele frequency', ylab='Power', ylim=range(.5,1))
lines(x=r[,1],y=r[,2],lty=2)
legend(2,1,legend=c('Dominant','Recessive')
text(2.8,1,'Test')

The data itself is irrelevant. 数据本身无关紧要。 I just don't understand why the legend won't show up? 我只是不明白为什么传说不会出现? Or the text? 还是文字? What values are these coordinate values supposed to be? 这些坐标值应该是什么值? I've tried everything. 我已经尝试了一切。

Actually, the data is not irrelevant. 实际上,数据并不相关。 legend(x,y,...) plots the upper left of the legend at (x,y) in the coordinates defined by your plot. legend(x,y,...)图表定义的坐标中绘制图例左上角的(x,y)。 So if the window of your data does not include (2,1), you will not see the legend. 因此,如果数据窗口不包含(2,1),您将看不到图例。

Consider: 考虑:

x=seq(0,3,length.out=10)
plot(x,x)
legend(2,1,"My legend")   # I can see you...

Now try: 现在尝试:

x=seq(0,1,length.out=10)
plot(x,x)
legend(2,1,"My legend")   # Nope.

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

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