pi_notation: pi notation

View source: R/pi_notation.R

pi_notationR Documentation

\pi notation

Description

This function re-expresses a vector of numbers in terms of \pi, returning an expression object that can be added to a plot.

Usage

pi_notation(x, as_fraction = TRUE, as_bar = TRUE, ...)

Arguments

x

A numeric vector.

as_fraction

A logical value that defines whether or not to express sequential multipliers of \pi as fractions (TRUE) or decimals (FALSE).

as_bar

If as_fraction = TRUE, as_bar is a logical value that defines whether or not to style fractions with a horizontal bar (i.e., \frac{a}{b}) or a slash (i.e., a/b).

...

Additional arguments passed to add_lagging_point_zero if as_fraction = FALSE.

Details

The function is designed to be used on a regular sequence of numbers (e.g., representing the tick marks on a plot) within pretty_plot.

Value

The function returns an expression object.

Author(s)

Edward Lavender

See Also

pretty_axis (and subsidiary plotting functions, such as pretty_plot) can implement this function internally.

Examples

#### Example (1): Implement the function in isolation
x <- seq(0, 8, by = 0.25*pi)/pi
pi_notation(x)
pi_notation(x, as_bar = FALSE)
pi_notation(x, as_fraction = FALSE)

#### Example (2): Plotting with fractions
## Stacked fraction
xlabels <- pi_notation(x, as_fraction = TRUE)
plot(x, rep(0, length(x)), axes = FALSE)
axis(side = 1, at = x*pi, labels = xlabels, col = "red")
## Spread fraction
x_axis <- pi_notation(x, as_fraction = TRUE, as_bar = FALSE)
plot(x, rep(0, length(x)), axes = FALSE)
axis(side = 1, at = x*pi, labels = xlabels, col = "red")

#### Example (3): Plotting with decimals
x_axis <- pi_notation(1:3, as_fraction = FALSE)
plot(x, rep(0, length(x)), axes = FALSE)
axis(side = 1, at = x*pi, labels = xlabels, col = "red")

#### Example (4): pi_notation() is implemented automatically in prettyGraphics
# ... via pretty_axis(), pretty_plot() etc.

## Define example time series of lunar phase in radians
dat <- data.frame(date = seq.Date(as.Date("2016-01-01"), as.Date("2016-02-01"), by = 1))
dat$lunar <- lunar::lunar.phase(dat$date)

## Visualise lunar phase time series, with pi_notation
pretty_plot(dat$date, dat$lunar,
            pretty_axis_args = list(pi_notation = list(NULL, list()))
            )
add_moons(side = 2, pos = min(dat$date), radius = 0.5)

## Customise pi_notation
# Use as_bar = FALSE
pretty_plot(dat$date, dat$lunar,
            pretty_axis_args = list(pi_notation = list(NULL, list(as_bar = FALSE)))
            )
add_moons(side = 2, pos = min(dat$date), radius = 0.5)
# Use decimals
pretty_plot(dat$date, dat$lunar,
            pretty_axis_args = list(pi_notation = list(NULL, list(as_fraction = FALSE)))
            )
add_moons(side = 2, pos = min(dat$date), radius = 0.5)

## Use pi_notation within specified axis limits
pretty_plot(dat$date, dat$lunar,
            ylim = c(0, 2*pi),
            pretty_axis_args = list(pi_notation = list(NULL, list()))
            )
add_moons(side = 2, pos = min(dat$date), radius = 0.5)


edwardlavender/prettyGraphics documentation built on Jan. 19, 2025, 2:47 p.m.