bb_histogram: Helper for creating an histogram

View source: R/bb_helpers.R

bb_histogramR Documentation

Helper for creating an histogram

Description

Helper for creating an histogram

Usage

bb_histogram(
  bb,
  data,
  mapping = NULL,
  stacked = FALSE,
  fill = FALSE,
  bins = 30,
  binwidth = NULL,
  ...
)

Arguments

bb

A billboard htmlwidget object.

data

A data.frame or a vector, the first column will be used to calculate density if x is NULL.

mapping

Mapping of variables on the chart, see bbaes.

stacked

Logical, create a stacked histogram.

fill

Logical, create a stacked percentage histogram.

bins

Number of bins. Overridden by binwidth. Defaults to 30.

binwidth

The width of the bins. See geom_histogram

...

Not used.

Value

A billboard htmlwidget object.

See Also

bb_densityplot

Examples


data("diamonds", package = "ggplot2")

# one variable
billboarder() %>% 
  bb_histogram(data = diamonds, x = "price")

# with mapping
billboarder() %>% 
  bb_histogram(diamonds, bbaes(price))

# equivalent to
billboarder() %>% 
  bb_histogram(data = diamonds$price)

# prettier with 'binwidth'
# (but you need to know your data)
billboarder() %>% 
  bb_histogram(data = diamonds, x = "price", binwidth = 500) %>% 
  bb_colors_manual()

# with a grouping variable
billboarder() %>%
  bb_histogram(data = diamonds, x = "price",
               group = "cut", binwidth = 500)

# and with mapping
billboarder() %>%
  bb_histogram(diamonds, bbaes(price, group = cut),
               binwidth = 500)

# stacked histogram
billboarder() %>%
  bb_histogram(diamonds, bbaes(price, group = cut),
               stacked = TRUE, binwidth = 500)


# another example
dat <- data.frame(
  sample = c(rnorm(n = 500, mean = 1), rnorm(n = 500, mean = 2)),
  group = rep(c("A", "B"), each = 500)
)

billboarder() %>% 
  bb_histogram(data = dat, x = "sample", binwidth = 0.25)

samples_mean <- tapply(dat$sample, dat$group, mean)
billboarder() %>% 
  bb_histogram(data = dat, x = "sample", group = "group",
               binwidth = 0.25) %>% 
  bb_x_grid(
    lines = list(
      list(value = unname(samples_mean['A']),
           text = "mean of sample A"),
      list(value = unname(samples_mean['B']), 
           text = "mean of sample B")
    )
  )


billboarder documentation built on Sept. 29, 2023, 5:07 p.m.