We’re going to make some plotly plots.
library(tidyverse)
library(p8105.datasets)
library(plotly)
library(httr)
library(jsonlite)
get_all_inspections = function(url) {
all_inspections = vector("list", length = 0)
loop_index = 1
chunk_size = 50000
DO_NEXT = TRUE
while (DO_NEXT) {
message("Getting data, page ", loop_index)
all_inspections[[loop_index]] =
GET(url,
query = list(`$order` = "zipcode",
`$limit` = chunk_size,
`$offset` = as.integer((loop_index - 1) * chunk_size)
)
) %>%
content("text") %>%
fromJSON() %>%
as_tibble()
DO_NEXT = dim(all_inspections[[loop_index]])[1] == chunk_size
loop_index = loop_index + 1
}
all_inspections
}
url = "https://data.cityofnewyork.us/resource/43nn-pn8j.json"
nyc_inspections =
get_all_inspections(url) %>%
bind_rows()
## Getting data, page 1
## Getting data, page 2
## Getting data, page 3
## Getting data, page 4
## Getting data, page 5
nyc = nyc_inspections %>%
select(score,boro,camis, grade, latitude, longitude,street, cuisine_description) %>%
filter(score %in% 20:100) %>%
drop_na(grade)
#Scatter plot: Score of resteraurants in Manhattan
nyc %>%
filter(boro == "Manhattan") %>%
plot_ly(x= ~latitude, y=~longitude, color = ~score, type = "scatter", mode ="markers", alpha = .5, text = ~score)
## Warning: Ignoring 14 observations
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
#box plot: Different locations restaurants score
nyc %>%
mutate(street = fct_reorder(boro, score)) %>%
plot_ly(x=~boro, y= ~score, color = ~street, type= "box", colors= "viridis")
## Warning in mean.default(sort(x, partial = half + 0L:1L)[half + 0L:1L]): argument
## is not numeric or logical: returning NA
nyc %>%
count(cuisine_description) %>%
mutate(cuisine_description = fct_reorder(cuisine_description, n)) %>%
plot_ly(x=~cuisine_description, y=~n, type="bar")
Let’s get some data
data("nyc_airbnb")
nyc_airbnb =
nyc_airbnb %>%
mutate(rating = review_scores_location / 2) %>%
select(borough = neighbourhood_group, neighbourhood, price, room_type, lat, long, rating) %>%
filter(borough == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500) %>%
drop_na(rating)
Let’s make a scatterplot!!
nyc_airbnb %>%
mutate(text_label = str_c("Price:", price)) %>%
plot_ly(x= ~lat, y=~long, color = ~price, type = "scatter", mode ="markers", alpha = .5, text = ~text_label)
can we make boxplots?? Sure can!
nyc_airbnb %>%
mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>%
plot_ly(y= ~price, color = ~neighbourhood, type= "box", colors= "viridis")
Can we make a bar plot?
nyc_airbnb %>%
count(neighbourhood) %>%
mutate(neighbourhood = fct_reorder(neighbourhood, n)) %>%
plot_ly(x=~neighbourhood, y=~n, type="bar")
##ggplotly..
ggp_scatterplot=
nyc_airbnb %>%
ggplot(aes(x=lat,y =long, color =price))+geom_point()
ggplotly(ggp_scatterplot)