Intermediate Interactive Data Visualization with plotly in R
Adam Loy
Statistician, Carleton College
world_indicators %>%
plot_ly(x = ~income, y = ~co2) %>%
add_markers(frame = ~year, ids = ~country, showlegend = FALSE)
library(dplyr)
library(purrr)
belgium %>%
split(.$year) %>%
accumulate(~bind_rows(.x, .y)) %>%
set_names(1960:2018) %>%
bind_rows(.id = "frame")
glimpse(monthly_logs)
Rows: 171
Columns: 4
$ package <chr> "ggvis", "ggvis", "ggvis", "ggvis", "ggvis", "ggvis",…
$ date <date> 2014-06-30, 2014-07-31, 2014-08-31, 2014-09-30, 2014…
$ dec_date <dbl> 2014.49, 2014.58, 2014.66, 2014.75, 2014.83, 2014.91,…
$ downloads <dbl> 1344, 2120, 2035, 1702, 3590, 2899, 2427, 2227, 2708,…
monthly_logs %>%
split(f = .$dec_date) %>%
accumulate(., ~bind_rows(.x, .y)) %>%
bind_rows(.id = "frame") %>%
plot_ly(x = ~dec_date, y = ~downloads) %>%
add_lines(color = ~package, frame = ~frame, ids = ~package)
Warning message:
In p$x$data[firstFrame] <- p$x$frames[[1]]$data :
number of items to replace is not a multiple of replacement length
library(tidyr) complete_logs <- monthly_logs %>% complete(package, dec_date, fill = list(downloads = 0))
arrange(complete_logs, dec_date)
## A tibble: 228 x 4
package dec_date date downloads
<chr> <dbl> <date> <dbl>
1 ggvis 2014. 2014-06-30 1344
2 highcharter 2014. NA 0
3 plotly 2014. NA 0
4 rbokeh 2014. NA 0
5 ggvis 2015. 2014-07-31 2120
# … with 223 more rows
complete_logs %>%
split(f = .$dec_date) %>%
accumulate(., ~bind_rows(.x, .y)) %>%
bind_rows(.id = "frame") %>%
plot_ly(x = ~dec_date, y = ~downloads) %>%
add_lines(color = ~package, frame = ~frame, ids = ~package)
Intermediate Interactive Data Visualization with plotly in R