-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2019_40_AllThePizza.Rmd
178 lines (146 loc) · 8.58 KB
/
2019_40_AllThePizza.Rmd
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
---
title: "2019_40_AllThePizza"
author: "Jongseung John Lim"
date: "10/1/2019"
output:
html_document:
theme: paper
highlight: kate
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE)
#install.packages("tidyverse")
devtools::install_github("dkahle/ggmap")
devtools::install_github("dr-harper/ggmapstyles")
#devtools::install_github("clauswilke/ggtext")
```
```{r prep, message=FALSE}
library(tidyverse)
library(ggtext)
library(ggmap)
library(ggmapstyles)
library(gridExtra)
library(reshape2)
library(here)
source(here::here('R/utils/secrets.R'))
source(here::here('R/utils/utils.R'))
source(here::here("R/utils/custom_themes.R"))
register_google(key = googleMapsAPIKey)
theme_set(theme_custom(base_family="dosis"))
```
```{r data}
pizza_jared <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-10-01/pizza_jared.csv")
pizza_barstool <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-10-01/pizza_barstool.csv")
pizza_datafiniti <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-10-01/pizza_datafiniti.csv")
```
```{r define map}
mymap <- get_snazzymap(center = 'Flatiron Building', mapRef = "https://snazzymaps.com/style/8097/wy", zoom = 12)
```
```{r pressure, echo=FALSE}
theme_update(rect = element_rect(fill = "grey10"),
plot.title = element_markdown(size = 35,
lineheight = 1.2,
hjust = 0.5),
plot.subtitle = element_markdown(size = 22,
color = "grey50",
hjust = 0,
lineheight = 1.4),
plot.caption = element_markdown(family = "dosis",
face = "bold",
size = 18,
color = "grey50"),
axis.text.x = element_text(size = 14,
family = "Roboto Mono",
color = "grey40"),
axis.text.y = element_text(size = 14,
family = "Roboto Mono",
color = "grey40"),
axis.ticks.x = element_line(color = "grey30"),
axis.ticks.y = element_blank(),
strip.background = element_blank())
pizza_barstool_newyork <- subset(pizza_barstool[order(pizza_barstool$review_stats_all_average_score, decreasing = TRUE),], substring(zip, 1, 1) == 1)
pizza_barstool_newyork_trustworthy <- subset(pizza_barstool_newyork, review_stats_all_count >= 20)
daveplot <- ggplot(data=head(pizza_barstool_newyork_trustworthy, 10), aes(x=reorder(paste(name, sep = ""), review_stats_all_average_score), y=review_stats_all_average_score, fill=city)) +
geom_bar(stat="identity") +
geom_text(aes(label=sprintf("%.3f", round(review_stats_all_average_score,3))), position=position_dodge(width=0.9), vjust=0.4, hjust=1.2) +
geom_text(aes(label=paste(review_stats_all_count, "votes"), color="white"), hjust=-0.1) + guides(colour=FALSE) +
coord_flip(ylim = c(8, 10))+
labs(x=NULL, y="Average rating", title = "<img src='img/pizza-slice-solid.png' width='50'/> Top 10 Pizza places in New York",
subtitle = "<br>According to Dave Portnoy & One Bite app users") +
theme(axis.text.y = element_text(size = 10,
color = "white"))
common <- intersect(pizza_barstool_newyork$name, pizza_jared$place)
pizza_barstool_intersect <- subset(pizza_barstool_newyork, name %in% common)
pizza_jared_intersect <- subset(pizza_jared, place %in% common)
pizza_jared_intersect <- pizza_jared_intersect[order(pizza_jared_intersect$total_votes, decreasing = TRUE),]
pizza_barstool_intersect <- pizza_barstool_intersect[order(pizza_barstool_intersect$review_stats_all_count, decreasing = TRUE),]
pizza_barstool_intersect <- pizza_barstool_intersect[!duplicated(pizza_barstool_intersect$name),]
pizza_barstool_intersect$jared_score = 0
pizza_jared_intersect_fixed <- pizza_jared_intersect[0,]
for( i in rownames(pizza_jared_intersect) ) {
placename <- pizza_jared_intersect[[i, "place"]]
recordedTime <- pizza_jared_intersect[[i, "time"]]
#pizza_jared_intersect <- subset(pizza_jared_intersect, (place == placename & time == recordedTime) | (place != placename) )
if (nrow(pizza_jared_intersect_fixed[pizza_jared_intersect_fixed$place == placename, ]) == 0) {
pizza_jared_intersect_fixed <- rbind(pizza_jared_intersect_fixed, pizza_jared_intersect[i,])
} else if (nrow(pizza_jared_intersect_fixed[pizza_jared_intersect_fixed$place == placename & pizza_jared_intersect_fixed$time == recordedTime, ]) > 0 ) {
pizza_jared_intersect_fixed <- rbind(pizza_jared_intersect_fixed, pizza_jared_intersect[i,])
}
}
for( i in rownames(pizza_jared_intersect_fixed) ) {
placename <- pizza_jared_intersect_fixed[[i, "place"]]
temp <- pizza_jared_intersect_fixed[[i,"answer"]]
if (temp == "Excellent") {
score = 10
} else if (temp == "Good") {
score = 7.5
} else if (temp == "Average") {
score = 5
} else if (temp == "Poor") {
score = 2.5
} else {
score = 0
}
scoreToAdd <- score * pizza_jared_intersect_fixed[[i,"percent"]]
pizza_barstool_intersect[pizza_barstool_intersect$name == placename,]$jared_score %+=% scoreToAdd
}
melted <- melt(pizza_barstool_intersect, id.vars = "name", measure.vars = c("review_stats_all_average_score", "jared_score"))
biasedplot <- ggplot(data=melted, aes(x=reorder(name, value), y=value, fill=variable)) + geom_bar (stat="identity", position = position_dodge(width = 0.5)) + labs(x = NULL, y = "Average Rating",
subtitle = "<br>Is One Bite biased?") + theme(axis.text.x = element_text(angle = 70, hjust = 1, size = 10, color="white")) + coord_cartesian(ylim = c(2, 10)) + scale_fill_discrete("Sources", labels=c("One Bite users", "Jared Lander's Pizza polls"))
mapplot <- ggmap(mymap) +
geom_point(aes(x = longitude, y = latitude, color = review_stats_all_average_score, alpha = review_stats_all_average_score), data = pizza_barstool_newyork, size = 2) +
scale_colour_gradient2(low="red", mid = "orange", high="blue", midpoint = 5, space = "rgb", aesthetics = "colour", na.value = "grey50", guide = "colourbar", limits = c(0,10), breaks=c(2, 4, 6, 8, 10), labels=format(c( 2, 4, 6, 8, 10))) +
scale_alpha(range = c(0.2, 0.7)) + guides(alpha=FALSE) +
theme(rect = element_rect(fill = "grey10"),
plot.title = element_markdown(size = 35,
lineheight = 1.2),
plot.subtitle = element_markdown(size = 22,
color = "grey50",
hjust = 0,
lineheight = 1.4),
plot.caption = element_markdown(family = "dosis",
size = 18,
face="bold",
hjust = 3,
color = "grey50"),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank()) +
labs(x = NULL, y = NULL, color="Average rating (out of 10)",
subtitle = "<br><span>\"Alright Frankie, it's pizza reviewing time. <b>One bite</b> - everyone knows the rules.\"</span><br>", caption="\n\nVisualization by John Lim | Data: Jared Lander and Barstool Sports")
g <- arrangeGrob(daveplot, biasedplot, mapplot, layout_matrix = rbind(c(1, 3),c(2,3)))
plot(g, bg = "#333333")
g1 <- arrangeGrob(daveplot, biasedplot, layout_matrix = rbind(c(1),c(2)))
g2 <- mapplot
```
```{r saveplot}
ggsave(g, file = "plots/2019_40/2019_40_AllThePizza.png", type = "cairo", bg = "#1a1a1a", dpi = 96) # adjust dpi accordingly
ggsave(g, file = "plots/2019_40/2019_40_AllThePizza.pdf", device =cairo_pdf, bg = "#1a1a1a", dpi = 96) # adjust dpi accordingly
ggsave(g1, file = "plots/2019_40/2019_40_AllThePizza_Charts.png", width = 10, height=10.5, type = "cairo", bg = "#1a1a1a", dpi = 96)
ggsave(g1, file = "plots/2019_40/2019_40_AllThePizza_Charts.pdf", width = 10, height=10.5, device=cairo_pdf, bg = "#1a1a1a", dpi = 96)
ggsave(g2, file = "plots/2019_40/2019_40_AllThePizza_Map.png", width = 10, height=9, type = "cairo", bg = "#1a1a1a", dpi = 96)
ggsave(g2, file = "plots/2019_40/2019_40_AllThePizza_Map.pdf", width = 10, height=9,device=cairo_pdf, bg = "#1a1a1a", dpi = 96)
```