Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R legend not working

Tags:

r

I'm an R newbie. I'm plotting 4 approximation to a line. I want to put the legend on a top corner. I've tried:

legend(
"topleft", legend=....)

Then I tried to manually set the position still not working. Below is my code and my plot:

 plot(1:N, vRm, pch=".", col="blue", xlab="m", ylim=c(0.885, 0.91)) 
 ylab="approximated 90th percentile")
 lines(1:N, v1m, pch=".", col="yellow")   
 lines(1:N, v2m, pch=".", col="green")
 lines(1:N, v3m, pch=".", col="red")

 legend(
 y=0.92, legend=c("quantile","90st", "91st", 
 "(90st+91st)/2"), col=c("blue", "yellow", "green", "red"),   pch=c(".",".", ".", ".")
 )

and the plot:

enter image description here

how can I place the legend on a top corner?

like image 466
user1871528 Avatar asked Sep 18 '13 10:09

user1871528


1 Answers

In your legend definition, you don't define the option x in the the function legend. Notice the R reference:

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

So, you could place on the top rigth, for instance, with this command:

legend( x= "topright", y=0.92, 
        legend=c("quantile","90st", "91st", "(90st+91st)/2"), 
        col=c("blue", "yellow", "green", "red"),   
        pch=c(".",".", ".", "."))
like image 187
FireVision Avatar answered Oct 30 '22 17:10

FireVision