Loading data

We are gonna look at NYC Airbnb data.

library(tidyverse)
library(p8105.datasets)
library(plotly)
data(nyc_airbnb)

nyc_airbnb = 
  nyc_airbnb %>% 
  mutate(rating = review_scores_location / 2) %>%
  select(
    neighbourhood_group, neighbourhood, rating, price, room_type, lat, long) %>%
  filter(
    !is.na(rating), 
    neighbourhood_group == "Manhattan",
    room_type == "Entire home/apt",
    price %in% 100:500) # in the range of

Plotly plots

scatterplot

nyc_airbnb %>%
  mutate(text_label = str_c("Price: $", price, "\nRating: ", rating)) %>% #\n is new line
  plot_ly(
    x = ~lat, y = ~long, type = "scatter", mode = "markers",
    color = ~price, text = ~text_label, alpha = 0.5)

boxplot

nyc_airbnb %>% 
  mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>% # re-order according to price
  plot_ly(y = ~price, color = ~neighbourhood, type = "box", colors = "viridis")

bar plot

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

ggplotly

scatter_ggplot = 
  nyc_airbnb %>%
  ggplot(aes(x = lat, y = long, color = price)) +
  geom_point(alpha = 0.25) +
  coord_cartesian()

ggplotly(scatter_ggplot)