| fortify.tis {tis} | R Documentation |
A fortify method for tis objects
## S3 method for class 'tis':
fortify(x, offset = 0.5, dfNames=ifelse(is.null(dim(x)), as.character(substitute(x)), NA))
x |
A tis object of time series |
offset |
A number between 0 and 1 specifying where in the period of time
represented by the 'ti(x)' the points should eventually be plotted in ggplot2.
'offset = 0' gives the beginning of the period and 'offset
= 1' the end of the period, 'offset = 0.5' the middle of the period, and
so on. For example if x is a tis object of quarterly time series and offset = 0.5
then the resulting plotted points would fall in the middle of each quarter.
offset is passed on to POSIXct(ti(x), offset=offset)
and used to create the field date in the resulting data frame.
|
dfNames |
A character vector of the names for the tis objects contained in x. Defaults to the name of the tis object in the univariate case and the column names of the tis object in the multivariate case. |
This function turns a tis object into a data frame containing the original time series plus a field of dates, called date
and adjusted by an ‘offset’,
so that the time series can be more easily plotted with ggplot2.
Trevor Davis
if(require("ggplot2")) {
# Examples of plotting tis series with ggplot2
require("datasets")
# univariate example
num_discoveries <- as.tis(discoveries)
ggplot(data = fortify(num_discoveries, offset=0)) +
geom_line(aes(x=date, y=num_discoveries)) +
scale_x_date(major="10 years")
# multivariate example using the "melt trick"
Seatbelts.tis <- as.tis(Seatbelts[ , c("drivers", "front", "rear")])
Seatbelts.df <- fortify(Seatbelts.tis)
Seatbelts.dfm <- melt(Seatbelts.df, id.var = "date", variable_name="type")
qplot( date, value, data = Seatbelts.dfm, geom="line",
group=type, colour=type, linetype=type ) +
geom_vline(xintercept=as.numeric(as.Date("1983-01-31")),
colour="black", linetype="dashed") +
ylab("Road Casulties in the UK")
}