-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode
373 lines (312 loc) · 13.8 KB
/
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
library(rvest)
library(tidyverse)
library(data.table)
library(tidytext)
library(stringr)
library(ggplot2)
library(ggrepel)
library(shiny)
library(data.table)
library(leaflet)
library(plotly)
library(corrplot)
library(lubridate)
library(readr)
library(reshape2)
library(ggmap)
library(leaflet)
ggmap::register_google(key = "AIzaSyCWJ8DwoFBs1kYzM82SSA-XAvBDXT8YJRA")
bay <- ggmap(get_googlemap(center = c(longitude = -122, latitude = 37.9),
zoom = 9, scale = 1,
maptype = "roadmap",
color = "color"))
# leaflet() %>%
# addTiles() %>%
# setView(lng = -122,lat = 38, zoom = 7.5) %>%
# addMarkers(lat = boba.data$lat,
# lng = boba.data$long, #west is negative, east is positive
# # label = name,
# popup = boba.data$name) #textbox for more description (long text that would take long to upload)
boba.data <- read_csv("boba_bay_area.csv")
city.data <- boba.data %>%
group_by(city) %>%
summarize(mean = mean(rating),
mean.lat = mean(lat),
mean.lng = mean(long))
colnames(city.data) <- c("City", "Average Rating", "Latitude", "Longitude")
city.rating <- bay +
geom_point(data = city.data,
aes(col = class, x = Longitude, y = Latitude)) +
guides(alpha = FALSE) +
ggtitle("Average Yelp Rating by City") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank()) +
labs(color= "Yelp ratings")
# city.rating.plotly <- ggplotly(city.rating, tooltip = c("City", "Average Rating"))
# boba.map <- bay +
# geom_point(data = boba.data,
# aes(col = factor(rating), x = long, y = lat), size = 2) +
# guides(alpha = FALSE) +
# ggtitle("Boba Bae in Your Area") +
# theme(plot.title = element_text(color = "black", size = 14, face = "bold.italic"),
# axis.title.x = element_blank(),
# axis.title.y = element_blank()) +
# labs(color= "Yelp ratings")
## RANKING BY CITY
city.data$`Average Rating` <- (city.data$`Average Rating`) - 2.5
city.data <- city.data %>%
arrange(-city.data$`Average Rating`)
city.data <- city.data %>%
mutate(class = ifelse(city.data$`Average Rating`>1.7,
"Top 10%",
ifelse(city.data$`Average Rating`>1.456,
"Top 20%",
"Rest")))
city.data$class <- factor(city.data$class, levels = c("Top 10%", "Top 20%", "Rest"))
# city.data %>%
# ggplot(aes(x = reorder(city.data$City, city.data$`Average Rating`), y = city.data$`Average Rating`)) +
# geom_bar(aes(fill = class), stat = "identity") +
# xlab("City") +
# ylab("Average Rating") +
# scale_y_continuous(labels = c("2.5", "3", "3.5", "4", "4.5")) +
# coord_flip()+
# ggtitle("Average Rating by City (including ties)")
## RANKING BY NAME
store.data <- boba.data %>%
group_by(name) %>%
summarize(mean = mean(rating)) %>%
arrange(mean)
store.data <- store.data %>%
mutate(class = ifelse(store.data$mean == 2,
"Lowest",
ifelse(store.data$mean == 2.5,
"Second to the Lowest",
ifelse(store.data$mean == 4.5,
"Second to the Highest",
"Highest"))))
store.data.low <- store.data %>%
filter(mean <= 2.5)
store.data.high <- store.data %>%
filter(mean >= 4.5)
store.data.low.high <- rbind(store.data.low, store.data.high)
store.joined <- merge(store.data.low.high, boba.data[,c(3,5,6,7,8)], by = "name")
# city.data %>%
# ggplot(aes(x = reorder(city.data$City, city.data$`Average Rating`), y = city.data$`Average Rating`)) +
# geom_bar(aes(fill = class), stat = "identity") +
# xlab("City") +
# ylab("Average Rating") +
# scale_y_continuous(labels = c("2.5", "3", "3.5", "4", "4.5")) +
# coord_flip() +
# ggtitle("Average Rating by City (including ties)")
#
# bay +
# geom_point(data = city.data,
# aes(col = factor(class), x = city.data$Longitude, y = city.data$Latitude), size = 2) +
# guides(alpha = FALSE) +
# ggtitle("Ranking by City") +
# theme(plot.title = element_text(color = "black", size = 14, face = "bold.italic"),
# axis.title.x = element_blank(),
# axis.title.y = element_blank()) +
# labs(color= "Class")
ui <- fluidPage(
titlePanel("Where's My Boba Bae?"),
sidebarLayout(
sidebarPanel(
tags$head(
tags$style("description {white-space: nowrap;}")),
fluidRow(textOutput(outputId = "intro"))
),
mainPanel(
tabsetPanel(
tabPanel("The snapshot",
verbatimTextOutput(outputId = "snapshot"),
plotlyOutput(outputId = "whole.map")),
tabPanel("Ranking by Cities",
plotOutput(outputId = "city"),
plotlyOutput(outputId = "store.map"),
radioButtons(inputId = "class",
label = "Choose your preferred city ranking level",
choices = c("Top 10%", "Top 20%", "Rest"),
selected = city.data$class[1]),
leafletOutput(outputId = "city.map"),
tableOutput("levelData"),
br()),
tabPanel("Ranking by Stores",
textOutput(outputId = "store.title"),
plotOutput(outputId = "store.low"),
plotOutput(outputId = "store.high"),
plotlyOutput(outputId = "store.ranking"),
radioButtons(inputId = "store",
label = "Choose your preferred store ranking level",
choices = c("Lowest", "Second to the Lowest", "Second to the Highest", "Highest"),
selected = store.joined$class[1]),
plotlyOutput(outputId = "store.select")),
tabPanel("Boba Bae Options",
selectInput(inputId = "select",
label = "Find your potential boba bae in your preferred city!",
choices = boba.data$city,
selected = boba.data$city[1]),
leafletOutput(outputId = "boba.map"),
textOutput(outputId = "text"),
br())
)
)
)
)
server <- function(input, output, session) {
output$intro <- renderText(
paste("You 1) have never tried boba before; 2) have just recognized the contagious presence of boba, but don't know which one to try (because there are just so many of them!); or 3) are a boba enthusiast and are willing to try more but need help finding them efficiently. Worry no more. This interactive app helps you find your Boba Bae in a pool of 603 boba stores located in the Bay Area.")
)
output$whole.map <- renderPlotly({
bay.whole.map <- bay +
geom_point(data = boba.data,
aes(col = factor(rating), x = long, y = lat), size = 1) +
guides(alpha = FALSE) +
ggtitle("Boba Bae in Your Area") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold.italic"),
axis.title.x = element_blank(),
axis.title.y = element_blank()) +
labs(color= "Yelp ratings")
mytext = paste("Store =", boba.data$name, "\n", "Yelp Rating =", boba.data$rating)
pp = plotly_build(bay.whole.map)
style(pp, text = mytext, hoverinfo= "text")
})
output$snapshot <- renderText({
paste("This maps shows all the 603 boba stores, ranked by color. Though the greater San Jose area is the most populated region (and note how the high-ranked ones are usually located there), there are other options scattered around in the Bay Area.")
})
output$boba.map <- renderLeaflet({
input.city <- boba.data %>%
mutate(is.selected.city =
ifelse(boba.data$city == input$select,
"city",
"not.city")) %>%
filter(is.selected.city == "city")
input.city %>%
leaflet() %>%
# setView(lng = boba.data$long[1],lat = boba.data$lat[1],zoom = 8) %>%
addTiles() %>%
addCircleMarkers(lat = ~lat,
lng = ~long,
clusterOptions = markerClusterOptions(),
popup = paste(input.city$name, "<br>",
"rating:", input.city$rating, "<br>")) #zipcode also needs to be a string
})
output$city.rating <- renderPlot({
city.data %>%
ggplot(aes(x = "City", y = "Average Rating")) %>%
geom_bar(stat = "identity")
})
output$city <- renderPlot({
city.data %>%
ggplot(aes(x = reorder(city.data$City, city.data$`Average Rating`), y = city.data$`Average Rating`)) +
geom_bar(aes(fill = class), stat = "identity") +
xlab("City") +
ylab("Average Rating") +
scale_y_continuous(labels = c("2.5", "3", "3.5", "4", "4.5")) +
coord_flip() +
ggtitle("Average Rating by City (including ties)")
})
output$store.title <- renderText({
paste("Average Ranking by Stores")
})
output$store.low <- renderPlot({
store.data.low %>%
ggplot(aes(x = reorder(store.data.low$name, store.data.low$mean), y = store.data.low$mean)) +
geom_bar(aes(fill = as.factor(class)), stat = "identity") +
xlab("Store") +
ylab("Average Rating") +
coord_flip()+
ggtitle("All-time low") +
scale_fill_discrete("Class")
})
output$store.high <- renderPlot({
store.data.high %>%
ggplot(aes(x = reorder(store.data.high$name, store.data.high$mean), y = store.data.high$mean)) +
geom_bar(aes(fill = as.factor(class)), stat = "identity") +
xlab("Store") +
ylab("Average Rating") +
coord_flip()+
ggtitle("All-time high") +
scale_fill_discrete("Class")
})
output$store.ranking <- renderPlotly({
store.level <- bay +
geom_point(data = store.joined,
aes(col = factor(class), x = store.joined$long, y = store.joined$lat), size = 2) +
guides(alpha = FALSE) +
ggtitle("Overview of Stores by Rankings") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold.italic"),
axis.title.x = element_blank(),
axis.title.y = element_blank()) +
labs(color = "class")
store.text = paste("Store =", store.joined$name, "\n", "Average Rating =", store.joined$mean)
store.plotly = plotly_build(store.level)
style(store.plotly, text = store.text, hoverinfo= "text")
})
output$store.select <- renderPlotly({
store.data <- store.joined %>%
filter(class == input$store)
store.level <- bay +
geom_point(data = store.data,
aes(col = factor(class), x = store.data$long, y = store.data$lat), size = 2) +
guides(alpha = FALSE) +
ggtitle("Stores by Preferred Rankings") +
theme(plot.title = element_text(color = "black", size = 10, face = "italic"),
axis.title.x = element_blank(),
axis.title.y = element_blank()) +
theme(legend.position = "none")
store.text = paste("Store =", store.data$name, "\n", "Average Rating =", store.data$mean)
store.plotly = plotly_build(store.level)
style(store.plotly, text = store.text, hoverinfo= "text")
})
output$city.map <- renderLeaflet({
input.class <- city.data %>%
mutate(is.selected.class =
ifelse(city.data$class == input$class,
"class",
"not.class")) %>%
filter(is.selected.class == "class")
input.class %>%
leaflet() %>%
addTiles() %>%
# setView(lng = -122,lat = 37.9, zoom = 8) %>%
addCircleMarkers(lat = ~Latitude,
lng = ~Longitude,
clusterOptions = markerClusterOptions(),
popup = paste(input.class$class, "<br>",
"City: ", input.class$City, "<br>")) #zipcode also needs to be a string
})
output$store.map <- renderPlotly({
city.data.adjusted <- city.data %>%
mutate(adjusted.rating = city.data$`Average Rating` + 2.5)
city.data.adjusted$adjusted.rating <- round(city.data.adjusted$adjusted.rating, digit = 2)
snapshot.level <- bay +
geom_point(data = city.data.adjusted,
aes(col = factor(class), x = city.data.adjusted$Longitude, y = city.data.adjusted$Latitude), size = 2) +
guides(alpha = FALSE) +
ggtitle("Snapshot of city rankings") +
theme(plot.title = element_text(color = "black", size = 10, face = "italic"),
axis.title.x = element_blank(),
axis.title.y = element_blank()) +
labs(color= "Class")
snapshot.text = paste("City =", city.data.adjusted$City, "\n", "Average Rating =", city.data.adjusted$adjusted.rating)
snapshot.plotly = plotly_build(snapshot.level)
style(snapshot.plotly, text = snapshot.text, hoverinfo= "text")
})
output$text <- renderText({
city.ranking <- city.data %>%
filter(City == input$select) %>% #take data set and give only one row with state name = whatever the user clicks
select(`Average Rating`)
city.ranking <- city.ranking + 2.5
city.ranking <- round(city.ranking, digits = 2)
paste("You have just clicked on", input$select,
", and that city's average rating of boba stores is", city.ranking, ". See 'Ranking by Cities' for detailed information about respective cities.")
})
output$levelData <- renderTable({
city.level.filter <- city.data %>%
mutate(level.filter <- city.data$`Average Rating` + 2.5)
colnames(city.level.filter) <- c("City", "Adj. Average Rating", "Lat", "Long", "Class", "Average Rating")
level.filter <- subset(city.level.filter[,c(1,6)], city.data$class == input$class)
})
}
shinyApp(ui = ui, server = server)