Hi:

I'm a little more familiar with ggplot2 than zoo for graphing multivariate
time series, so my response is based on that bias.

On Sat, Jul 17, 2010 at 11:41 AM, linda.s <samrobertsm...@gmail.com> wrote:

> i am a beginner and tried to provide a reproducible example. is the
> following style a correct one?
>

It's beautiful. Thank you!


> Thanks.
>
> > dput(unem)
> > structure(list(a = c(10.2, 9.8, 9.5, 8.3, 7.9, 8.8, 8.9, 9.3,
> + 9.2, 9, 9.5, 12, 15.7, 16.1, 15.4, 14.7, 13.9, 15.3, 15.4, 15,
> + 13.8, 13.9, 14.1, 15.8), b = c(7, 6.7, 6.8, 6.1, 6.5, 7.4, 8.4,
> + 7.6, 7.5, 7.5, 7.8, 9.1, 11.2, 12.1, 12.2, 11.5, 11.5, 11.7,
> + 11.7, 11.2, 10.3, 10.7, 10.8, 11.6), c = c(6.5, 5.9, 5.9, 5.4,
> + 6.1, 6.6, 7.6, 7.2, 6.9, 7.1, 7.7, 8.4, 11.6, 11.3, 11, 10.9,
> + 12, 12.7, 12.8, 11, 10, 10.1, 10.3, 11.1), d = c(8.3, 7.6, 7.3,
> + 6.2, 6.2, 7.1, 8.5, 8.3, 7.7, 7.3, 8, 10.2, 13.9, 14.9, 14.8,
> + 13.1, 13.1, 13.3, 13.3, 12.1, 11.1, 11.3, 11.6, 12.7)), .Names = c("a",
> + "b", "c", "d"), class = "data.frame", row.names = c("JAN_08",
> + "FEB_08", "MAR_08", "APR_08", "MAY_08", "JUN_08", "JULY_08",
> + "AUG_08", "SEP_08", "OCT_08", "NOV_08", "DEC_08", "JAN_09", "FEB_09",
> + "MAR_09", "APR_09", "MAY_09", "JUN_09", "JUL_09", "AUG_09", "SEP_09",
> + "OCT_09", "NOV_09", "DEC_09"))
>           a    b    c    d
> JAN_08  10.2  7.0  6.5  8.3
> FEB_08   9.8  6.7  5.9  7.6
> MAR_08   9.5  6.8  5.9  7.3
> APR_08   8.3  6.1  5.4  6.2
> MAY_08   7.9  6.5  6.1  6.2
> JUN_08   8.8  7.4  6.6  7.1
> JULY_08  8.9  8.4  7.6  8.5
> AUG_08   9.3  7.6  7.2  8.3
> SEP_08   9.2  7.5  6.9  7.7
> OCT_08   9.0  7.5  7.1  7.3
> NOV_08   9.5  7.8  7.7  8.0
> DEC_08  12.0  9.1  8.4 10.2
> JAN_09  15.7 11.2 11.6 13.9
> FEB_09  16.1 12.1 11.3 14.9
> MAR_09  15.4 12.2 11.0 14.8
> APR_09  14.7 11.5 10.9 13.1
> MAY_09  13.9 11.5 12.0 13.1
> JUN_09  15.3 11.7 12.7 13.3
> JUL_09  15.4 11.7 12.8 13.3
> AUG_09  15.0 11.2 11.0 12.1
> SEP_09  13.8 10.3 10.0 11.1
> OCT_09  13.9 10.7 10.1 11.3
> NOV_09  14.1 10.8 10.3 11.6
> DEC_09  15.8 11.6 11.1 12.7
> > attach(unem)
> The following object(s) are masked from 'unem (position 3)':
>
>    a, b, c, d
> The following object(s) are masked from 'unem (position 4)':
>
>    a, b, c, d
>

This is one reason why attaching data frames that you created from objects
in your workspace is generally not a good idea. A cleaner approach is to use
with(df, {code}) instead, where {code} represents the function(s) you want
to apply to the (temporarily) attached data frame df. See ?with for details
and examples.

One way to create a multivariate ts object is as follows:

unemp <- ts(unem, start = c(2008, 1), end = c(2009, 12), freq = 12)
plot(unemp)

