stackedbars is an htmlwidget JavaScript/D3 chart library based heavily on the streamgraph package.

Installation

devtools::install_github("zachcp/stackedbars")

Usage

The stackedbars pacakge is an htmlwidget1 that is based on the D3.js2 JavaScript library. Examples are based of Mike Bostocks stacked barchart 3 and normalized stacked barchart 4. This library owes its existence, organizational philosophy and most other details to the streamgraph 5 package

Making a stacked barchart

library(dplyr)
library(tidyr)
library(stackedbar)

read.csv(system.file("extdata", "statesdata.csv", package = "stackedbar"),
         stringsAsFactors = FALSE) %>%
  tidyr::gather(ages,n,-State) -> dat

stackedbar(dat, "ages", "n", "State", legend=TRUE) %>%
        sg_fill_brewer("PuOr")

Currently there are only two functions, stackedbar and stackedbarnormal which do what they advertise…

stackedbarnormal(dat, "ages", "n", "State", right=150,) %>%
        sg_fill_brewer("PuOr")

Data Expectations

The data to use for a stackedbar should be in “long format”6. For example:

head(dat)
#>   State          ages       n
#> 1    AL Under_5_Years  310504
#> 2    AK Under_5_Years   52083
#> 3    AZ Under_5_Years  515910
#> 4    AR Under_5_Years  202070
#> 5    CA Under_5_Years 2704659
#> 6    CO Under_5_Years  358280

stackedbar R package by zach charlop-powers
streamgraph R package by Bob Rudis
htmlwidgets R package by Ramnath Vaidyanathan, Kenton Russell
D3 JavaScript library by Mike Bostock.


  1. http://www.htmlwidgets.org/

  2. http://d3js.org/

  3. http://bl.ocks.org/mbostock/3886208

  4. http://bl.ocks.org/mbostock/3886394

  5. http://github.com/hrbrmstr/streamgraph

  6. http://blog.rstudio.org/2014/07/22/introducing-tidyr/