Plotly

We’re making interactive plots

library(tidyverse)
library(plotly)

library(p8105.datasets)

Focus on NYC Airbnb data.

data("nyc_airbnb")

nyc_airbnb = 
  nyc_airbnb |> 
  mutate(stars = review_scores_location / 2) |> 
  select(borough = neighbourhood_group, 
         neighbourhood, stars, price, room_type, lat, long) |> 
  drop_na(stars) |> 
  filter(
    borough == "Manhattan",
    room_type == "Entire home/apt",
    price %in% 100:500
  )

Let’s make a scatterplot!

nyc_airbnb |> 
  mutate(text_label = str_c("Price: $", price, "\nRating: ", stars)) |> 
  plot_ly(
    x = ~lat, y = ~long, color = ~price, text = ~text_label,
    type = "scatter", mode = "markers", alpha = 0.5)

Let’s make a box plot!

nyc_airbnb |> 
  mutate(neighbourhood = fct_reorder(neighbourhood, price)) |> 
  plot_ly(y = ~price, color = ~neighbourhood, type = "box", colors = "viridis")

Let’s make a bar plot!

nyc_airbnb |> 
  count(neighbourhood) |> 
  mutate(neighbourhood = fct_reorder(neighbourhood, n)) |> 
  plot_ly(
    x = ~neighbourhood, y = ~n, color = ~neighbourhood,
    type = "bar", colors = "viridis")