zoo (version 1.7-9)

ggplot2.zoo: Convenience Functions for Plotting zoo Objects with ggplot2

Description

fortify.zoo takes a zoo object and converts it into a data frame (intended for ggplot2). autoplot.zoo takes a zoo object and returns a ggplot2 object. It essentially uses the mapping aes(x = Time, y = Value, group = Series) and adds colour = Series, linetype = Series, shape = Series in the case of a multivariate series with facets = NULL.

Usage

## S3 method for class 'zoo':
autoplot(object, geom = "line", facets, \dots)
  ## S3 method for class 'zoo':
fortify(model, data, melt = FALSE, \dots)
  facet_free(facets = Series ~ ., margins = FALSE, scales = "free_y", ...)

Arguments

object
an object of class "zoo".
geom
character specifying which geom to use in qplot.
facets
specification of facets for qplot. The default is to use facets = NULL for univariate series and facets = Series ~ . for multivariate series.
...
further arguments passed to qplot for autplot (and not used for fortify).
model
an object of class "zoo" to be converted to a "data.frame".
data
not used (required by generic fortify method).
melt
Should the resulting data frame be in long format (melt = TRUE) or wide format (melt = FALSE).
margins
As in facet_grid.
scales
As in facet_grid except it defaults to "free_y".

Value

  • fortify.zoo returns a data.frame either in long format (melt = TRUE) or in wide format (melt = FALSE). The long format has three columns: the time Index, a factor indicating the Series, and the corresponding Value. The wide format simply has the time Index plus all columns of coredata(model). autoplot.zoo returns a ggplot object.

Details

Convenience interface for visualizing zoo objects with ggplo2. autoplot.zoo uses fortify.zoo (with melt = TRUE) to convert the zoo object into a data frame and then uses a suitable aes() mapping to visiualize the series.

See Also

autoplot, fortify, qplot

Examples

Run this code
if(require("ggplot2")) {
## example data
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
x <- zoo(rnorm(5), x.Date)
xlow <- x - runif(5)
xhigh <- x + runif(5)
z <- cbind(x, xlow, xhigh)

## univariate plotting
autoplot(x)
## by hand
ggplot(aes(x = Index, y = Value), data = fortify(x, melt = TRUE)) + geom_line() + xlab("Index") + ylab("x")
## adding series one at a time
last_plot() + geom_line(aes(x = Index, y = xlow), colour = "red", data = fortify(xlow))
## add ribbon for high/low band
ggplot(aes(x = Index, y = x, ymin = xlow, ymax = xhigh), data = fortify(x)) + geom_ribbon(fill = "darkgray") + geom_line()

## multivariate plotting in multiple or single panels
autoplot(z)                      ## multiple without color/linetype
autoplot(z, facets = Series ~ .) ## multiple with series-dependent color/linetype
autoplot(z, facets = NULL)       ## single with series-dependent color/linetype
## by hand with color/linetype and with/without facets
qplot(x = Index, y = Value, group = Series, colour = Series, linetype = Series, facets = Series ~ ., data = fortify(z, melt = TRUE)) +
  geom_line() + xlab("Index") + ylab("")
ggplot(aes(x = Index, y = Value, group = Series, colour = Series, linetype = Series), data = fortify(z, melt = TRUE)) +
  geom_line() + xlab("Index") + ylab("")
## variations
autoplot(z, geom = "point")
autoplot(z, facets = NULL) + geom_point()
autoplot(z, facets = NULL) + scale_colour_grey() + theme_bw()

## grid facets with free y scale
autoplot(z) + aes(colour = NULL, linetype = NULL) + facet_grid(Series ~ ., scales = "free_y")
autoplot(z) + aes(colour = NULL, linetype = NULL) + facet_free()

## for "ts" series via coercion
autoplot(as.zoo(EuStockMarkets))
autoplot(as.zoo(EuStockMarkets), facets = NULL)
}

Run the code above in your browser using DataLab