EVOLUTION-MANAGER
Edit File: ggplot2.zoo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Convenience Functions for Plotting zoo Objects with ggplot2</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for ggplot2.zoo {zoo}"><tr><td>ggplot2.zoo {zoo}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Convenience Functions for Plotting zoo Objects with ggplot2</h2> <h3>Description</h3> <p><code>fortify.zoo</code> takes a zoo object and converts it into a data frame (intended for ggplot2). <code>autoplot.zoo</code> takes a zoo object and returns a ggplot2 object. It essentially uses the mapping <code>aes(x = Time, y = Value, group = Series)</code> and adds <code>colour = Series</code>, <code>linetype = Series</code>, <code>shape = Series</code> in the case of a multivariate series with <code>facets = NULL</code>. </p> <h3>Usage</h3> <pre> ## S3 method for class 'zoo' autoplot(object, geom = "line", facets, ...) ## S3 method for class 'zoo' fortify(model, data, names = c("Index", "Series", "Value"), melt = FALSE, sep = NULL, ...) facet_free(facets = Series ~ ., margins = FALSE, scales = "free_y", ...) yearmon_trans(format = "%b %Y", n = 5) scale_x_yearmon(..., format = "%b %Y", n = 5) scale_y_yearmon(..., format = "%b %Y", n = 5) yearqtr_trans(format = "%Y-%q", n = 5) scale_x_yearqtr(..., format = "%Y-%q", n = 5) scale_y_yearqtr(..., format = "%Y-%q", n = 5) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>object</code></td> <td> <p>an object of class <code>"zoo"</code>.</p> </td></tr> <tr valign="top"><td><code>geom</code></td> <td> <p>character specifying which <code>geom</code> to use in <code><a href="../../ggplot2/html/qplot.html">qplot</a></code>.</p> </td></tr> <tr valign="top"><td><code>facets</code></td> <td> <p>specification of <code>facets</code> for <code><a href="../../ggplot2/html/qplot.html">qplot</a></code>. The default in the <code>autoplot</code> method is to use <code>facets = NULL</code> for univariate series and <code>facets = Series ~ .</code> for multivariate series.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments passed to <code><a href="../../ggplot2/html/qplot.html">qplot</a></code> for <code>autoplot</code> (and not used for <code>fortify</code>). For the <code>scale_*_*</code> functions the arguments are passed on to <code>scale_*_continuous</code>.</p> </td></tr> <tr valign="top"><td><code>model</code></td> <td> <p>an object of class <code>"zoo"</code> to be converted to a <code>"data.frame"</code>.</p> </td></tr> <tr valign="top"><td><code>data</code></td> <td> <p>not used (required by generic <code><a href="../../ggplot2/html/fortify.html">fortify</a></code> method).</p> </td></tr> <tr valign="top"><td><code>names</code></td> <td> <p>(list of) character vector(s). New names given to index/time column, series indicator (if melted), and value column (if melted). If only a subset of characters should be changed, either NAs can be used or a named vector.</p> </td></tr> <tr valign="top"><td><code>sep</code></td> <td> <p>If specified then the Series column is split into multiple columns using sep as the split character.</p> </td></tr> <tr valign="top"><td><code>melt</code></td> <td> <p>Should the resulting data frame be in long format (<code>melt = TRUE</code>) or wide format (<code>melt = FALSE</code>).</p> </td></tr> <tr valign="top"><td><code>margins</code></td> <td> <p>As in <code><a href="../../ggplot2/html/facet_grid.html">facet_grid</a></code>.</p> </td></tr> <tr valign="top"><td><code>scales</code></td> <td> <p>As in <code><a href="../../ggplot2/html/facet_grid.html">facet_grid</a></code> except it defaults to <code>"free_y"</code>.</p> </td></tr> <tr valign="top"><td><code>format</code></td> <td> <p>A format acceptable to <a href="yearmon.html">format.yearmon</a> or <a href="yearqtr.html">format.yearqtr</a>.</p> </td></tr> <tr valign="top"><td><code>n</code></td> <td> <p>Approximate number of axis ticks.</p> </td></tr> </table> <h3>Details</h3> <p>Convenience interface for visualizing zoo objects with ggplot2. <code>autoplot.zoo</code> uses <code>fortify.zoo</code> (with <code>melt = TRUE</code>) to convert the zoo object into a data frame and then uses a suitable <code>aes()</code> mapping to visiualize the series. </p> <h3>Value</h3> <p><code>fortify.zoo</code> returns a <code>data.frame</code> either in long format (<code>melt = TRUE</code>) or in wide format (<code>melt = FALSE</code>). The long format has three columns: the time <code>Index</code>, a factor indicating the <code>Series</code>, and the corresponding <code>Value</code>. The wide format simply has the time <code>Index</code> plus all columns of <code>coredata(model)</code>. </p> <p><code>autoplot.zoo</code> returns a <code>ggplot</code> object. </p> <h3>Author(s)</h3> <p>Trevor L. Davis <a href="mailto:trevor.l.davis@gmail.com">trevor.l.davis@gmail.com</a>, Achim Zeileis </p> <h3>See Also</h3> <p><code><a href="../../ggplot2/html/autoplot.html">autoplot</a></code>, <code><a href="../../ggplot2/html/fortify.html">fortify</a></code>, <code><a href="../../ggplot2/html/qplot.html">qplot</a></code> </p> <h3>Examples</h3> <pre> if(require("ggplot2") && require("scales")) { suppressWarnings(RNGversion("3.5.0")) set.seed(1) ## 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() ## for "ts" series via coercion autoplot(as.zoo(EuStockMarkets)) autoplot(as.zoo(EuStockMarkets), facets = NULL) autoplot(z) + aes(colour = NULL, linetype = NULL) + facet_grid(Series ~ ., scales = "free_y") autoplot(z) + aes(colour = NULL, linetype = NULL) + facet_free() # same z.yq <- zooreg(rnorm(50), as.yearqtr("2000-1"), freq = 4) autoplot(z.yq) ## mimic matplot data <- cbind(A = c(6, 1, NA, NA), B = c(16, 4, 1, NA), C = c(25, 7, 2, 1)) autoplot(zoo(data), facet = NULL) + geom_point() ## with different line types autoplot(zoo(data), facet = NULL) + geom_point() + aes(linetype = Series) ## illustrate just fortify() method z <- zoo(data) fortify(z) fortify(z, melt = TRUE) fortify(z, melt = TRUE, names = c("Time", NA, "Data")) fortify(z, melt = TRUE, names = c(Index = "Time")) ## with/without splitting z <- zoo(cbind(a.A = 1:2, a.B = 2:3, b.A = 3:4, c.B = 4:5)) fortify(z) fortify(z, melt = TRUE, sep = ".", names = list(Series = c("Lower", "Upper"))) ## scale_x_yearmon with custom discrete breaks df <- data.frame(dates = as.yearmon("2018-08") + 0:6/12, values = c(2:6, 0, 1)) ggdf <- ggplot(df, aes(x = dates, y = values)) + geom_bar(position = "dodge", stat = "identity") + theme_light() + xlab("Month") + ylab("Values") ggdf ## with default scale_x_yearmon ggdf + scale_x_yearmon(breaks = df$dates) ## with custom discrete breaks } </pre> <hr /><div style="text-align: center;">[Package <em>zoo</em> version 1.8-11 <a href="00Index.html">Index</a>]</div> </body></html>