[Xts-commits] r617 - in pkg/xtsExtra: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue May 22 06:13:22 CEST 2012
Author: weylandt
Date: 2012-05-22 06:13:22 +0200 (Tue, 22 May 2012)
New Revision: 617
Added:
pkg/xtsExtra/R/plot.R
pkg/xtsExtra/man/plot.xts.Rd
Log:
Add scatterplot to plot.xts -- should handle par() args better; probably broke regular plots
Added: pkg/xtsExtra/R/plot.R
===================================================================
--- pkg/xtsExtra/R/plot.R (rev 0)
+++ pkg/xtsExtra/R/plot.R 2012-05-22 04:13:22 UTC (rev 617)
@@ -0,0 +1,134 @@
+#
+# xtsExtra: Extensions to xts during GSOC-2012
+#
+# Copyright (C) 2012 Michael Weylandt: michael.weylandt at gmail.com
+#
+# Scatterplot code taken from plot.zoo in the CRAN zoo package
+#
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# SHOULD REMOVE par() ARGS FROM FORMALS AND INSTEAD TREAT ... BETTER
+`plot.xts` <- function(x, y = NULL,
+ screens, screens.layout,
+ type, auto.grid=TRUE,
+ major.ticks='auto', minor.ticks=TRUE,
+ major.format=TRUE,
+ bar.col='grey', candle.col='white',
+ ann, axes, xlab, ylab, main, xlim, ylim,
+ xy.labels = FALSE, xy.lines = NULL,
+ ...) {
+
+ ## if y supplied: scatter plot y ~ x
+ if(!is.null(y)) {
+ if(NCOL(x) > 1 || NCOL(y) > 1) stop("Scatter plots only for univariate series")
+
+ # Catch these early enough?
+ xlab <- if(missing(xlab)) deparse(substitute(x)) else xlab
+ ylab <- if(missing(ylab)) deparse(substitute(y)) else ylab
+
+ x <- try.xts(x); y <- try.xts(y)
+
+ xy.xts <- merge(x, y, join = "inner")
+
+ xy <- coredata(xy.xts)
+
+ xy <- xy.coords(xy[,1], xy[,2])
+
+ xlim <- if(missing(xlim)) range(xy$x[is.finite(xy$x)]) else xlim
+ ylim <- if(missing(ylim)) range(xy$y[is.finite(xy$y)]) else ylim
+
+ if(missing(main)) main <- paste(xlab, "vs.", ylab)
+
+ do.lab <- if(is.logical(xy.labels)) xy.labels else TRUE
+
+ if(is.null(xy.lines)) xy.lines <- do.lab
+
+ ptype <- if(do.lab) "n" else if(missing(type)) "p" else type
+
+ plot.default(xy, type = ptype, main = main, xlab = xlab,
+ ylab = ylab, xlim = xlim, ylim = ylim, ...)
+
+ if(do.lab) text(xy,
+ labels = if(!is.logical(xy.labels)) xy.labels else index2char(index(xy.xts)), ...)
+ if(xy.lines) lines(xy, type = if(do.lab) "c" else "l", ...)
+
+ return(invisible(xy.xts))
+ }
+
+ ## Else : no y, only x
+ x <- try.xts(x)
+
+ # By default one screen per panel
+ if(missing(screens)){
+ screens <- 1:NCOL(x)
+ }
+
+ if(missing(screens.layout)){
+ screens.layout <- seq_along(unique(screens))
+ }
+
+ #####
+ #
+ # SOME CODE TO MAKE SURE screens.layout IS LEGAL
+ #
+ #####
+
+
+ #time.scale <- periodicity(x)$scale
+ ep <- axTicksByTime(x,major.ticks, format.labels=major.format)
+
+ otype <- type
+
+ if(xts:::is.OHLC(x) && type %in% c('candles','bars')) {
+ x <- x[,xts:::has.OHLC(x, TRUE)]
+ xycoords <- list(x=.index(x), y=seq(min(x),max(x),length.out=NROW(x)))
+ type <- "n"
+ } else {
+ if(NCOL(x) > 1) warning('only the univariate series will be plotted')
+ if(is.null(y))
+ xycoords <- xy.coords(.index(x), x[,1])
+ }
+
+ plot(xycoords$x, xycoords$y, type=type, axes=FALSE, ann=FALSE, ...)
+
+ if(auto.grid) {
+ abline(v=xycoords$x[ep], col='grey', lty=4)
+ grid(NA,NULL)
+ }
+
+ if(xts:::is.OHLC(x) && otype == 'candles')
+ xts:::plot.ohlc.candles(x, bar.col=bar.col, candle.col=candle.col, ...)
+
+ dots <- list(...)
+
+# if('axes' %in% names(dots)) {
+# if(!dots$axes) axes <- FALSE
+# } else axes <- TRUE
+
+ if(axes) {
+ if(minor.ticks)
+ axis(1, at=xycoords$x, labels=FALSE, col='#BBBBBB', ...)
+ axis(1, at=xycoords$x[ep], labels=names(ep), las=1, lwd=1, mgp=c(3,2,0),...)
+ axis(2, ...)
+ }
+
+ box()
+
+ if(!'main' %in% names(dots)) title(main)
+ do.call('title',list(...))
+ assign(".plot.xts",recordPlot(),.GlobalEnv)
+
+ return(invisible(reclass(x)))
+}
Added: pkg/xtsExtra/man/plot.xts.Rd
===================================================================
--- pkg/xtsExtra/man/plot.xts.Rd (rev 0)
+++ pkg/xtsExtra/man/plot.xts.Rd 2012-05-22 04:13:22 UTC (rev 617)
@@ -0,0 +1,58 @@
+\name{plot.xts}
+\alias{plot.xts}
+\title{ Plotting xts Objects }
+\description{
+Plotting methods for xts objects.
+}
+\usage{
+\method{plot}{xts}(x, y = NULL,
+ screens, screens.layout,
+ type, auto.grid=TRUE,
+ major.ticks='auto', minor.ticks=TRUE,
+ major.format=TRUE,
+ bar.col='grey', candle.col='white',
+ ann, axes,
+ xlab, ylab, main,
+ xlim, ylim,
+ xy.labels = FALSE, xy.lines = NULL,
+ ...)
+}
+\arguments{
+ \item{x}{ an \code{xts} object }
+ \item{y}{ an \code{xts} object or NULL }
+ \item{screens}{ factor (or coerced to factor) whose levels specify which graph each series is to
+ be plotted in. If not specified, then defaults to a single series per screen for
+ \code{plot.type} not \code{OHLC}. See examples.}
+ \item{screens.layout}{ Matrix (in a form that could be passed to layout) which arranges screens.}
+ \item{type}{ type of plot to produce }
+ \item{auto.grid}{ should grid lines be drawn }
+ \item{major.ticks}{ should major tickmarks be drawn and labeled }
+ \item{minor.ticks}{ should minor tickmarks be drawn }
+ \item{major.format}{ passed along to axTicksByTime. See also }
+ \item{bar.col}{ the color of the bars when type is \sQuote{bars} or \sQuote{candles} }
+ \item{candle.col}{ the color of the candles when type is \sQuote{candles} }
+ \item{ann, axes, xlab, ylab, main, xlim, ylim}{ passed \sQuote{par} graphical parameters }
+ \item{xy.labels}{ label points in scatterplot?}
+ \item{xy.lines}{ connect points in scatterplot?}
+ \item{\dots}{ additional graphical arguments }
+}
+\details{
+Mainly used to draw time-series plots with sensible x-axis labels, it
+can also plot basic OHLC series using \code{type='candles'} or \code{type='bars'}.
+
+Better financial plots can be found in the \pkg{quantmod} package, though
+these are generally incompatible with standard R graphics tools.
+}
+\value{
+Plots an xts object to the current device.
+}
+\author{ Jeffrey A. Ryan }
+\examples{
+#data(sample_matrix)
+#plot(sample_matrix)
+#plot(as.xts(sample_matrix))
+#plot(as.xts(sample_matrix), type='candles')
+}
+% Add one or more standard keywords, see file 'KEYWORDS' in the
+% R documentation directory.
+\keyword{ hplot }
More information about the Xts-commits
mailing list