简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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