## example dates
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
## univariate plotting
x <- zoo(rnorm(5), x.Date)
x2 <- zoo(rnorm(5, sd = 0.2), x.Date)
plot(x)
lines(x2, col = 2)
## multivariate plotting
z <- cbind(x, x2, zoo(rnorm(5, sd = 0.5), x.Date))
plot(z, type = "b", pch = 1:3, col = 1:3, ylab = list(expression(mu), "b", "c"))
colnames(z) <- LETTERS[1:3]
plot(z, screens = 1, col = list(B = 2))
plot(z, type = "b", pch = 1:3, col = 1:3)
plot(z, type = "b", pch = list(A = 1:5, B = 3), col = list(C = 4, 2))
plot(z, type = "b", screen = c(1,2,1), col = 1:3)
# right axis is for broken lines
plot(x)
opar <- par(usr = c(par("usr")[1:2], range(x2)))
lines(x2, lty = 2)
# axis(4)
Axis(side = 4)
par(opar)
## Custom x axis labelling using a custom panel.
# 1. test data
z <- zoo(c(21, 34, 33, 41, 39, 38, 37, 28, 33, 40),
as.Date(c("1992-01-10", "1992-01-17", "1992-01-24", "1992-01-31",
"1992-02-07", "1992-02-14", "1992-02-21", "1992-02-28", "1992-03-06",
"1992-03-13")))
zz <- merge(a = z, b = z+10)
# 2. axis tick for every point. Also every 3rd point labelled.
my.panel <- function(x, y, ..., pf = parent.frame()) {
fmt <- "%b-%d" # format for axis labels
lines(x, y, ...)
# if bottom panel
if (with(pf, length(panel.number) == 0 ||
panel.number %% nr == 0 || panel.number == nser)) {
# create ticks at x values and then label every third tick
Axis(side = 1, at = x, labels = FALSE)
ix <- seq(1, length(x), 3)
labs <- format(x, fmt)
Axis(side = 1, at = x[ix], labels = labs[ix], tcl = -0.7, cex.axis = 0.7)
}
}
# 3. plot
plot(zz, panel = my.panel, xaxt = "n")
# with a single panel plot a fancy x-axis is just the same
# procedure as for the ordinary plot command
plot(zz, screen = 1, col = 1:2, xaxt = "n")
# axis(1, at = time(zz), labels = FALSE)
tt <- time(zz)
Axis(side = 1, at = tt, labels = FALSE)
ix <- seq(1, length(tt), 3)
fmt <- "%b-%d" # format for axis labels
labs <- format(tt, fmt)
# axis(1, at = time(zz)[ix], labels = labs[ix], tcl = -0.7, cex.axis = 0.7)
Axis(side = 1, at = tt[ix], labels = labs[ix], tcl = -0.7, cex.axis = 0.7)
legend("bottomright", colnames(zz), lty = 1, col = 1:2)
## plot a mulitple ts series with nice x-axis using panel function
tab <- ts(cbind(A = 1:24, B = 24:1), start = c(2006, 1), freq = 12)
pnl.xaxis <- function(...) {
lines(...)
panel.number <- parent.frame()$panel.number
nser <- parent.frame()$nser
# if bottom panel
if (!length(panel.number) || panel.number == nser) {
tt <- list(...)[[1]]
ym <- as.yearmon(tt)
mon <- as.numeric(format(ym, "%m"))
yy <- format(ym, "%y")
mm <- substring(month.abb[mon], 1, 1)
# axis(1, tt[mon == 1], yy[mon == 1], cex.axis = 0.7)
Axis(side = 1, at = tt[mon == 1], labels = yy[mon == 1], cex.axis = 0.7)
# axis(1, tt[mon > 1], mm[mon > 1], cex.axis = 0.5, tcl = -0.3)
Axis(side = 1, at = tt[mon > 1], labels = mm[mon > 1], cex.axis = 0.5, tcl = -0.3)
}
}
plot(as.zoo(tab), panel = pnl.xaxis, xaxt = "n", main = "Fancy X Axis")
## Another example with a custom axis
# test data
z <- zoo(matrix(1:25, 5), c(10,11,20,21))
colnames(z) <- letters[1:5]
plot(zoo(coredata(z)), xaxt = "n", panel = function(x, y, ..., Time = time(z)) {
lines(x, y, ...)
# if bottom panel
pf <- parent.frame()
if (with(pf, panel.number %% nr == 0 || panel.number == nser)) {
Axis(side = 1, at = x, labels = Time)
}
})
## plot with left and right axes
## modified from http://www.mayin.org/ajayshah/KB/R/html/g6.html
set.seed(1)
z <- zoo(cbind(A = cumsum(rnorm(100)), B = cumsum(rnorm(100, mean = 0.2))))
opar <- par(mai = c(.8, .8, .2, .8))
plot(z[,1], type = "l",
xlab = "x-axis label", ylab = colnames(z)[1])
par(new = TRUE)
plot(z[,2], type = "l", ann = FALSE, yaxt = "n", col = "blue")
# axis(4)
Axis(side = 4)
legend(x = "topleft", bty = "n", lty = c(1,1), col = c("black", "blue"),
legend = paste(colnames(z), c("(left scale)", "(right scale)")))
usr <- par("usr")
# if you don't care about srt= in text then mtext is shorter:
# mtext(colnames(z)[2], 4, 2, col = "blue")
text(usr[2] + .1 * diff(usr[1:2]), mean(usr[3:4]), colnames(z)[2],
srt = -90, xpd = TRUE, col = "blue")
par(opar)
# automatically placed point labels
library("maptools")
pointLabel(time(z), coredata(z[,2]), labels = format(time(z)), cex = 0.5)
## plot one zoo series against the other.
plot(x, x2)
plot(x, x2, xy.labels = TRUE)
plot(x, x2, xy.labels = 1:5, xy.lines = FALSE)
## barplot
x <- zoo(cbind(rpois(5, 2), rpois(5, 3)), x.Date)
barplot(x, beside = TRUE)
# interactive plotting
library("TeachingDemos")
tke.test1 <- list(Parameters = list(
pch = list("spinbox", init = 1, from = 0, to = 255, width = 5),
cex = list("slider", init = 1.5, from = 0.1, to = 5, resolution = 0.1),
type = list("combobox", init = "b",
values = c("p", "l", "b", "o", "c", "h", "s", "S", "n"), width = 5),
lwd = list("spinbox", init = 1, from = 0, to = 5, increment = 1, width = 5),
lty = list("spinbox", init = 1, from = 0, to = 6, increment = 1, width = 5)
))
z <- zoo(rnorm(25))
tkexamp(plot(z), tke.test1, plotloc = "top")
Run the code above in your browser using DataLab