today we’re making interactive plots in plotly. we’ll make examples using the NYC airbnb dataset.
library(tidyverse)
library(p8105.datasets)
library(plotly)
Do some initial data cleaning / subsetting.
data("nyc_airbnb")
nyc_airbnb =
nyc_airbnb |>
mutate(rating = review_scores_location / 2) |>
select(
rating, neighbourhood_group, neighbourhood,
room_type, lat, long, price) |>
drop_na(rating) |>
filter(
neighbourhood_group == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500
)
Use plotly to make some quick plots.
First a scatterplot.
nyc_airbnb |>
mutate(
text_label =
str_c("Price: ", price, "\nNeighborhood: ", neighbourhood)) |>
plot_ly(
x = ~lat, y = ~long, color = ~price, text = ~text_label,
type = "scatter", mode = "markers", alpha = 0.5
)
Next up – box plot.
nyc_airbnb |>
mutate(neighbourhood = fct_reorder(neighbourhood, price)) |>
plot_ly(
x = ~neighbourhood, y = ~price, color = ~neighbourhood,
type = "box", colors = "viridis")
Let’s do a bar chart with number of rentals.
nyc_airbnb |>
count(neighbourhood) |>
mutate(neighbourhood = fct_reorder(neighbourhood, n)) |>
plot_ly(
x = ~neighbourhood, y = ~n,
type = "bar")
plot_ly(
z = volcano, type = "heatmap"
)
Here’s a chloropleth
plot_ly(
type = "choropleth",
locations = c( "AZ", "CA", "VT" ) ,
locationmode = "USA-states" ,
colorscale = "Viridis" ,
z = c( 10, 20, 40 )) %>%
layout ( geo = list ( scope = "usa" ))