Skip to content
Prev 280877 / 398503 Next

need help with a time series plotting problem

Try this; this should put the labels on the x-axis like Excel does:

flow <- read.table(text = "Date    USGS700 USGS1000        USGS1500
USGS1898       USGS1975        USGS2500        USGS2700 USGS2800
10/1/2001       0.05    0.57    2.32    2.27    4.11    29.45   29.45   29.45
10/1/2002       0.04    0.54    2.12    1.70    4.05    29.17   29.17   29.17
10/1/2003       0.03    0.48    1.93    1.98    3.96    28.88   28.88   28.88
10/1/2004       0.03    0.45    1.76    1.42    3.91    28.60   28.60   28.60
10/1/2005       0.03    0.42    1.64    1.27    3.82    28.32   28.32   28.32
10/1/2006       0.03    0.42    1.53    1.13    3.74    28.26   28.26   28.26
10/1/2007       0.11    0.51    1.59    5.66    3.68    28.23   28.23   28.23
10/1/2008       0.16    0.45    1.70    3.40    3.62    27.84   27.84   27.84
10/1/2009       0.10    0.42    1.78    2.55    3.54    26.56   26.56   26.56"

   , as.is = TRUE
   , header = TRUE
   )

# setup margins
par(mar = c(5, 5, 3, 3))

# create x-axis with yearly tick marks starting in Oct
x_range <- seq(from = as.Date("2001-10-1")
            , to = as.Date("2011-10-1")
            , by = '1 year'
            )
# now create the format of month-year
x_label <- sprintf("%s-%02d", format(x_range, "%b"),
as.POSIXlt(x_range)$year %% 100)

# plot each column of data
for (i in names(flow)[-1]){  # ignore the "Date" column
   plot(as.Date(flow$Date, "%m/%d/%Y")
       , flow[[i]]  # column to plot
       ,type="l"
       ,xlab="date"
       ,ylab=expression("daily discharge (" * m^3/s * ")")
       ,main=i
       ,yaxs="i"
       , xaxs="i"
       , xaxt = 'n'
       , xlim = range(x_range)
       )

   axis(1, at = x_range, labels = x_label, las = 2)
}
On Sat, Dec 24, 2011 at 3:23 PM, jim holtman <jholtman at gmail.com> wrote: