Learn R Programming

berryFunctions (version 1.11.0)

spiralDate: Spiral graph of time series

Description

Plot seasonality of (daily) time series along spiral

Usage

spiralDate(dates, values, data, drange = NULL, vrange = NULL, months = substr(month.abb, 1, 1), add = FALSE, prop = NULL, zlab = substitute(values), format = "%Y", nint = 1, ...)

Arguments

dates
Dates in ascending order. Can be charater strings or strptime results, as accepted (and coerced) by as.Date
values
Values to be mapped in color with colPoints along seasonal spiral
data
Optional: data.frame with the column names as given by dates and values
drange
Optional date range (analogous to xlim), can be a vector like dates. DEFAULT: NULL
vrange
Optional value range (analogous to ylim), can be a vector like values. DEFAULT: NULL
months
Labels for the months. DEFAULT: J,F,M,A,M,J,J,A,S,O,N,D
add
Add to existing plot? DEFAULT: FALSE
prop
Proportion of the data to be actually plotted, used in spiralDateAnim. DEFAULT: NULL
zlab
format
Format of date labels see details in strptime. DEFAULT: "%Y"
nint
Number of interpolation segments between points, only used if lines=TRUE. DEFAULT: 1 (with long time series, the colPoints default of 30 is too high!)
...
Further arguments passed to colPoints, but not Range (use vrange)

Value

invisible data.frame with date, vals, and the plotting coordinates

See Also

seasonality, colPoints, as.Date

Examples

Run this code
# synthetic seasonal Data
set.seed(42)
fakeData <- data.frame(time = as.Date("1985-01-01")+0:5000,
                       vals = cumsum(rnorm(5001))+50          )
fakeData$vals <- fakeData$vals + sin(0:5000/366*2*pi)*max(abs(fakeData$vals))

sp <- spiralDate(time,vals, data=fakeData)
tail(sp)
spiralDate(time,vals, data=fakeData, drange=as.Date(c("1980-01-01", "2004-11-15")), lines=TRUE)

par(mfrow=c(1,3), mar=c(3,3,6,1), mgp=c(2,0.6,0), las=1)
colPoints(time,vals,vals, data=fakeData, col=divPal(100), add=FALSE, legend=FALSE,
          lines=TRUE, pch=NA, nint=1, lwd=2)
title(main="classical time series\nworks badly for long time series\nshows trends well")

fakeData$Year <- as.numeric(format(fakeData$time,"%Y"))
fakeData$DOY  <- as.numeric(format(fakeData$time,"%j")) # Day of Year
colPoints(Year, DOY, vals, data=fakeData, add=FALSE, zlab="Daily mean discharge",
           ylim=c(366,0), col=divPal(100), legend=FALSE)
title(main="yearly time series\nday of year over time\nfails for cyclicity over the winter")

spiralDate(time,vals, data=fakeData, col=divPal(100), legargs=list(y1=70,y2=80))
title(main="spiral graph\nshows cyclic values nicely 
            trends are harder to detect\nrecent values = more visual weight")

par(mfrow=c(1,1))

# Data with missing values:
fakeData[1300:1500, 2] <- NA
spiralDate(time,vals, data=fakeData, lines=TRUE) # no problem
# Missing data:
fakeData <- na.omit(fakeData)
spiralDate(time,vals, data=fakeData, lines=TRUE) # problematic for lines
spiralDate(time,vals, data=fakeData, pch=3)      # but not for points

## Real data:    
#library2("waterData")
#data(exampleWaterData)
#spiralDate(dates, val, data=q05054000LT, lines=TRUE, lwd=3)

Run the code above in your browser using DataLab