Hello, I plot the abundance of a species in relation to the date. To have the date as a continous variable I put it in the format "standard" in excel (f.ex. 39939 means 06.05.2009). R uses 39939 on the x axis, but I would like to have "06.05". I tried to use as.Date as suggested in some discussion but I don't manage to use it, the returned date is not correct. Do you have any clue? thank you -- View this message in context: http://www.nabble.com/Date-format-in-plot-tp25244066p25244066.html Sent from the R help mailing list archive at Nabble.com.
We will need a reproducible example! Please give us R commands that display the behavior you're observing: For example, I am having trouble understanding the as.Date function. When I input 39939, I would like to get "06.05.2009", but when I try it, I get> as.Date(39939)Error in as.Date.numeric(39939) : 'origin' must be supplied I looked up what origin Excel uses for its' dates, and it seems like it might be January 1, 1900, so I tried as.Date(39939, origin = "1900-01-01") [1] "2009-05-08" Then we will much better be able to help you, because we will be able to paste your commands into R and see the results and make changes. But this still seems to be off by two days. So did you really mean "06.05", or "08.05"? -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of swertie Sent: Tuesday, September 01, 2009 12:59 PM To: r-help at r-project.org Subject: [R] Date format in plot Hello, I plot the abundance of a species in relation to the date. To have the date as a continous variable I put it in the format "standard" in excel (f.ex. 39939 means 06.05.2009). R uses 39939 on the x axis, but I would like to have "06.05". I tried to use as.Date as suggested in some discussion but I don't manage to use it, the returned date is not correct. Do you have any clue? thank you
On Sep 1, 2009, at 1:58 PM, swertie wrote:> > Hello, I plot the abundance of a species in relation to the date. To > have the > date as a continous variable I put it in the format "standard" in > excel > (f.ex. 39939 means 06.05.2009). R uses 39939 on the x axis, but I > would like > to have "06.05". I tried to use as.Date as suggested in some > discussion but > I don't manage to use it, the returned date is not correct. Do you > have any > clue? thank youYou may need to do some jiggling around because R doesn't agree with that number of days since the beginning of the last century: > as.Date(39939, origin="1900-01-01") [1] "2009-05-08" Then you need to look at docs re: the formating of date objects. Some code might move this process along. -- David Winsemius, MD Heritage Laboratories West Hartford, CT
See R News 4/1. The article on dates there discusses how they work and discusses Excel's dates as well. On Tue, Sep 1, 2009 at 1:58 PM, swertie<v_coudrain at voila.fr> wrote:> > Hello, I plot the abundance of a species in relation to the date. To have the > date as a continous variable I put it in the format "standard" in excel > (f.ex. 39939 means 06.05.2009). R uses 39939 on the x axis, but I would like > to have "06.05". I tried to use as.Date as suggested in some discussion but > I don't manage to use it, the returned date is not correct. Do you have any > clue? thank you > > -- > View this message in context: http://www.nabble.com/Date-format-in-plot-tp25244066p25244066.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
As suggested in the article R News 4/1, I used as.Date(as.character(Phenology_VE$Date), "%Y-%m-%d"), however this function returns me only "NA" values as.Date(as.character(Phenology_VE$Date), "%Y-%m-%d") [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [26] NA NA NA NA NA NA NA NA NA NA The date corresponding to 39939 is really 06.05.2009, so it doesn't match R format. I tried to use names(Date)<-c("06.05","07.05","11.05","19.05","20.05","22.05","23.05","24.05","28.05","29.05","31.05","03.06","04.06","09.06","10.06","12.06","14.06","17.06","24.06","29.06","30.06","01.07","26.07","27.07","29.07","04.08","05.08","12.08","13.08","14.08","17.08","18.08","19.08","27.08","28.08") plot(Date, Phenology_VE[,3], ylim=range(0, 16), main=names(Phenology_VE[3]),xaxt="n", yaxt="n") axis(1, labels=names(Date), at=Date) It works more or less, but I don't get regular intervals, but a thick for each date. Do you have any idea how I could represent only some dates? Thank you -- View this message in context: http://www.nabble.com/Date-format-in-plot-tp25244066p25252150.html Sent from the R help mailing list archive at Nabble.com.