Use Plotly to make plots!

Three plots are made on this web page. The dataset used is the Restaurant Inspection dataset.

Load data

library(tidyverse)
library(p8105.datasets)
library(plotly)
data("rest_inspec")

rest_inspec = 
  rest_inspec %>% 
  select(grade, boro, cuisine_description, dba, zipcode, score, violation_description, grade) %>% 
  mutate(
    cuisine_description = replace(cuisine_description, cuisine_description == 'Latin (Cuban, Dominican, Puerto Rican, South & Central American)', 'Latin'),
    cuisine_description = replace(cuisine_description, cuisine_description == 'Café/Coffee/Tea', 'Cafe'),
    cuisine_description = replace(cuisine_description, cuisine_description == 'Delicatessen', 'Deli')
    ) %>% 
  filter(
    boro == "QUEENS",
    zipcode == "11101",
    grade == c("A", "B", "C")
  )

Scatterplot

rest_inspec %>% 
  mutate(cuisine_description = fct_reorder(cuisine_description, score)) %>% 
  mutate(text_label = str_c("Restaurant Name: ", dba, "\nGrade: ", grade, "\nViolation: ", violation_description)) %>% 
  plot_ly(
    x = ~cuisine_description, y = ~score, text = ~text_label, color = ~cuisine_description, colors = "viridis",
    alpha = 0.5, type = "scatter", mode = "markers"
  ) %>% 
  layout(
    title = "Scatterplot of scores of different cuisine types in Queens(zipcode 11101)",
    xaxis = list(title = "Cuisine Type"),
    yaxis = list(title = "Score"))

Barplot

rest_inspec %>% 
  count(cuisine_description) %>% 
  mutate(cuisine_description = fct_reorder(cuisine_description, n)) %>% 
  plot_ly(
    x = ~cuisine_description, y = ~n, 
    color = ~cuisine_description, colors = "viridis",
    type = "bar"
  ) %>% 
  layout(
    title = "Barplot of different cuisine types in Queens(zipcode 11101)",
    xaxis = list(title = "Cuisine Type"),
    yaxis = list(title = "Count")
  )

Boxplot

rest_inspec %>% 
  mutate(cuisine_description = fct_reorder(cuisine_description, score)) %>% 
  plot_ly(
    y = ~score, x = ~cuisine_description, color = ~cuisine_description,
    colors = "viridis", type = "box"
  ) %>% 
  layout(
    title = "Boxplot of scores of different cuisine types in Queens(zipcode 11101)",
    xaxis = list(title = "Cuisine Type"),
    yaxis = list(title = "Count")
  )