15.21 Converting a Time Series Object to Times and Values

15.21.1 Problem

You have a time series object that you wish to convert to numeric vectors representing the time and values at each time.

15.21.3 Discussion

Time series objects efficiently store information when there are observations at regular time intervals, but for use with ggplot, they need to be converted to a format that separately represents times and values for each observation.

Some time series objects are cyclical. The presidents data set, for example, contains four observations per year, one for each quarter:

To convert it to a two-column data frame with one column representing the year with fractional values, we can do the same as before:

It is also possible to store the year and quarter in separate columns, which may be useful in some visualizations:

15.21.4 See Also

The zoo package is also useful for working with time series objects.