Ryan Simmons
Ryan Simmons

Reputation: 883

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. 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.

Upvotes: 1

Views: 1845

Answers (1)

jlhoward
jlhoward

Reputation: 59385

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. So if the window of your data does not include (2,1), you will not see the legend.

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.

Upvotes: 2

Related Questions