[R] zoo plot: yearly marks on X-Axis

lanczos at fns.uniba.sk lanczos at fns.uniba.sk
Sun Sep 13 11:55:17 CEST 2009


Many thanks,

the solution is simple and elegant, as usual ;). R is a great tool, the
best I know, unfortunately I need more time to practice ...

regards

Tomas

On Sat, 2009-09-12 at 22:52 -0400, Gabor Grothendieck wrote:
> Try this:
>
> library(zoo)
> x <- zoo(rnorm(1:6000), as.Date("1992-11-11")+c(1:6000))
> plot(x, xaxt = "n")
> # cut time into years, remove duplicate years and convert back to Date
yy <- as.Date(unique(cut(time(x), "year")))
> axis(1, yy, format(yy, "%y"))
>
> There are a number of examples of custom X axes in the
> examples section of ?plot.zoo
>
> On Sat, Sep 12, 2009 at 10:33 PM,  <lanczos at fns.uniba.sk> wrote:
> > On Sun, 2009-07-26 at 13:17 +0200, Poersching wrote:
> >> stvienna wiener schrieb:
> >> > Hi all,
> >> >
> >> > I am plotting a financial time series, but I need a more detailed
X-Axis.
> >> >
> >> > Example:
> >> > x <- zoo(rnorm(1:6000), as.Date("1992-11-11")+c(1:6000))
> >> > plot(x)
> >> >
> >> > The X-Axis is labeled "1995", "2000" and "2005".
> >> > I would need either "1995", "1997", etc. or maybe yearly
> >> >
> >> >
> >> > I used google first, then look at "?plot.zoo" but could't get it
working.
> >> >
> >> >
> >> > Regards,
> >> > Steve
> >> >
> >> > ______________________________________________
> >> > 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.
> >> >
> >> >
> >> Hey,
> >>
> >> try something like the following:
> >>
> >> plot(x, y, xaxt="n")
> >> axis.Date(1, at=seq(as.Date("1960-01-01"), max(as.Date(x)), "years"),
labels = FALSE, tcl = -0.2)
> >> axis.Date(1, at=seq(as.Date("1960-01-01"), max(as.Date(x)), "5
years"),
> >> labels = TRUE, las=3, tcl = -0.2)
> >>
> >> Regards,
> >> Christian
> >
> > Hello,
> >
> > I'm actually struggling with the similar problem. I applied Your
script
> > into mine like this:
> > -
> > library(Rdbi)
> > library(RdbiPgSQL)
> > conn <- dbConnect(PgSQL(), host="localhost", dbname="BVS",
> > user="postgres", password = "******")
> >
> > query_duj_kal <- dbSendQuery(conn, "select zdroj as well, cas as date,
fe2, fe3, mn, nh4, no2, no3, o2, teplota as temperature from
analystats
> > where zdroj like 'Dunaj Kalinkovo' order by date")
> > watchem_duj_kal <- dbGetResult(query_duj_kal)
> >
> > date <- (watchem_duj_kal$date)
> > date_p <- strptime(date, "%m-%d-%Y")
> > no3 <- (watchem_duj_kal$no3)
> > nh4 <- (watchem_duj_kal$nh4)
> >
> > par(mfrow=c(3,1), ylog = TRUE, yaxp = c(0.01, 100, 3))
> > maxy <- 100
> > miny <- 0.005
> > plot(date_p, no3,log = "y", type = "l", ylim  = c(miny, maxy), col =
"darkred", main = "Dunaj Kalinkovo", xlab = "time", ylab =
"log(NO3-,NH4
> > +)", xaxt="n")
> > axis.Date(1, at=seq(as.Date("1972-01-01"),max(as.Date(date_p))),
"years"), labels = FALSE, tcl = -0.2)
> > axis.Date(1, at=seq(as.Date("1970-01-01"),max(as.Date(date_p))), "5
years"), labels = TRUE, las=3, tcl = -0.2)
> > -
> >
> > Everything goes well, except that I cannot get no tickles nor labels
at
> > the moment and I cannot find what I did wrong.
> >
> > Many thanks for any advice
> >
> > Tomas
> >
> > ______________________________________________
> > 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