Learn R Programming

timeplyr (version 1.1.0)

time_ggplot: Quick time-series ggplot

Description

time_ggplot() is a neat way to quickly plot aggregate time-series data.

Usage

time_ggplot(
  data,
  time,
  value,
  group = NULL,
  facet = FALSE,
  geom = ggplot2::geom_line,
  ...
)

Value

A ggplot.

Arguments

data

A data frame

time

Time variable using tidyselect.

value

Value variable using tidyselect.

group

(Optional) Group variable using tidyselect.

facet

When groups are supplied, should multi-series be plotted separately or on the same plot? Default is FALSE, or together.

geom

ggplot2 'geom' type. Default is geom_line().

...

Further arguments passed to the chosen 'geom'.

See Also

ts_as_tbl

Examples

Run this code
library(dplyr)
library(timeplyr)
library(ggplot2)
library(lubridate)
# \dontshow{
.n_dt_threads <- data.table::getDTthreads()
.n_collapse_threads <- collapse::get_collapse()$nthreads
data.table::setDTthreads(threads = 1L)
collapse::set_collapse(nthreads = 1L)
# }
# It's as easy as this
AirPassengers %>%
  ts_as_tbl() %>%
  time_ggplot(time, value)

# And this
EuStockMarkets %>%
  ts_as_tbl() %>%
  time_ggplot(time, value, group)

# Converting this to monthly averages

EuStockMarkets %>%
  ts_as_tbl() %>%
  mutate(month = year_month_decimal(time)) %>%
  summarise(avg = mean(value),
            .by = c(group, month)) %>%
  time_ggplot(month, avg, group)

# zoo example
x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 9, 14) - 1
x <- zoo::zoo(rnorm(5), x.Date)
x %>%
  ts_as_tbl() %>%
  time_ggplot(time, value)
# \dontshow{
data.table::setDTthreads(threads = .n_dt_threads)
collapse::set_collapse(nthreads = .n_collapse_threads)
# }

Run the code above in your browser using DataLab