Learn R Programming

ggvis (version 0.3.0.1)

compute_bin: Bin data along a continuous variable

Description

Bin data along a continuous variable

Usage

compute_bin(x, x_var, w_var = NULL, binwidth = NULL, origin = NULL,
  right = TRUE, 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.
binwidth
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.
origin
The initial position of the left-most bin. If NULL, the the default, will use the smallest value in the dataset.
right
Should bins be right-open, left-closed, or right-closed, left-open.
pad
If TRUE, adds empty bins at either end of x. This ensures frequency polygons touch 0, and adds padidng 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

Run this code
mtcars %>% compute_bin(~mpg)
mtcars %>% compute_bin(~mpg, binwidth = 10)
mtcars %>% group_by(cyl) %>% compute_bin(~mpg, binwidth = 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()

Run the code above in your browser using DataLab