time_ggplot {timeplyr}R 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_tibble

Examples

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

# It's as easy as this
AirPassengers %>%
  ts_as_tibble() %>%
  time_ggplot(time, value)

# And this
EuStockMarkets %>%
  ts_as_tibble() %>%
  time_ggplot(time, value, 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_tibble() %>%
  time_ggplot(time, value)

# An example using raw data

ebola <- outbreaks::ebola_sim$linelist

# We can build a helper to count and complete
# Using the same time grid

count_and_complete <- function(.data, time, .name,
                               from = NULL, ...,
                               time_by = NULL){
  .data %>%
    time_by(!!dplyr::enquo(time), time_by = time_by,
               .name = .name, from = !!dplyr::enquo(from)) %>%
    dplyr::count(...) %>%
    dplyr::ungroup() %>%
    time_complete(.data[[.name]], ..., time_by = time_by,
                  fill = list(n = 0))
}
ebola %>%
  count_and_complete(date_of_onset, outcome, time_by = "week", .name = "date_of_onset",
                     from = floor_date(min(date_of_onset), "week")) %>%
  time_ggplot(date_of_onset, n, geom = geom_blank) +
  geom_col(aes(fill = outcome))


[Package timeplyr version 0.8.1 Index]