[R] Show date on graph

Jim Lemon jim at bitwrit.com.au
Wed Dec 6 08:49:51 CET 2006


Syed Abid Hussaini wrote:
> I have a character "2006-11-06" which was originally scanned from a csv file. This character is
> named date. I now use mtext (date) on a plot and i see strange cluttered characters instead of
> 2006-11-06. I tried many ways but i dont know whats going on. Then for testing i made an
> artificial character d <- ("2006-11-06") and now when i do mtext (d) it shows up on the plot.
> While both date and d return "2006-11-06" only d shows up on the plots. Any help would be appreciated.
> 
> 
>  
Hi Syed,
If you try to display something with mtext, it expects the object to be 
a character string. When you just print the object, the print method 
works out that it is a date object and silently converts it to a 
character string to be printed. Try this:

mtext(format(date,"%Y-%m-%d"),...)

Also note that it isn't a great idea to use "date" to name a variable. 
Even though you can now get away with it, using the same name for two 
things (a function and a variable) often comes back to haunt one.

Jim




More information about the R-help mailing list