compute_bin: Bin data along a continuous variable

Description Usage Arguments Value See Also Examples

View source: R/compute_bin.R

Description

Bin data along a continuous variable

Usage

1
2
compute_bin(x, x_var, w_var = NULL, width = NULL, center = NULL,
  boundary = NULL, closed = c("right", "left"), pad = TRUE)

Arguments

x

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

x_var,w_var

Names of x and weight variables. The x variable must be continuous.

width

The width of the bins. The default is NULL, which yields 30 bins that cover the range of the data. You should always override this value, exploring multiple widths to find the best to illustrate the stories in your data.

center

The center of one of the bins. Note that if center is above or below the range of the data, things will be shifted by an appropriate number of widths. To center on integers, for example, use width = 1 and center = 0, even if 0 is outside the range of the data. At most one of center and boundary may be specified.

boundary

A boundary between two bins. As with center, things are shifted when boundary is outside the range of the data. For example, to center on integers, use width = 1 and boundary = 0.5, even if 1 is outside the range of the data. At most one of center and boundary may be specified.

closed

One of "right" or "left" indicating whether right or left edges of bins are included in the bin.

pad

If TRUE, adds empty bins at either end of x. This ensures frequency polygons touch 0, and adds padding between the data and axis for histograms.

Value

A data frame with columns:

count_

the number of points

x_

mid-point of bin

xmin_

left boundary of bin

xmax_

right boundary of bin

width_

width of bin

See Also

compute_count For counting cases at specific locations of a continuous variable. This is useful when the variable is continuous but the data is granular.

Examples

1
2
3
4
5
6
7
mtcars %>% compute_bin(~mpg)
mtcars %>% compute_bin(~mpg, width = 10)
mtcars %>% group_by(cyl) %>% compute_bin(~mpg, width = 10)

# It doesn't matter whether you transform inside or outside of a vis
mtcars %>% compute_bin(~mpg) %>% ggvis(~x_, ~count_) %>% layer_paths()
mtcars %>% ggvis(~ x_, ~ count_) %>% compute_bin(~mpg) %>% layer_paths()

rpruim/ggvis2 documentation built on May 28, 2019, 2:34 a.m.