[R] plot dates in x-axis

Viknesh viknesh at stat.ufl.edu
Wed Oct 14 02:03:12 CEST 2009


Hi,

here is a code snippet that will help you achieve what you want. With it,
you can format the Date object to display any aspect of the Date on the
x-axis, like dd-mm, dd-yyyy, or day, etc. 

See the help on strftime() for the formatting key.

d1 <- "10/10/2009"
d2 <- "10/11/2009"
d3 <- "10/12/2009"
d <- c(d1, d2, d3)
d <- as.Date(d, format="%m/%d/%Y")
Y <- rnorm(3)
plot(Y, xaxt='n', xlab="Date")
# to plot the Date vertically
axis(side=1, at=1:3, labels=strftime(d, format="%d-%m-%Y"), cex.axis=0.8)
# to plot the Date sideways, and only *month*
axis(side=1, at=1:3, labels=strftime(d, format="%b"), las=2)

Cheers,
Vik


Lathouri, Maria wrote:
> 
> Hallo,
> 
> I am trying to plot dates in x-axis. The format of my dates are in
> dd/mm/yyyy. At first I install zoo package. After R reads my file, I
> change the default format of dates to the format that I have,
> 
>>myfile<-read.csv
>>DATE<-as.Date(DATE,format="%d/%m/%Y")
> 
> up till now everything goes ok; when however I go and plot for example
>>plot(DATE, Discharge)
> 
> in the x-axis I only get the years, 1998, 2000, 2002...
> 
> How can I see in the x-axis the whole format of the dates that I have e.g.
> 29/01/1998?
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/plot-dates-in-x-axis-tp25870714p25883066.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list