time_ggplot: Quick time-series ggplot

View source: R/time_ggplot.R

time_ggplotR Documentation

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,
  ...
)

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'.

Value

A ggplot.

See Also

ts_as_tbl

Examples

library(dplyr)
library(timeplyr)
library(ggplot2)
library(lubridate)

# 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)


timeplyr documentation built on April 3, 2025, 6:15 p.m.