Dave Childers
- Data Scientist with Powerley
- Before that: Data Science Consulting
- Before that: CSCAR at UM
October 15, 2015
Dave Childers
| state | potency | weight | month | price |
|---|---|---|---|---|
| WA | 77 | 217 | 1 | 5000 |
| CT | 51 | 248 | 1 | 4800 |
| FL | 68 | 43 | 1 | 3500 |
| OH | 69 | 123 | 1 | 3500 |
| MS | 75 | 118 | 1 | 3400 |
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document. ## Generating a static (non-dynamic, non-interactive) version of the plot.
# easy to read/write cocaine %>% mutate(log_weight = log(weight)) %>% filter(weight <= 10) # hard to read/write filter(mutate(cocaine, log_weight = log(weight)), weight <= 10)
ggplot(cocaine, aes(x = weight, y = price)) + geom_point()
ggvis(cocaine, x = ~weight, y = ~price) %>% layer_points
ggplot(cocaine, aes(x = weight, y = price)) + geom_point() ggvis(cocaine, x = ~weight, y = ~price) %>% layer_points
Similarities
ggplot(cocaine, aes(x = weight, y = price)) + geom_point() ggvis(cocaine, x = ~weight, y = ~price) %>% layer_points
Differences (ggplot -> ggvis)
| geom | layer |
|---|---|
| geom_bar | layer_rects |
| geom_histogram | layer_histograms |
| geom_density | layer_densities |
| geom_line | layer_lines |
| geom_smooth | layer_smooths |
| geom_text | layer_text |
| geom | layer |
|---|---|
| geom_abline | TBD |
| geom_errorbar | TBD |
| geom_jitter | TBD |
| geom_freqpoly | TBD |
# mapping state -> color ggplot(cocaine, aes(x = weight, y = price)) + geom_point(aes(color = state)) # setting color to a constant value ggplot(cocaine, aes(x = weight, y = price)) + geom_point(color = "orange")
| Variable | Constant | |
|---|---|---|
| Map | fill = ~state | fill = state |
| Set | fill := ~state | fill := state |
ggvis(cocaine, x = ~price) %>% layer_histograms(width = 100, center = 50)
ggvis(cocaine, x = ~price) %>% layer_densities %>%
add_axis("y", title_offset = 50)
ggvis(cocaine, x = ~state) %>% layer_bars
What happens if we say text = ~n ?
cocaine %>% count(state, sort = TRUE) %>%
ggvis(x = ~reorder(state, -n), y = ~n) %>%
filter(n >= 100) %>%
add_axis("x", title = "State") %>%
layer_bars %>%
layer_text(text := ~n, fontSize := 20)
# what happens if we say text = ~n ?
cocaine %>% count(state, sort = TRUE) %>%
ggvis(x = ~reorder(state, -n), y = ~n) %>%
filter(n >= 100) %>%
layer_bars %>%
layer_text(text := ~n) %>%
add_axis("x", title = "State") %>%
add_axis(
"x",
title = "2007 Cocaine Seizures by State",
orient = "top",
ticks = 0,
properties = axis_props(
axis = list(stroke = "white"),
labels = list(fontSize = 0)
)
)
ggtitle()coord_flip()themes%>% layer_smooths
%>% layer_model_predictions(model = "lm", formula = price ~ weight)
ggvis(cocaine, ~weight, ~price, fill = ~state, stroke = ~state) %>%
filter(state %in% c("FL", "IN", "NY")) %>%
mutate(log_weight = log(weight), log_price = log(price)) %>%
layer_points(~log_weight, ~log_price, opacity := 0.2) %>%
auto_group() %>%
layer_model_predictions(
model = "lm",
formula = log_price ~ log_weight
) %>%
add_axis("x", title = "Log Price") %>%
add_axis("y", title = "Log Weight")
methods(class = "ggvis")
## [1] arrange_ compute_align ## [3] compute_bin compute_boxplot ## [5] compute_count compute_density ## [7] compute_model_prediction compute_stack ## [9] compute_tabulate distinct_ ## [11] explain filter_ ## [13] group_by_ groups ## [15] knit_print mutate_ ## [17] print rename_ ## [19] select_ slice_ ## [21] summarise_ transmute_ ## [23] ungroup ## see '?methods' for accessing help and source code
| type | scale |
|---|---|
| time | scale_datetime |
| categorical | scale_nominal |
| numeric | scale_numeric |
| ordered | scale_ordinal |
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document. ## Generating a static (non-dynamic, non-interactive) version of the plot.
%>% layer_points(
size := input_slider(min = 50, max = 500, value = 50, step = 50)
)
| Shiny | ggvis |
|---|---|
| checkboxGroupInput | input_checkboxgroup |
| checkboxInput | input_checkbox |
| radioButtons | input_radiobuttons |
| numericInput | input_numeric |
| selectInput | input_select |
| sliderInput | input_slider |
| textInput | input_text |
| dateInput | [NA] |
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document. ## Generating a static (non-dynamic, non-interactive) version of the plot.
%>% add_tooltip(function(x) x$id)
cannot switch between data sets
cannot add/remove layers
need Shiny for full interactivity
| type | server.R | ui.R |
|---|---|---|
| plot | renderPlot | plotOutput |
| ggvis | bind_shiny | ggvisOutput |
More interactive than ggplot
Faster exploratory analysis than shiny
Be cautious about using in production