Learn R Programming

ggetho (version 0.3.7)

time_scales: Scales for durations

Description

A set of scales used to represent experimental durations.

Usage

scale_x_days(
  name = "Time",
  breaks = waiver(),
  minor_breaks = waiver(),
  labels = waiver(),
  limits = NULL,
  expand = waiver(),
  oob = scales::censor,
  na.value = NA_real_,
  position = "bottom",
  time_wrap = NULL,
  unit = "day",
  log = FALSE
)

scale_y_days( name = "Time", breaks = waiver(), minor_breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = scales::censor, na.value = NA_real_, position = "left", time_wrap = NULL, unit = "day", log = FALSE )

scale_x_hours( name = "Time", breaks = waiver(), minor_breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = scales::censor, na.value = NA_real_, position = "bottom", time_wrap = NULL, unit = "h", log = FALSE )

scale_y_hours( name = "Time", breaks = waiver(), minor_breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = scales::censor, na.value = NA_real_, position = "left", time_wrap = NULL, unit = "h", log = FALSE )

scale_x_seconds( name = "Time", breaks = waiver(), minor_breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = scales::censor, na.value = NA_real_, position = "bottom", time_wrap = NULL, unit = "s", log = FALSE )

scale_y_seconds( name = "Time", breaks = waiver(), minor_breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = scales::censor, na.value = NA_real_, position = "left", time_wrap = NULL, unit = "s", log = FALSE )

Value

A ggplot scale.

Arguments

name

The name of the scale. Used as the axis or legend title. If waiver(), the default, the name of the scale is taken from the first mapping used for that aesthetic. If NULL, the legend title will be omitted.

breaks

One of:

  • NULL for no breaks

  • waiver() for the breaks specified by date_breaks

  • A Date/POSIXct vector giving positions of breaks

  • A function that takes the limits as input and returns breaks as output

minor_breaks

One of:

  • NULL for no breaks

  • waiver() for the breaks specified by date_minor_breaks

  • A Date/POSIXct vector giving positions of minor breaks

  • A function that takes the limits as input and returns minor breaks as output

labels

One of:

  • NULL for no labels

  • waiver() for the default labels computed by the transformation object

  • A character vector giving labels (must be same length as breaks)

  • An expression vector (must be the same length as breaks). See ?plotmath for details.

  • A function that takes the breaks as input and returns labels as output. Also accepts rlang lambda function notation.

limits

One of:

  • NULL to use the default scale range

  • A numeric vector of length two providing limits of the scale. Use NA to refer to the existing minimum or maximum

  • A function that accepts the existing (automatic) limits and returns new limits. Also accepts rlang lambda function notation. Note that setting limits on positional scales will remove data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see coord_cartesian()).

expand

Vector of range expansion constants used to add some padding around the data, to ensure that they are placed some distance away from the axes. Use the convenience function ggplot2::expand_scale() to generate the values for the expand argument. The defaults are to expand the scale by 5\ 0.6 units on each side for discrete variables.

oob

One of:

  • Function that handles limits outside of the scale limits (out of bounds). Also accepts rlang lambda function notation.

  • The default (scales::censor()) replaces out of bounds values with NA.

  • scales::squish() for squishing out of bounds values into range.

  • scales::squish_infinite() for squishing infinite values into range.

na.value

Missing values will be replaced with this value.

position

For position scales, The position of the axis. left or right for y axes, top or bottom for x axes.

time_wrap

duration (in seconds) used to wrap the labels of the time axis

unit

the name of unit (string) to be used in the label (e.g. one could use "second" instead of "s")

log

logical, whether axis should be on a log-transformed

Details

time_wrap is useful, for instance, to express time within a day (ZT), instead of absolute time.

References

See Also

  • ggetho to generate a plot object

  • ggplot2::scale_x_continuous, the defaut ggplot scale, to understand limits, breaks, labels and name

Examples

Run this code
# We generate some data
metadata <- data.frame(id = sprintf("toy_experiment | %02d", 1:20),
                   condition = c("A","B"))
dt <- toy_activity_data(metadata, 3)
# Then, a simple plot
pl <-  ggetho(dt, aes(y = asleep)) + stat_pop_etho()
pl + scale_x_hours(breaks = days(c(1, 2)))
pl + scale_x_hours()
pl + scale_x_days(breaks = days(c(1, 2)))
pl + scale_x_days()

# To express time modulus `time_wrap`
# e.g. time n the day
pl + scale_x_hours(time_wrap = hours(24)) +
     coord_cartesian(xlim=c(0, days(2)))

# On a shorter time scale
pl <-  ggetho(dt[t < hours(5)], aes(z = asleep)) + stat_tile_etho()
pl + scale_x_hours()
pl + scale_x_hours(breaks = hours(1:4))
pl + scale_x_seconds(breaks = hours(1:4))

Run the code above in your browser using DataLab