This page presents the code behind the plots provided in Dashbord page.

This is an example of how we can use Plotly and Flexdashboards to share our data analysis results interactively. I have worked with rest_inspec dataset which contains resaurant inspection reports of some reastaurants in New York. This dataset contains information such as date, zipcode, food categories, boroughs, etc.

rest_inspec is a dataset from p8105.datasets, and you can find more information about this dataset here.

Data Cleaning

data("rest_inspec")

inspec_data =
rest_inspec %>% 
drop_na(score) %>%
separate(inspection_date, into = c("year", "month-day"), sep = 4) %>% 
  select(
    boro, critical_flag, cuisine_description, year, score, zipcode) %>%
  mutate(
    cuisine_description = case_when(
      cuisine_description == "Latin (Cuban, Dominican, Puerto Rican, South & Central American)" ~ "Latin",
      cuisine_description == "Sandwiches/Salads/Mixed Buffet" ~ "Mixed Buffet",
      TRUE ~ as.character(cuisine_description))
  )

In this part, I did some data tidying to my dataset. To do so, I have changed date to year (which I care about), selected some of the variables to work with (since the dataset is big and heavy), and renamed some of cuisine descriptions which were too long to be used in plots.

Plot #1 (Bar-plot)

# score of different cuisines in Washington Heights
inspec_barplot = inspec_data %>%
  filter(boro == "MANHATTAN" & zipcode == 10032) %>% 
  group_by(cuisine_description) %>%
  summarize(
    score_mean = mean(score)
  ) %>% 
  plot_ly(
    x = ~fct_reorder(cuisine_description, score_mean), y = ~score_mean, type = "bar",
    color = ~cuisine_description, colors = "Set2", alpha = .7
  ) %>% 
    layout(
    xaxis = list(
      title = "Cuisine Category",
      autotick = FALSE, tick0 = 1, dtick = 1),
    yaxis = list(
      title = "Mean Score"
    ))

This bar plot shows the mean score of different cuisine types in Washington Heights in an increasing order.

Plot #2 (Line-plot)

# number of critical flags in each boro through years
inspec_lineplot = inspec_data %>% 
  filter(critical_flag == "Critical" & boro != "Missing") %>% 
  group_by(boro, year) %>% 
  summarize(n = n()) %>% 
  plot_ly(
    y = ~n, x = ~year, type = "scatter", mode = "lines",
    color = ~boro, colors = "Set2"
  ) %>% 
  layout(
    xaxis = list(
      title = "Year",
      autotick = FALSE, tick0 = 1, dtick = 1),
    yaxis = list(
      title = "Number of Critical Flags"
    ))

This line plot shows the number of critical flags in each borough over years.

Plot #3 (Box-plot)

# distribution of score for American food in different boroughs
inspec_boxplot = inspec_data %>%
  filter(year == 2017 & cuisine_description == "American" & boro != "Missing") %>% 
  group_by (boro, cuisine_description) %>% 
  plot_ly(
    color= ~boro, y= ~score, type = "box", colors = "Set2", alpha = .7
  ) %>%
  layout(
    xaxis = list(
      showticklabels = FALSE,
      title = "Borough",
      autotick = FALSE, tick0 = 1, dtick = 1),
    yaxis = list(
      title = "Score"
    ))

This boxplot shows the distribution of Score for American food in different boroughs.