compute_stack: Stack overlapping data.

View source: R/compute_stack.R

compute_stackR Documentation

Stack overlapping data.

Description

Stack overlapping data.

Usage

compute_stack(x, stack_var = NULL, group_var = NULL)

Arguments

x

A data object

stack_var

A string specifying the stacking variable.

group_var

A string specifying the grouping variable.

Value

A data frame with columns:

stack_upr_

the lower y coordinate for a stack bar

stack_lwr_

the upper y coordinate for a stack bar

Examples

mtcars %>% cbind(count = 1) %>% compute_stack(~count, ~cyl)

# Shouldn't use or affect existing grouping
mtcars %>% cbind(count = 1) %>% group_by(am) %>% compute_stack(~count, ~cyl)

# If given a ggvis object, will use x variable for stacking by default
mtcars %>% ggvis(x = ~cyl, y = ~wt) %>%
  compute_stack(stack_var = ~wt, group_var = ~cyl) %>%
  layer_rects(x = ~cyl - 0.5, x2 = ~cyl + 0.5, y = ~stack_upr_,
    y2 = ~stack_lwr_)

# Collapse across hair & eye colour data across sex
hec <- as.data.frame(xtabs(Freq ~ Hair + Eye, HairEyeColor))
hec %>% compute_stack(~Freq, ~Hair)

# Without stacking - bars overlap
hec %>% ggvis(~Hair, ~Freq, fill = ~Eye, fillOpacity := 0.5) %>%
  layer_rects(y2 = 0, width = band())

# With stacking
hec %>% ggvis(x = ~Hair, y = ~Freq, fill = ~Eye, fillOpacity := 0.5) %>%
  compute_stack(~Freq, ~Hair) %>%
  layer_rects(y = ~stack_lwr_, y2 = ~stack_upr_, width = band())

# layer_bars stacks automatically:
hec %>% ggvis(~Hair, ~Freq, fill = ~Eye, fillOpacity := 0.5) %>%
  group_by(Eye) %>%
  layer_bars(width = 1)

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