stackedbars is an htmlwidget JavaScript/D3 chart library based heavily on the streamgraph package.
devtools::install_github("zachcp/stackedbars")
The stackedbars
pacakge is an htmlwidget
1 that is based on the D3.js
2 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
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")
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