Skip to contents

Create an Histogram

Usage

v_hist(
  vc,
  mapping = NULL,
  data = NULL,
  name = NULL,
  stack = FALSE,
  bins = 30,
  binwidth = NULL,
  ...,
  serie_id = NULL,
  data_id = NULL
)

Arguments

vc

A chart initialized with vchart().

mapping

Default list of aesthetic mappings to use for chart.

data

Default dataset to use for chart. If not already a data.frame, it will be coerced to with as.data.frame.

name

Name for the serie, only used for single serie (no color/fill aesthetic supplied).

stack

Whether to stack the data or not (if fill aesthetic is provided).

bins

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

binwidth

The width of the bins. Can be specified as a numeric value or as a function that calculates width from unscaled x. Here, "unscaled x" refers to the original x values in the data, before application of any scale transformation. When specifying a function along with a grouping structure, the function will be called once per group. The default is to use the number of bins in bins, covering 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.

The bin width of a date variable is the number of days in each time; the bin width of a time variable is the number of seconds.

...

Additional properties for histogram bars.

data_id, serie_id

ID for the data/serie, can be used to further customize the chart with v_specs().

Value

A vchart() htmlwidget object.

Examples


library(vchartr)
library(palmerpenguins)

# Create an histogram using a numeric variable
vchart(penguins) %>%
  v_hist(aes(flipper_length_mm))
#> Warning: Removed 2 rows containing non-finite outside the scale range (`stat_bin()`).
# Customize some style properties vchart(penguins) %>% v_hist( aes(flipper_length_mm), bar = list( style = list( stroke = "white", line_width = 1, fill = "forestgreen" ) ) ) #> Warning: Removed 2 rows containing non-finite outside the scale range (`stat_bin()`).
# Use fill aesthetic to differentiate series vchart(penguins) %>% v_hist(aes(flipper_length_mm, fill = species)) #> Warning: Removed 2 rows containing non-finite outside the scale range (`stat_bin()`).
# Stack results vchart(penguins) %>% v_hist(aes(flipper_length_mm, fill = species), stack = TRUE) #> Warning: Removed 2 rows containing non-finite outside the scale range (`stat_bin()`).
# Use custom colors vchart(penguins) %>% v_hist( aes(flipper_length_mm, fill = species), bar = list( style = list(opacity = 0.5) ) ) %>% v_scale_color_manual(c( Adelie = "#ffa232", Chinstrap = "#33a2a2", Gentoo = "#b34df2" )) #> Warning: Removed 2 rows containing non-finite outside the scale range (`stat_bin()`).