Description Usage Arguments Details Author(s) See Also Examples
A fortify method for tis objects
1 2 |
x |
A |
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 |
dfNames |
A character vector of the names for the |
timeName |
A character vector of length one with the desired name for
the column of dates that will be created from the |
This function turns a tis
object into a data frame containing the
original time series plus a field of dates
adjusted by an ‘offset’,
so that the time series can be more easily plotted with ggplot2
.
Trevor Davis
fortify
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | if(require("ggplot2") && require("reshape")) {
# Examples of plotting tis series with ggplot2
require("datasets")
require("scales")
# 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(breaks = date_breaks("10 years"), labels = date_format("%Y"))
# multivariate example using the "melt trick"
Seatbelts.tis <- as.tis(Seatbelts[ , c("drivers", "front", "rear")])
Seatbelt.names <- c("Driver", "Front Seat Passenger", "Back Seat Passenger")
Seatbelts.df <- fortify(Seatbelts.tis, dfNames = Seatbelt.names,
timeName = "Time")
Seatbelts.dfm <- melt(Seatbelts.df, id.var = "Time", variable_name="type")
qplot( Time, 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("Monthly Road Casulties in the UK")
}
|
Loading required package: ggplot2
Loading required package: reshape
Loading required package: scales
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.