Learn R Programming

guideR (version 0.8.1)

plot_trajectories: Plot trajectories

Description

Create a trajectory index plot (similar to sequence index plot) from a data frame in long or period format.

Usage

plot_trajectories(
  data,
  id,
  time,
  fill,
  by = NULL,
  sort_by = NULL,
  nudge_x = NULL,
  hide_y_labels = NULL,
  facet_labeller = ggplot2::label_wrap_gen(width = 50, multi_line = TRUE),
  ...
)

plot_periods( data, id, start, stop, fill, by = NULL, sort_by = NULL, nudge_x = NULL, hide_y_labels = NULL, facet_labeller = ggplot2::label_wrap_gen(width = 50, multi_line = TRUE), ... )

Arguments

data

A data frame, or a data frame extension (e.g. a tibble).

id

<tidy-select> Column containing individual ids.

time

<tidy-select> Time variable.

fill

<tidy-select> Variable mapped to fill aesthetic.

by

<tidy-select> Optional variables to group by.

sort_by

<tidy-select> Optional variables to sort trajectories.

nudge_x

Optional amount of horizontal distance to move.

hide_y_labels

Hide y labels? If NULL, hide them when more than 20 trajectories are displayed.

facet_labeller

Labeller function for strip labels.

...

Additional arguments passed to ggplot2::geom_tile()

start, stop

<tidy-select> Start and stop variables of the periods.

Examples

Run this code
d <- dplyr::tibble(
  id = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3),
  time = c(0:3, 0:2, 0:4),
  status = c("a", "a", "b", "b", "b", "b", "a", "b", "b", "b", "b", "a"),
  group = c("f", "f", "f", "f", "f", "f", "f", "m", "m", "m", "m", "m")
)

d |> plot_trajectories(id = id, time = time, fill = status, colour = "black")
d |> plot_trajectories(id = id, time = time, fill = status, nudge_x = .5)
d |> plot_trajectories(id = id, time = time, fill = status, by = group)

d2 <- d |>
  dplyr::mutate(end = time + 1) |>
  long_to_periods(id = id, start = time, stop = end, by = status)
d2
d2 |> plot_periods(
  id = id,
  start = time,
  stop = end,
  fill = status,
  colour = "black",
  height = 0.8
)

Run the code above in your browser using DataLab