Last active
August 29, 2015 14:13
-
-
Save dholstius/334bc34b3de074515f25 to your computer and use it in GitHub Desktop.
Stacked area chart in ggivs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(ggivs) | |
chart_data <- structure(list(CalYr = c(1990L, 1990L, 1990L, 1990L, 1990L, 1990L, | |
1990L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2010L, | |
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2020L, 2020L, 2020L, | |
2020L, 2020L, 2020L, 2020L), Speed = c(0, 10, 20, 30, 40, 50, | |
60, 0, 10, 20, 30, 40, 50, 60, 0, 10, 20, 30, 40, 50, 60, 0, | |
10, 20, 30, 40, 50, 60), VMT = c(323694, 940682, 25396442, 43050277, | |
71101181, 16987641, 105467584, 222397, 859767, 57905435, 61835130, | |
52478438, 13402243, 133584545, 591510, 2003120, 30768516, 80895398, | |
80467412, 18261031, 129277323, 618255, 2114937, 32854406, 87450202, | |
87820416, 19972164, 140161697)), class = "data.frame", row.names = c(NA, | |
-28L), .Names = c("CalYr", "Speed", "VMT")) | |
# Stacked area chart | |
chart <- chart_data %>% | |
arrange(CalYr, Speed) %>% | |
mutate(to = cumsum(VMT), from = c(0, to[-n()])) %>% | |
ggvis(x = ~CalYr, fill = ~Speed) %>% | |
group_by(Speed) %>% | |
layer_ribbons(y = ~from, y2 = ~to, fill = ~Speed) %>% | |
add_axis("y", title = "VMT", format = "s") %>% | |
add_axis("x", title = "Year", format = "d") | |
# Works OK | |
chart %>% | |
add_legend("fill", title = "MPH") | |
# Per example in `help("add_legend", package = "ggvis")` | |
# (but does not work) | |
chart %>% | |
add_legend("fill", | |
title = "MPH", | |
properties = legend_props( | |
legend = list(x = scaled_value("x_rel", 0.1), | |
y = scaled_value("y_rel", 0.9)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment