Interactivity is great.

Here’s the tidyverse.

library(tidyverse)
library(plotly)

library(p8105.datasets)

Let’s load the AirBnB dataset.

data("nyc_airbnb")

nyc_airbnb = 
  nyc_airbnb |> 
  mutate(
    rating = review_scores_location / 2
  ) |> 
  select(
    neighbourhood_group, neighbourhood, rating,
    price, room_type, lat, long
  ) |> 
  filter(
    neighbourhood_group == "Manhattan",
    room_type == "Entire home/apt",
    price <= 500,
    price >= 100
  )

Let’s make a scatterplot!!!! But interactive this time.

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

Let’s make a boxplot.

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

Maybe barcharts next?

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

ggplotly

don’t use this often.

ggp_scatterplot = 
  nyc_airbnb |> 
  ggplot(aes(x = lat, y = long, color = price)) + 
  geom_point()

ggplotly(ggp_scatterplot)