knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(p8105.datasets)
library(plotly)
I filtered the instacart data to only focous on snack department
data("instacart")
ins_df = instacart %>%
filter(department == "snacks")
ins_df %>%
group_by(product_name) %>%
mutate(reordered_sum = sum(reordered),
n_obs = n()) %>%
mutate(text_label = str_c("Product Name: ", product_name, "\n Number of Reorder Purchases: ", n_obs)) %>%
plot_ly(
x = ~n_obs, y = ~reordered_sum, color = ~aisle, text = ~text_label,
alpha = .5, type = "scatter", mode = "markers", colors = "viridis", showlegend = FALSE) %>%
layout(
xaxis = list(title = "Number of Purchases"),
yaxis = list(title = "Number of Reorder Purchases"),
title = "Number of Purchases vs. Number of Reorder Purchases"
) %>%
layout(legend = list(x = 0.05, y = 0.9))