[R] no x label using axis.Date

Duncan Mackay dulcalma at bigpond.com
Sat Jun 14 01:50:31 CEST 2014


Dates are a problem in any language because they are irregular
As you have not provided a reproducible example

here are 2 solutions

totaldays <- seq.Date(as.Date("2013-11-01"), by = 7, length = 10)
 str(totaldays)
 Date[1:10], format: "2013-11-01" "2013-11-08" "2013-11-15" "2013-11-22"
"2013-11-29" "2013-12-06" "2013-12-13" "2013-12-20" "2013-12-27"
"2014-01-03"

 plot(totaldays, rep(1,10), axes = F)
 axis(1, at =  seq.Date(as.Date("2013-11-01"), by = "month", length = 3),
labels = format(seq.Date(as.Date("2013-11-01"), by = "month", length = 3),
"%b %Y"))
 axis(2, at = seq(0,2,0.5), labels = paste(seq(0,2,0.5)))
 box()
 
 
library(lattice)
xyplot(rep(1,10)~ totaldays,
       scales = list(x= list(at =  seq.Date(as.Date("2013-11-01"), by =
"month", length = 3),
                             labels = format(seq.Date(as.Date("2013-11-01"),
by = "month", length = 3), "%b %Y"))))

If you have more data you may get away without having to format the xaxis in
lattice.

xyplot(rep(1,10)~ totaldays,
            xlim =c(as.Date("2013-10-01"), as.Date("2014-02-01")) )

HTH

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of MKN
Sent: Friday, 13 June 2014 12:47
To: r-help at r-project.org
Subject: [R] no x label using axis.Date

I have tried multiple different methods to figure out how to get a date axis
of my preference (start date of each month). Any assistance would be
appreciated.
The below section is not producing a date axis:

plot(totaldays$totaldays,totaldays$y,type="n",ylim=c(0,Emax),xaxt="n")
*xlabels<-(strptime(totaldays$totaldays,"%Y-%m-%d",tz=""))
xlabels<-xlabels[xlabels$mday==1]
axis.Date(1,at=xlabels,format="%b-%Y")*

The above section is used in the following plot:

par(mfrow=c(4,1))
par(mar=c(0.8,0,0,0))
par(oma=c(2,4.5,3,2))

plot(totaldays$totaldays,totaldays$y,type="n",ylim=c(0,Dmax),xaxt="n")
points(D$date,D$TSP,col=Dcol,type="p",pch=16,lwd=2,xaxt="n")
text(mdate,Dmax-20,labels="DM1'",cex=1)
legend("topright",pch=16,cex=0.8,col=colLegend,
       legend=ALegend)
abline(h=Target,col="red",lwd=2)

plot(totaldays$totaldays,totaldays$y,type="n",ylim=c(0,Bmax),xaxt="n")
points(B$date,B$TSP,col=Bcol,type="p",pch=16,lwd=2,xaxt="n")
text(mdate,Bmax-20,labels="DM2",cex=1)
abline(h=Target,col="red",lwd=2)

plot(totaldays$totaldays,totaldays$y,type="n",ylim=c(0,Amax),xaxt="n")
points(A$date,A$TSP,col=Acol,type="p",pch=16,lwd=2,xaxt="n")
text(mdate,Amax-20,labels="DM3",cex=1)
abline(h=Target,col="red",lwd=2)

plot(totaldays$totaldays,totaldays$y,type="n",ylim=c(0,Emax),xaxt="n")
*xlabels<-(strptime(totaldays$totaldays,"%Y-%m-%d",tz=""))
xlabels<-xlabels[xlabels$mday==1]
axis.Date(1,at=xlabels,format="%b-%Y")*
points(E$date,E$TSP,col=Ecol,type="p",pch=16,lwd=2)
text(mdate,Emax-20,labels="DM4'",cex=1)
abline(h=Target,col="red",lwd=2)

title(main=Amain, ylab=Labely,
      outer=TRUE,cex.lab=1, cex.main=1.5)

Below is an excerpt of the total days data:

	totaldays	        y
1	2013-11-01	1
2	2013-11-02	1
3	2013-11-03	1
4	2013-11-04	1
5	2013-11-05	1
6	2013-11-06	1
7	2013-11-07	1
8	2013-11-08	1
9	2013-11-09	1
10	2013-11-10	1




--
View this message in context:
http://r.789695.n4.nabble.com/no-x-label-using-axis-Date-tp4692034.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.



More information about the R-help mailing list