compute_align: Align positions using length.

View source: R/compute_align.R

compute_alignR Documentation

Align positions using length.

Description

This compute function is often used in conjunction with compute_count, when used on data with a continuous x variable. By default, the computed width will be equal to the resolution of the data, or, in other words the smallest difference between two values in the data.

Usage

compute_align(x, var, length = NULL, align = 0.5, dir = "x")

Arguments

x

Dataset-like object to align. Built-in methods for data frames, grouped data frames and ggvis visualisations.

var

Name of variable to compute width of.

length

An absolute length to use. If NULL (the default), the width will be equivalent to the resolution of the data.

align

Where does the existing variable fall on the new bins? 0 = left edge, 0.5 = center, 1 = right edge.

dir

Direction, i.e. "x" or "y". Used to generate variable names in output.

Details

An absolute width for each x can be specified by using the width argument. If width is NULL (the default), it will use the resolution of the data as the width.

Value

The original data frame, with additional columns:

'dir'min_

left boundary of bin

'dir'max_

right boundary of bin

'dir'len_

width of bin

See Also

compute_bin For counting cases within ranges of a continuous variable.

compute_count For counting cases at specific values of a variable.

Examples

mtcars %>% compute_count(~disp) %>% compute_align(~x_)
mtcars %>% compute_count(~mpg) %>% compute_align(~x_)

# Use a specific width
pressure %>% compute_count(~temperature) %>% compute_align(~x_)
pressure %>% compute_count(~temperature) %>% compute_align(~x_, length = 5)

# It doesn't matter whether you transform inside or outside of a vis
mtcars %>% compute_count(~cyl, ~wt) %>%
  compute_align(~x_, length = .5) %>%
  ggvis(x = ~xmin_, x2 = ~xmax_, y = ~count_, y2 = 0) %>%
  layer_rects()

mtcars %>%
  ggvis(x = ~xmin_, x2 = ~xmax_, y = ~count_, y2 = 0) %>%
  compute_count(~cyl, ~wt) %>%
  compute_align(~x_) %>%
  layer_rects()

# Varying align
mtcars %>%
  ggvis(x = ~xmin_, x2 = ~xmax_, y = ~count_, y2 = 0) %>%
  compute_count(~cyl, ~wt) %>%
  compute_align(~x_, length = 0.5, align = input_slider(0, 1)) %>%
  layer_rects()

ggvis documentation built on May 29, 2024, 1:12 a.m.