[R] Problem using plot and converted dates

s.e.roberts at qmul.ac.uk s.e.roberts at qmul.ac.uk
Tue Apr 18 00:07:41 CEST 2006


Dear R-help,
My apologies, my previous script ran successfully before I posted as I had
already created mkf
as an object in my active workspace. I have had some success, however I am
unable to plot
all 9 of the 'sam5' dates using axis.Date:

mkf<-c(0.021375,0.078780,0.028560,0.070278,0.021375,0.078780,0.028560,0.078780,0.021375)
sam4<-c("03/03/01","03/05/01","03/07/01","03/09/01","03/11/01","04/01/01","04/03/01","04/05/01","04/07/01")
sam5<-as.Date(sam4,format="%y/%m/%d")
plot(sam5,mkf,type="l", xaxt="n")
axis.Date(1, sam5, format = "%b-%y")

# The above just plots 'Jan-04', removing the last 2 gives:
mkf<-c(0.021375,0.078780,0.028560,0.070278,0.021375,0.078780,0.028560)
sam4<-c("03/03/01","03/05/01","03/07/01","03/09/01","03/11/01","04/01/01","04/03/01")
sam5<-as.Date(sam4,format="%y/%m/%d")
plot(sam5,mkf,type="l", xaxt="n")
axis.Date(1, sam5, format = "%b-%y")

#I have tried reducing the size using cex.axis (to no avail):
mkf<-c(0.021375,0.078780,0.028560,0.070278,0.021375,0.078780,0.028560,0.078780,0.021375)
sam4<-c("03/03/01","03/05/01","03/07/01","03/09/01","03/11/01","04/01/01","04/03/01","04/05/01","04/07/01")
sam5<-as.Date(sam4,format="%y/%m/%d")
plot(sam5,mkf,type="l", xaxt="n")
axis.Date(1, sam5, format = "%b-%y",cex.axis=0.1)


#and reducing the margins (also to no avail...):
mar=c(1,1,1,1)
mkf<-c(0.021375,0.078780,0.028560,0.070278,0.021375,0.078780,0.028560,0.078780,0.021375)
sam4<-c("03/03/01","03/05/01","03/07/01","03/09/01","03/11/01","04/01/01","04/03/01","04/05/01","04/07/01")
sam5<-as.Date(sam4,format="%y/%m/%d")
plot(sam5,mkf,type="l", xaxt="n")
axis.Date(1, sam5, format = "%b-%y")


Any help gratefully received.

Best wishes,
Sion

R 2.1.1



Quoting Prof Brian Ripley <ripley at stats.ox.ac.uk>:

> On Mon, 17 Apr 2006, s.e.roberts at qmul.ac.uk wrote:
>
> Please do follow the posting guide and at least tell us your version of R
> and give a working example (mkf is missing)
>
> > Dear R-help,
> > I am having problems with plotting converted dates in chronological as
> Mar-03,
> > May-03...Jul-04.
> > My script is:
> >
> >
>
sam4<-c("03/03/01","03/05/01","03/07/01","03/09/01","03/11/01","04/01/01","04/03/01","04/05/01","04/07/01")
> > sam5<-strptime(sam4,format="%y/%m/%d")
>
> I would suggest you use as.Date here, for you have dates, not date-times.
>
> > f<-data.frame(sam5,mkf)
> > plot(f,type="l")
> > # This works ok, but none of the labels appear on the x-axis,
>
> It works for me: it is not intended to plot your labels (and plot.default
> does not do that either).  If you want add a special axis, use axis.Date
> to do so.
>
> > so I thought I
> > would convert them using
> > # as.POSIXct:
>
> You have converted them to a factor, which is not what you want.
>
> > sam6<-format(as.POSIXct(sam5), "%b-%y")
> > mon<-factor(as.character(sam6),levels=sam6)
> > f2<-data.frame(mon,mkf)
> > plot(f2,type="l")
> >
> > The problem is that plot(f2,type="l") no longer produces a line plot...
>
> Why don't you use plot(sam5, mkf, type="l")?  That works, and in fact
> your example works in R 2.3.0RC, but creating a data frame you don't need
> is counter-productive.
>
> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>


--
Siôn Roberts

Department of Geography,
Queen Mary, University of London,
London,
E1 4NS.

Tel: +44 20 7882 5400
http://www.geog.qmul.ac.uk/postgraduate/student/roberts.html




More information about the R-help mailing list