[R] as.Date and Legend in a plot

MacQueen, Don m@cqueen1 @end|ng |rom ||n|@gov
Fri Jun 22 23:34:05 CEST 2018


What David Winsemius said, plus:

Does
  legend('topleft', c("Climax", "Thule", "Sopo"), lty = 1, col = ("black","red","blue"))
not work?

It should, which will illustrate that you CAN add a legend to the plot.

The x,y that you supply to legend must be values that are within the x,y range of the plot. Type
   par()$usr
to see what that range is, and I think you will see that 1998 is not within the plot range.

David showed you how to supply legend with an x value that is, we assume, within the plot range.

You *should* have had an error on your second try:

> as.Date(1998-02-10)
Error in as.Date.numeric(1998 - 2 - 10) : 'origin' must be supplied

-Don

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 6/22/18, 12:12 PM, "R-help on behalf of Ogbos Okike" <r-help-bounces using r-project.org on behalf of giftedlife2014 using gmail.com> wrote:

    Dear Contributors,
    
    I am surprised that I cannot add legend to a certain plot. Although the
    x-axis indicates years, the actual data was in year, month and day format.
    I then used as.Date to play around and get what I am looking for. I am,
    however, worried that I cannot add legend to the plot no matter how I
    tried. Looking at the axis, I tried:
    legend(1998,3, c("Climax", "Thule", "Sopo"), lty = 1, col =
    c("black","red","blue")). I also tried:
    
    legend(as.Date(1998-02-10),3, c("Climax", "Thule", "Sopo"), lty = 1, col =
    c("black","red","blue"))
    but no result and no error.
    
    I have attached the plot in case it will assist in your suggestions.
    
    Many thanks for your time.
    
    Ogbos
    ______________________________________________
    R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
    https://stat.ethz.ch/mailman/listinfo/r-help
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.
    



More information about the R-help mailing list