This page shows some examples code for plotly plots using the Instacart dataset.
# inserting data and selecting random sample, set seed for reproducibility
set.seed(1)
data(instacart)
instacart_dat =
instacart %>%
mutate(order_dow = ordered(order_dow, levels = c(0:6),
labels = c("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"))) %>%
sample_n(5000)
# looking at the "add to cart order" over the course of 24 hours for top 8 aisles
common_aisles_1 =
instacart_dat %>%
count(aisle, sort = TRUE) %>%
top_n(8) %>%
select(aisle)
## Selecting by n
hour_orders_top8 =
inner_join(instacart_dat, common_aisles_1, by = "aisle") %>%
plot_ly(y = ~add_to_cart_order, x = ~order_hour_of_day, color = ~aisle, type = "scatter", mode = "markers", text = ~product_name)
hour_orders_top8
# top 10 aisles and their corresponding average number of orders
common_aisles_2 =
instacart_dat %>%
count(aisle, sort = TRUE) %>%
top_n(20) %>%
select(aisle)
## Selecting by n
mean_orders_top20 =
inner_join(instacart_dat, common_aisles_2, by = "aisle") %>%
mutate(aisle = fct_reorder(aisle, order_id)) %>%
plot_ly(y = ~order_id, color = ~aisle, type = "box",
colors = "Set2")
mean_orders_tot =
instacart_dat %>%
mutate(aisle = fct_reorder(aisle, order_id)) %>%
plot_ly(y = ~order_id, color = ~aisle, type = "box",
colors = "Set2")
mean_orders_tot
# count 'day of the week' variable
day_of_week =
instacart_dat %>%
count(order_dow) %>%
mutate(order_dow = fct_reorder(order_dow, n)) %>%
plot_ly(x = ~order_dow, y = ~n, color = ~order_dow, type = "bar")
day_of_week