Title: | Supporting Graphs for Analysing Time Series |
---|---|
Description: | Provides 'ggplot2' graphics for analysing time series data. It aims to fit into the 'tidyverse' and grammar of graphics framework for handling temporal data. |
Authors: | Earo Wang [aut, cre] , Di Cook [aut, ths] , Rob Hyndman [aut, ths] |
Maintainer: | Earo Wang <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.2.9 |
Built: | 2024-11-07 03:57:25 UTC |
Source: | https://github.com/earowang/sugrrants |
Provides 'ggplot2' graphics for analysing time series data. It aims to fit into the 'tidyverse' and grammar of graphics framework for handling temporal data.
Maintainer: Earo Wang [email protected] (ORCID)
Authors:
Useful links:
Lay out panels in a calendar format
facet_calendar( date, format = "%b %d", week_start = getOption("lubridate.week.start", 1), nrow = NULL, ncol = NULL, scales = "fixed", shrink = TRUE, dir = "h", labeller = "label_value", strip.position = "top" )
facet_calendar( date, format = "%b %d", week_start = getOption("lubridate.week.start", 1), nrow = NULL, ncol = NULL, scales = "fixed", shrink = TRUE, dir = "h", labeller = "label_value", strip.position = "top" )
date |
A variable that contains dates or an expression that generates dates will be mapped in the calendar. |
format |
A character string, such as |
week_start |
Day on which week starts following ISO conventions -
1 means Monday (default), 7 means Sunday. You can set |
nrow , ncol
|
Number of rows and columns defined for "monthly" calendar
layout. If |
scales |
Should scales be fixed ( |
shrink |
If |
dir |
Direction of calendar: "h" for horizontal (the default) or "v" for vertical. |
labeller |
A function that takes one data frame of labels and
returns a list or data frame of character vectors. Each input
column corresponds to one factor. Thus there will be more than
one with |
strip.position |
By default, the labels are displayed on the top of
the plot. Using |
A monthly calendar is set up as a 5 by 7 layout matrix. Each month could extend over six weeks but in these months is to wrap the last few days up to the top row of the block.
frame_calendar for a compact calendar display, by quickly transforming the data.
fs <- hourly_peds %>% dplyr::filter(Date < as.Date("2016-05-01")) fs %>% ggplot(aes(x = Time, y = Hourly_Counts)) + geom_line(aes(colour = Sensor_Name)) + facet_calendar(~ Date, nrow = 2) + # or ~ as.Date(Date_Time) theme(legend.position = "bottom")
fs <- hourly_peds %>% dplyr::filter(Date < as.Date("2016-05-01")) fs %>% ggplot(aes(x = Time, y = Hourly_Counts)) + geom_line(aes(colour = Sensor_Name)) + facet_calendar(~ Date, nrow = 2) + # or ~ as.Date(Date_Time) theme(legend.position = "bottom")
Temporal data of daily intervals or higher frequency levels can be organised
into a calendar-based format, which is useful for visually presenting
calendar-related activities or multiple seasonality (such as time of day,
day of week, day of month). The function only returns a rearranged data frame,
and ggplot2
takes care of the plotting afterwards. It allows more
flexibility for users to visualise the data in various ways.
frame_calendar( data, x, y, date, calendar = "monthly", dir = "h", week_start = getOption("lubridate.week.start", 1), nrow = NULL, ncol = NULL, polar = FALSE, scale = "fixed", width = 0.95, height = 0.95, margin = NULL, ... ) prettify(plot, label = c("label", "text"), locale, abbr = TRUE, ...)
frame_calendar( data, x, y, date, calendar = "monthly", dir = "h", week_start = getOption("lubridate.week.start", 1), nrow = NULL, ncol = NULL, polar = FALSE, scale = "fixed", width = 0.95, height = 0.95, margin = NULL, ... ) prettify(plot, label = c("label", "text"), locale, abbr = TRUE, ...)
data |
A data frame or a grouped data frame including a |
x |
A bare (or unquoted) variable mapping to x axis, for example time of day. If integer 1 is specified, it simply returns calendar grids on x without transformation. |
y |
A bare (or unquoted) variable or more mapping to y axis. More than
one variable need putting to |
date |
A |
calendar |
Type of calendar. (1) "monthly" calendar (the default) organises
the |
dir |
Direction of calendar: "h" for horizontal (the default) or "v" for vertical. |
week_start |
Day on which week starts following ISO conventions -
1 means Monday (default), 7 means Sunday. You can set |
nrow , ncol
|
Number of rows and columns defined for "monthly" calendar
layout. If |
polar |
FALSE (the default) for Cartesian or TRUE for polar coordinates. |
scale |
"fixed" (the default) for fixed scale. "free" for scaling conditional on each daily cell, "free_wday" for scaling on weekdays, "free_mday" for scaling on day of month. |
width , height
|
Numerics between 0 and 1 to specify the width/height for each glyph. |
margin |
Numerics of length two between 0 and 1 to specify the horizontal and vertical margins between month panels. |
... |
Extra arguments passed to |
plot |
A "ggplot" object or "plotly". |
label |
If "label" is specified, it will add month/week text on the
|
locale |
ISO 639 language code. The default is "en" (i.e. US English). For other languages support, package readr needs to be installed. See readr::locale for more details. |
abbr |
Logical to specify if the abbreviated version of label should be used. |
The calendar-based graphic can be considered as small multiples
of sub-series arranged into many daily cells. For every multiple (or
facet), it requires the x
variable mapped to be time of day and y
to
value. New x
and y
are computed and named with a .
prefixed to variable
according to x
and y
respectively, and get ready for ggplot2
aesthetic
mappings. In conjunction with group_by()
, it allows the grouped variable
to have their individual scales. For more details, see vignette("frame-calendar", package = "sugrrants")
A data frame or a dplyr::tibble with newly added columns of .x
, .y
. .x
and .y
together give new coordinates computed for different types of
calendars. date
groups the same dates in a chronological order, which is
useful for geom_line
or geom_path
. The basic use is ggplot(aes(x = .x, y = .y, group = date)) + geom_*
. The variable names .x
and .y
reflect
the actual x
and y
with a prefix .
.
facet_calendar for a fully-fledged faceting calendar with formal labels and axes.
library(dplyr, warn.conflicts = FALSE) # compute the calendar layout for the data frame calendar_df <- hourly_peds %>% filter(Sensor_ID == 13, Year == 2016) %>% frame_calendar(x = Time, y = Hourly_Counts, date = Date, nrow = 4) # ggplot p1 <- calendar_df %>% ggplot(aes(x = .Time, y = .Hourly_Counts, group = Date)) + geom_line() prettify(p1, size = 3, label.padding = unit(0.15, "lines")) # use in conjunction with group_by() grped_calendar <- hourly_peds %>% filter(Year == "2017", Month == "March") %>% group_by(Sensor_Name) %>% frame_calendar(x = Time, y = Hourly_Counts, date = Date, week_start = 7) p2 <- grped_calendar %>% ggplot(aes(x = .Time, y = .Hourly_Counts, group = Date)) + geom_line() + facet_wrap(~ Sensor_Name, nrow = 2) prettify(p2) ## Not run: # allow for different languages # below gives simplied Chinese labels with STKaiti font family, # assuming this font installed in user's local system prettify(p2, locale = "zh", family = "STKaiti") # plotly example if (!requireNamespace("plotly", quietly = TRUE)) { stop("Please install the 'plotly' package to run these following examples.") } library(plotly) pp <- calendar_df %>% group_by(Date) %>% plot_ly(x = ~ .Time, y = ~ .Hourly_Counts) %>% add_lines(text = ~ paste("Count: ", Hourly_Counts, "<br> Time: ", Time)) prettify(pp) ## End(Not run)
library(dplyr, warn.conflicts = FALSE) # compute the calendar layout for the data frame calendar_df <- hourly_peds %>% filter(Sensor_ID == 13, Year == 2016) %>% frame_calendar(x = Time, y = Hourly_Counts, date = Date, nrow = 4) # ggplot p1 <- calendar_df %>% ggplot(aes(x = .Time, y = .Hourly_Counts, group = Date)) + geom_line() prettify(p1, size = 3, label.padding = unit(0.15, "lines")) # use in conjunction with group_by() grped_calendar <- hourly_peds %>% filter(Year == "2017", Month == "March") %>% group_by(Sensor_Name) %>% frame_calendar(x = Time, y = Hourly_Counts, date = Date, week_start = 7) p2 <- grped_calendar %>% ggplot(aes(x = .Time, y = .Hourly_Counts, group = Date)) + geom_line() + facet_wrap(~ Sensor_Name, nrow = 2) prettify(p2) ## Not run: # allow for different languages # below gives simplied Chinese labels with STKaiti font family, # assuming this font installed in user's local system prettify(p2, locale = "zh", family = "STKaiti") # plotly example if (!requireNamespace("plotly", quietly = TRUE)) { stop("Please install the 'plotly' package to run these following examples.") } library(plotly) pp <- calendar_df %>% group_by(Date) %>% plot_ly(x = ~ .Time, y = ~ .Hourly_Counts) %>% add_lines(text = ~ paste("Count: ", Hourly_Counts, "<br> Time: ", Time)) prettify(pp) ## End(Not run)
Since the data input is data.frame
, it's better to sort the date-times
from early to recent and make implicit missing values explicit before using
geom_acf
.
geom_acf( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, lag.max = NULL, type = "correlation", level = 0.95, ... )
geom_acf( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, lag.max = NULL, type = "correlation", level = 0.95, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
Logical. If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
lag.max |
An integer indicating the maximum lag at which to calculate the acf. |
type |
A character string giving the type of the acf to be computed. The |
level |
A numeric defining the confidence level. If |
... |
Other arguments passed on to |
library(dplyr) fstaff <- hourly_peds %>% filter(Sensor_ID == 13) # use ggplot2 fstaff %>% ggplot(aes(x = ..lag.., y = Hourly_Counts)) + geom_acf()
library(dplyr) fstaff <- hourly_peds %>% filter(Sensor_ID == 13) # use ggplot2 fstaff %>% ggplot(aes(x = ..lag.., y = Hourly_Counts)) + geom_acf()
A dataset containing the pedestrian counts at hourly intervals from 2016-01-01 to 2017-04-20 at 7 sensors in the city of Melbourne. The variables are as follows:
hourly_peds
hourly_peds
A tibble with 78755 rows and 9 variables:
Date time when the pedestrian counts are recorded
Year associated with Date_Time
Month associated with Date_Time
Day of month associated with Date_Time
Weekday associated with Date_Time
Hour associated with Date_Time
Sensor identifiers
Sensor names
Hourly pedestrian counts
hourly_peds
hourly_peds
Since the data input is data.frame
, it's better to sort the date-times
from early to recent and make implicit missing values explicit before using
stat_acf
.
stat_acf( mapping = NULL, data = NULL, geom = "bar", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, lag.max = NULL, type = "correlation", level = 0.95, ... )
stat_acf( mapping = NULL, data = NULL, geom = "bar", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, lag.max = NULL, type = "correlation", level = 0.95, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data, either as a
|
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
Logical. If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
lag.max |
An integer indicating the maximum lag at which to calculate the acf. |
type |
A character string giving the type of the acf to be computed. The default is the "correlation" and other options are "covariance" and "partial". |
level |
A numeric defining the confidence level. If |
... |
Other arguments passed on to |
library(dplyr) fstaff <- hourly_peds %>% filter(Sensor_ID == 13) # use ggplot2 fstaff %>% ggplot(aes(x = ..lag.., y = Hourly_Counts)) + stat_acf(geom = "bar")
library(dplyr) fstaff <- hourly_peds %>% filter(Sensor_ID == 13) # use ggplot2 fstaff %>% ggplot(aes(x = ..lag.., y = Hourly_Counts)) + stat_acf(geom = "bar")