Unfortunately, the time labels are not what you wanted and an attempt to
suppress the x-axis on the multiple ts plot was futile. So, move on to plan
B.

> unem1 <- ts(unem$a, start = c(2008, 1), freq = 12)
> > plot(unem1, type = "o")
>
> Question:
> The X axis on the plot now starts from 2008.0; Since the data starts
> from January 2008, can I make it 2008.1, and also show 2009.12 on the
> axis?
>

2008.1 - 2009.12 doesn't thrill me; let's look at what we can get from the
graphics package ggplot2. If you don't already have it on your system,
install it from CRAN first.

library(ggplot2)

# Stack the time series into a vector labeled 'value' with series
identifiers
# put into a factor called 'variable'. This comes in handy when we
# plot and add the facet_grid() layer to the graph below.
munemp <- melt(unem)

# Create a vector of dates, convert them to class Date, and add to munemp
dts <- c(paste('2008-', 1:12, '-1', sep = ''), paste('2009-', 1:12, '-1',
    sep = ''))
> dts   # what it looks like
 [1] "2008-1-1"  "2008-2-1"  "2008-3-1"  "2008-4-1"  "2008-5-1"  "2008-6-1"
 [7] "2008-7-1"  "2008-8-1"  "2008-9-1"  "2008-10-1" "2008-11-1" "2008-12-1"
[13] "2009-1-1"  "2009-2-1"  "2009-3-1"  "2009-4-1"  "2009-5-1"  "2009-6-1"
[19] "2009-7-1"  "2009-8-1"  "2009-9-1"  "2009-10-1" "2009-11-1" "2009-12-1"
dates <- as.Date(dts)   # convert to class Date
munemp$dates <- rep(dates, 4)  # repeat each time for the four series

# ggplot is a system of adding graphical layers to a plot.
# Create the 'scaffold' - data frame, x and y variables
p <- ggplot(munemp, aes(x = dates, y = value))
# Add layers - line plots stacked on top one another with axis labels
p + geom_line() + facet_grid(variable ~ .) + xlab('') +
    ylab('Unemployment (%)')

I don't know about you, but I like the default labeling in ggplot2 better
than that from the first plot.

There is sure to be a nice way to do this in package zoo. As I'm in the
beginning phases of learning it, I'm looking forward to solutions from Achim
and/or Gabor to further my appreciation of their excellent package (no
pressure :).


HTH,
Dennis

Thanks.
>
> On Wed, Jul 14, 2010 at 9:49 AM, Achim Zeileis <achim.zeil...@uibk.ac.at>
> wrote:
> > You do not provide a reproducible example, as the posting guide asks you
> to.
> > But I guess that your time series setup using ts() is insufficient, see
> ?ts.
> > If the data starts in January 2008, why do you tell R that it starts in
> 1?
> > Presumably you have monthly data and
> >
> >  unem1 <- ts(unem$a, start = c(2008, 1), freq = 12)
> >  plot(unem1, type = "o")
> >
> > is what you want.
> >
> > hth,
> > Z
> >
> > On Wed, 14 Jul 2010, linda.s wrote:
> >
> >> R Code begins
> >> unem=read.csv("book5.csv",header=T,row.names=1)
> >> attach(unem)
> >> unem1=ts(unem$a, start=1)
> >> ts.plot(unem1,main="a")
> >> points(unem1,type="o")
> >> R Code ends
> >>
> >> because the time starts at JAN_08 and ends on DEC_09, how to make the
> >> y axis in the plot show month starting from JAN_08 instead of having
> >> the current
> >> ugly appearance (5, 10, 15, 20,?)?
> >>
> >> On Wed, Jul 14, 2010 at 9:20 AM, linda.s <samrobertsm...@gmail.com>
> wrote:
> >>>
> >>> R Code begins
> >>> unem=read.csv("book5.csv",header=T,row.names=1)
> >>> attach(unem)
> >>> unem1=ts(unem$Allen, start=1)
> >>> ts.plot(unem1,main="Allen")
> >>> points(unem1,type="o")
> >>> R Code ends
> >>>
> >>> because the time starts at JAN_08 and ends on DEC_09, how to make the
> >>> y axis in the plot show month starting from JAN_08 instead of having
> >>> the current
> >>> ugly appearance (5, 10, 15, 20,?)?
> >>>
> >>
> >
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to