Learn R Programming

oce (version 0.2-1)

oce.plot.ts: Plot a time-series, obeying the timezone

Description

Plot a time-series, obeying the timezone and possibly drawing the range in the top-left margin

Usage

oce.plot.ts(x, y, drawTimeRange=TRUE, xaxs="r", grid=TRUE, adorn=NULL,
  fill=FALSE,
  xlab="", ylab="", 
  cex=par("cex"), cex.axis=par("cex.axis"), cex.main=par("cex.main"),
  mgp=getOption("oceMgp"),
  mar=c(mgp[1]+if(nchar(xlab)>0) 1 else 0.5,
  mgp[1]+if(nchar(ylab)>0) 1.5 else 1, mgp[2]+1, mgp[2]+3/4),
  type="l",
  main="",
  despike=FALSE,
  debug=getOption("oceDebug"),
  ...)

Arguments

x
the times of observations.
y
the observations.
drawTimeRange
a boolean, set to TRUE to indicate the range of times in the top-left margin.
xaxs
character indicating whether image should extend to edge of x axis (with value "i") or not; see par("xaxs").
grid
boolean, set to TRUE to get a grid on the plot. (Note that grid does work correctly because it does not account properly for tic locations on the time axis.
adorn
optional expression to be performed immediately after drawing the panel. (See plot.adp for an example.)
fill
boolean, set TRUE to fill the curve to zero (which it does incorrectly if there are missing values in y).
xlab
names for x axis.
ylab
names for y axis.
cex
size of labels on axes; see par("cex").
cex.axis
see par("cex.axis").
cex.main
see par("cex.main").
mgp
3-element numerical vector to use for par(mgp), and also for par(mar), computed from this. The default is tighter than the R default, in order to use more space for the data and less for the axes.
mar
value to be used with par("mar").
type
plot type.
main
title of plot.
despike
boolean flag that can turn on despiking with despike.
debug
a flag that turns on debugging. Set to 1 to get a moderate amount of debugging information, or to 2 to get more.
...
graphical parameters passed down to plot.

encoding

UTF-8

Details

Depending on the version of R, the standard plot and plot.ts routines will not obey the time zone of the data. This routine gets around that problem. It can also plot the time range in the top-left margin, if desired; this string includes the timezone, to remove any possible confusion.

For flexibility, this does not set to line-type plots; use type='l' to get these.

Examples

Run this code
library(oce)
t.start <- as.POSIXct("2008-01-01", tz="UTC")
t <- seq(t.start, length.out=48, by="30 min")
y <- sin(as.numeric(t - t.start) * 2 * pi / (12 * 3600))
oce.plot.ts(t, y, type='l', xaxs='i')

weatherplot <- function(id=6358, time=as.POSIXlt(Sys.Date()))
{
    ## The default stationID is Halifax, NS, Canada
    site <- "http://www.climate.weatheroffice.gc.ca/climateData/bulkdata_e.html"
    time <- as.POSIXlt(time)
    f <- paste(site,
               "?StationID=", id,
               "&Year=", time$year+1900,
               "&Month=", time$mon+1,
               "&Day=", time$mday,
               "&format=csv&type=hly", sep="")
    ## Read lines first, to see where header ends.  Cannot parse header, owing
    ## to multi-byte strings there.
    ll <- readLines(f)
    skip <- which(grepl("Date", ll, perl=TRUE))
    d <- read.csv(f, skip=skip, header=FALSE)
    t <- as.POSIXct(d[,1])
    p <- d[,19]
    oce.plot.ts(t, p, type='l', ylab="Pressure [dbar]", cex=2/3, main=paste("Station", id))
}
weatherplot()

Run the code above in your browser using DataLab