-
Notifications
You must be signed in to change notification settings - Fork 1
/
BeerCaseStudy.Rmd
542 lines (393 loc) · 21.3 KB
/
BeerCaseStudy.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
---
title: "Beer: A 'Case' Study"
author: "Dustin Bracy and Tazeb Abera"
date: "10/26/2019"
output: html_document
editor_options:
chunk_output_type: console
---
## A Case Study of US Craft Beer and Breweries
### Introduction and Overview
This case study explores datasets for various `beers` and `breweries` where they are produced in the US. The steps and procedures taken for this analysis are detailed below.
The initial data provided were in two separate datasets, one each for beers and the other breweries. The dataset combined into a single dataset using Full join.
### Repository Structure
For Repository files, please see our [`Github`](https://github.com/tazeb6531/Project_one/blob/master/BeerCaseStudy.Rproj}
* [`ReadMe.md`](https://github.com/tazeb6531/Project_one/blob/master/Readme.md): This is the base page for the repository, containing an introduction as well as our codebook for how we coded variable names throughout the datasets.
* [`BeerCaseStudy.Rmd`](https://github.com/tazeb6531/Project_one/blob/master/BeerCaseStudy.Rmd) : This is the primary file for the analysis, and is the file we are currently reading. It contains all the relevant R code, graphics, and analysis for the project; also included are specific .docx, .html, and .pdf knitted versions of this file.
* [`Beers.csv`](https://github.com/tazeb6531/Project_one/blob/master/Beers.csv) and [`Breweries.csv`](https://github.com/tazeb6531/Project_one/blob/master/Breweries.csv), were provided and are the original datasets from which our merged dataset is produced.
### Reproduction of Analysis
`README.md` is the suggested place to start. It is also the possible method to downlaod this repository.
We begin by setting `knitr` options,
```{r setup, include=FALSE, echo=T}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
options(knitr.kable.NA = '', knitr.kable.booktabs=T)
library(tidyverse) # auto includes ggplot2, readr, deplyr,tidyr etc
library(ggthemes) # for themes in ggplot
library(kableExtra) # library to make the document more presentable
library(mice) # optional to impute ABV & IBU values
library(class)
library(caret)
library(usmap)
library(highcharter)
library(magrittr)
library(ggmap)
library(ggExtra)
library(treemapify)
library(maps)
library(naniar)
library(revealjs)
library(htmltools)
library(viridisLite)
library(treemap)
library(RColorBrewer)
```
```{r}
# Read in beer and breweries data set using _csv for more tidy output
Beers <- read_csv('./data/Beers.csv', col_types = cols())
Breweries <- read_csv('./data/Breweries.csv', col_types = cols())
```
## Research Questions
### 1. How many breweries are present in each state?
We can answer this question by counting the number of values of each `State` from the `Breweries` dataset.
```{r}
# Format for output:
BPS <- data.frame(table(Breweries$State))
colnames(BPS) <- c('State','Breweries')
BrewPerState <- cbind(BPS[1:11,], BPS[12:22,], BPS[23:33,], BPS[34:44,], BPS[45:55,])
# Stylized output:
kable(BrewPerState, caption = 'Breweries per State') %>% kable_styling()
# Set up the map:
Breweries <- rename(Breweries, state = State)
map <- Breweries %>% group_by(state) %>% summarise(Breweries = n())
# Plot Brewery heatmap:
plot_usmap(data=map, values = "Breweries", labels=F) +
labs(title = "Breweries per State") +
scale_fill_continuous(name = "Breweries")
```
### 2a. Merge beer data with the breweries data.
The field `Brewery_id` in `Beers.csv` and `Brew_ID` in `Breweries.csv` share the same data, but do not share a name. We remedy this by renaming the column in `Beers.csv`.
After renaming, we can merge them into a single dataset, using `full_join`.
```{r}
# Rename Brewery_id to Brew_ID to satisfy merging requirement
Beers <- rename(Beers, Brew_ID = Brewery_id)
# Merge tables
Brewdata <- full_join(Beers, Breweries, by="Brew_ID")
# Change variable names to more meaningful title
Brewdata <- rename(Brewdata, Beer = Name.x, Brewery = Name.y, OZ = Ounces)
#Convert OZ to factor
Brewdata$OZ = as.factor(Brewdata$OZ)
```
### 2b. Check the first and last 6 observations to verify the merged file.
To retrieve the first and last six observations from the combined data,
we run `head` and `tail` on `Brewdata`, our combined dataset.
```{r}
kable(Brewdata %>% head()) %>% kable_styling()
kable(Brewdata %>% tail()) %>% kable_styling()
```
### 3. Address the missing values in each column.
To start, we first use a function (which returns `true` if a given value is NA, `false` otherwise, using `is.na`) and `sapply` to determine the number of missing values for each column within `Brewdata`. This gives us a raw data view of the missing data. We then plot the missing data to visualize the quantity.
```{r}
# Explore missing values with kable library to make document more presentable:
MissingValues <- sapply(Brewdata, function(x)sum(is.na(x)))
MissingValues %>% kable("html") %>% kable_styling()
# Missing values code borrowed from: https://jenslaufer.com/data/analysis/visualize_missing_values_with_ggplot.html
missing.values <- Brewdata %>% gather(key = "key", value = "val") %>%
mutate(isna = is.na(val)) %>% group_by(key) %>%
mutate(total = n()) %>% group_by(key, total, isna) %>%
summarise(num.isna = n()) %>% mutate(pct = num.isna / total * 100)
levels <- (missing.values %>% filter(isna == T) %>% arrange(desc(pct)))$key
percentage.plot <- missing.values %>% ggplot() + geom_bar(aes(x = reorder(key, desc(pct)), y = pct, fill=isna),
stat = 'identity', alpha=0.8) + scale_x_discrete(limits = levels) +
scale_fill_manual(name = "", values = c('steelblue', 'tomato3'), labels = c("Present", "Missing")) +
coord_flip() + labs(title = "Percentage of missing values", x = 'Columns with missing data', y = "Percentage of missing values")
percentage.plot
```
To tackle the low hanging fruit, we will remove 5 observations with missing styles, and mark the missing IBU/ABV values to compare later.
> 3/5 of these also have missing ABV and IBU values also.
> The ABV/IBU of the non-missings values aren't remarkable, so their removal shouldn't affect the overall data, but it is worth noting.
```{r}
# remove the 5 beers missing style (3 of them were missing ABV/IBU also)
Brewdata %>% filter(is.na(Style))
RemovedBrews <- Brewdata %>% filter(is.na(Style))
Brewdata <- Brewdata %>% filter(!is.na(Style))
# identify missing IBU/ABV values:
Brewdata$ImputedABV <- ifelse(is.na(Brewdata$ABV),'Imputed','Original')
Brewdata$ImputedIBU <- ifelse(is.na(Brewdata$IBU),'Imputed','Original')
```
Now that we've marked the missing values, we can explore imputation of 62 missing ABV and 1005 missing IBU values into BrewComplete (so we can compare vs Brewdata).
```{r, include =F}
# impute missing ABV & IBU values:
imp <- mice(Brewdata, seed=5)
# remove Beer_ID as a predictor:
imp$pred [,'Beer_ID'] = 0
BrewComplete <- mice::complete(imp)
sum(is.na(BrewComplete))
summary(BrewComplete)
```
```{r}
# view random sample of imputed vs original plot ABV:
set.seed(10)
StyleSample <- sample(unique(BrewComplete$Style), size=15)
BrewComplete %>% filter(Style %in% StyleSample) %>% ggplot(aes(x=ABV, y=reorder(Style,ABV), color=ImputedABV)) + geom_point() + labs(y = 'Style')
# view random sample of imputed vs original plot IBU:
BrewComplete %>% filter(Style %in% StyleSample) %>% ggplot(aes(x=IBU, y=reorder(Style,IBU), color=ImputedIBU)) + geom_point() + labs(y = 'Style')
# assign imputed ABV values
Brewdata$ABV <- BrewComplete$ABV
# store IBU containing dataset for further examination
IBUdata <- Brewdata %>% filter(!is.na(IBU))
```
Based on visual inspection, it appears ABV is safe to impute, but IBU looks like it might be overreaching! (e.g. Cider Beer IBUs have no reference data for comparison)
We will exclude beer data with missing IBU for comparisons using IBU in future tests.
### 4. Compare the median alcohol content and international bitterness unit for each state.
Arkansas and Utah are tied for lowest ABV at 4.0%
Maine has highest ABV at 6.7%
Wisconsin has the lowest IBU @ 19
Maine has the highest IBU @ 61
```{r}
# 4.a transform the data
Firewater <- Brewdata %>% na.omit() %>% group_by(state) %>% summarise(Median = median(ABV)) %>% arrange(Median)
Bitter <- Brewdata %>% na.omit() %>% group_by(state) %>% summarise(Median = median(IBU)) %>% arrange(Median)
# 4.b Plot a bar chart to compare ABV by state
ggplot(data=Firewater, aes(x=state, y=Median)) +
geom_bar(stat="identity", fill="steelblue")+
theme_economist() +
scale_color_economist()+
theme(axis.text.x=element_text(size=rel(0.8), angle=90)) +
ggtitle("Median ABV by State") +
labs(x="state",y="ABV")
# 4.c Plot a bar chart to compare IBU by state
ggplot(data=Bitter, aes(x=state, y=Median)) +
geom_bar(stat="identity", fill="steelblue")+
theme_economist() +
scale_color_economist()+
theme(axis.text.x=element_text(size=rel(0.8), angle=90))+
ggtitle("Median IBU by State") +
labs(x="State",y="IBU")
# Combined plot merging 4b and 4c:
Brewdata %>% filter(!is.na(IBU & ABV)) %>% select(state, ABV,IBU) %>%
group_by(state) %>%
dplyr::summarize(Median_IBU=median(IBU), Median_ABV=median(ABV*100)) %>%
gather(`Median_IBU`, `Median_ABV`, key='Type', value='Measurement') %>%
ggplot(aes(state, Measurement, fill=Type)) +
geom_bar(stat='identity', position = 'Dodge') +
labs(y='ABV% and IBU', title = 'Median ABV and IBU by State') +
theme_economist() +
scale_fill_manual(name = "", values = c('tomato3','steelblue'), labels = c("ABV %", "IBU")) +
scale_color_economist() +
theme(axis.text.x=element_text(size=rel(0.8), angle=90))
```
### 5. Which state has the maximum alcoholic (ABV) beer? Which state has the most bitter (IBU) beer?
We identify Colorado as having the beer with the highest ABV, at `.128`; and we identify Oregon has having the beer with the highest IBU, at `138`.
```{r}
TopABV <- top_n(Brewdata, 1, ABV)
TopIBU <- top_n(Brewdata, 1, IBU)
kable(TopABV) %>% kable_styling()
kable(TopIBU) %>% kable_styling()
# Set up the map:
df <- data.frame(abbr=c('CO','OR'),Top_Beer=c('ABV','IBU'))
map <- left_join(statepop,df, by ='abbr')
# Plot the map:
plot_usmap(data = map, values = "Top_Beer") + labs(x='',y='',title = "Top States by ABV/IBU") +
scale_discrete_manual(labels(remove)) + scale_fill_manual(name = "Category", values=c(ABV='tomato3',IBU='steelblue'), na.value='darkgrey') + theme(legend.background = element_rect(fill = "#D5E4EB"), plot.background = element_rect(fill = "#D5E4EB"))
```
### 6. Comments on the summary statistics and distribution of the ABV variable.
We get summary statistics by calling `summary` and `sd` on the `ABV` column in our `Brewdata` dataset.
The ABV of all measured beer falls into a range of .1% to 12.8% alcohol content by volume. The data appears to be fairly normally distributed with a touch of right skewness, with a mean of 5.97%, a median of 5.60%, a standard deviation of 1.35% and an interquartile range of 1.7%.
```{r}
summary(Brewdata$ABV)
sd(Brewdata$ABV)
# Plot the histogram:
Brewdata %>% ggplot(aes(ABV)) +
geom_histogram(binwidth = .01, fill='steelblue', color='black') +
theme_economist() +
labs(y='Count of Beers', title='ABV Summary')
```
ABV of all measured beer falls into a range of .1% to 12.8% alcohol content by volume. The data appears to be fairly normally distributed with a touch of right skewness, with a mean of 5.97%, a median of 5.60%, a standard deviation of 1.35% and an interquartile range of 1.7%.
* Median ABV = 5.6%
* Mean ABV = 5.98%
* Range (0.1% - 12.8%)
* Std dev = 1.35%
* IQR = 1.7%
```{r}
# Plot the boxplot:
Brewdata %>% ggplot(aes(OZ, ABV, )) +
geom_boxplot(fill='steelblue', color='black') +
theme_economist() + labs(title='ABV distiribution by container size', x='Fluid Ounces')
```
* 12oz has the lowest median ABV
* 8.4oz has the highest median ABV
### 7. Is there an apparent relationship between the bitterness of the beer and its alcoholic content?
We utilize `ggplot` to plot a scatter plot of the data, using `IBU` and `ABV` as our variables.
Examination of this scatter plot and the regression line suggests that there is a positive, linear relationship between `IBU` and `ABV`.
```{r}
ggplot(Brewdata, aes(x=IBU, y= ABV)) +
geom_point(shape=1) +
geom_smooth(method=lm) +
theme_economist() +
scale_color_economist() +
theme(axis.text.x=element_text(size=rel(1.0))) +
labs(x="IBU",y="ABV", title="Correlation between IBU and ABV")
cor.test(Brewdata$IBU, Brewdata$ABV)
```
It appears that there is some correlation between bitterness and ABV. The data shows a trend that generally as bitterness increases, so does alcohol content. However, alcohol content may increase with or without an increase in bitterness.
* Several of the highly bitter beers have considerably more alcohol in them than thier less bitter counterparts.
* Pearson’s R = .6706
* Pearson’s 𝑅^2 = 45%
There is sufficient evidence (p-value <.0001) that the alcohol by volume (ABV) and International Bittering Units (IBU) are linearly correlated. We estimate that 45% of the variation in ABV is explained by IBU.
### 8. Investigate the difference with respect to IBU and ABV between IPAs (India Pale Ales) and other types of Ale (any beer with “Ale” in its name other than IPA). We will use KNN to classify each 'ale' as either an IPA or Other and provide statistical evidence to back up this claim.
``` {r Imputing the missing data, echo=T, include=F}
Brewdata.impute <- mice(Brewdata [,c("Beer_ID","ABV","IBU")],m=5,maxit=50,meth='pmm',seed=100)
#summary(Brewdata.impute)
Brewdata.impute$imp$ABV #check the imputed data ABV
Brewdata.impute$imp$IBU # check the imputed data IBU
Brewdata.impute.draft <- complete(Brewdata.impute,1) # Complete impute dataset
```
``` {r Create new dataset after imputing the missing data}
Brewdata.impute.draft <- Brewdata.impute.draft %>% dplyr::rename(ABV.New = ABV)
Brewdata.impute.draft <- Brewdata.impute.draft %>% dplyr::rename(IBU.New = IBU)
Brewdata.New <- full_join(Brewdata, Brewdata.impute.draft, by = "Beer_ID")
```
```{r Create 2 dataframes - IPA and Ales and regroup them.}
Ales.beer <- cbind(Brewdata.New, type='Ales', stringsAsFactors=F) %>% filter(grepl('\\bale\\b', Style, ignore.case=T))
IPA.beer <- cbind(Brewdata.New, type='IPA', stringsAsFactors=F) %>% filter(grepl('\\bIPA\\b', Style, ignore.case=T))
IPA.Ales <- union(Ales.beer, IPA.beer)
IPA.Ales$type <- as.factor(IPA.Ales$type)
```
``` {r KNN model}
set.seed(100)
splitPerc = .7
iterations = 100
numks =50
masterAcc = matrix(nrow = iterations, ncol = numks)
for(j in 1:iterations) {
accs = data.frame(accuracy = numeric(numks), k = numeric(numks))
trainIndices = sample(1:dim(IPA.Ales)[1],round(splitPerc * dim(IPA.Ales)[1]))
train = IPA.Ales[trainIndices,]
test = IPA.Ales[-trainIndices,]
for(i in 1:numks) {
classifications = knn(train[,c('IBU.New','ABV.New')],test[,c('IBU.New','ABV.New')],as.factor(train$type), prob = TRUE, k = i)
table(as.factor(test$type),classifications)
CM = confusionMatrix(table(as.factor(test$type),classifications))
masterAcc[j,i] = CM$overall[1]
}
}
MeanAcc = colMeans(masterAcc)
plot(seq(1,numks,1),MeanAcc, type = "l")
k <- which.max(MeanAcc)
```
``` {r The best value of k is 11 for % Acurracy}
classifications = knn(train[,c('IBU.New','ABV.New')],test[,c('IBU.New','ABV.New')],train$type, prob = TRUE, k)
table(test$type,classifications)
confusionMatrix(table(test$type,classifications))
```
### 9. Additional Research.
```{r Create data sets for reference in this file}
# Reset Datafiles:
breweries <- read.csv("./data/Breweries.csv", header = TRUE, strip.white=TRUE)
beers <- read.csv("./data/Beers.csv", header = TRUE)
beers <- beers %>% dplyr::rename( Brew_ID = Brewery_id)
breweries <- breweries %>% dplyr::rename(Name_Brew = Name)
beers <- beers %>% dplyr::rename(Name_Beer = Name)
brew.beer <- full_join(breweries, beers, by = "Brew_ID")
brew.beerABVper <- mutate(brew.beer, ABVper = ABV * 100)
```
```{r Create Regional Data Column based on State}
# Build regional info:
brew.beerABVper$Region[brew.beerABVper$State == "ME"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "NH"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "VT"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "MA"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "RI"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "CT"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "NY"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "NJ"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "PA"] <- "Northeast"
brew.beerABVper$Region[brew.beerABVper$State == "ND"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "SD"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "NE"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "KS"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "MN"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "IA"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "MO"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "WI"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "IL"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "IN"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "MI"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "OH"] <- "Midwest"
brew.beerABVper$Region[brew.beerABVper$State == "OK"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "TX"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "AR"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "LA"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "MS"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "KY"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "TN"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "AL"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "FL"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "GA"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "SC"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "NC"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "VA"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "WV"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "MD"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "DE"] <- "South"
brew.beerABVper$Region[brew.beerABVper$State == "WA"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "OR"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "CA"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "AK"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "HI"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "AZ"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "NV"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "ID"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "MT"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "WY"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "CO"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "NM"] <- "West"
brew.beerABVper$Region[brew.beerABVper$State == "UT"] <- "West"
```
#### Compare Region, City and Brewery Stats {data-background="https://i.ibb.co/M6Yh3pg/In-Bev-Background.png"}
```{r}
# Calculate mean ABV by Region
bbABVper.clean <- brew.beerABVper %>% filter(!is.na(brew.beerABVper$ABVper))
bbABVper.cleanmea <- aggregate(bbABVper.clean$ABVper, by=list(bbABVper.clean$Region), FUN = mean)
colnames(bbABVper.cleanmea) <- c("Region", "MeanABV")
bbABVper.cleanmea$MeanABV <- round(bbABVper.cleanmea$MeanABV, 2)
# Calculate mean IBU by Region
bbIBU.clean <- brew.beerABVper %>% filter(!is.na(brew.beerABVper$IBU))
bbIBU.cleanmed <- aggregate(bbIBU.clean$IBU, by=list(bbIBU.clean$Region), FUN = median)
colnames(bbIBU.cleanmed) <- c("Region", "MedianIBU")
bbIBU.cleanmea <- aggregate(bbIBU.clean$IBU, by=list(bbIBU.clean$Region), FUN = mean)
colnames(bbIBU.cleanmea) <- c("Region", "MeanIBU")
# Calculate Top 10 Cities with the most Breweries
TopCities <- Brewdata %>% mutate(City = paste0(City, ', ', state)) %>% group_by(City) %>%
summarise(Breweries = n()) %>% arrange(desc(Breweries))
# Calculate Top 10 Breweries with the most original beers
TopBreweries <- Brewdata %>% mutate(Brewery = paste0(Brewery,'<br>', City, ', ', state)) %>%
group_by(Brewery) %>% summarise(Beers = n()) %>% arrange(desc(Beers))
Top10Cities <- top_n(TopCities,10)
Top10Breweries <- top_n(TopBreweries,10)
```
#### Build the plots
```{r section 9 build plots}
charts <- list()
charts[['zero']] <- ''
charts[['meanABVbyRegion']] <-
hchart(bbABVper.cleanmea, "column", hcaes(x = Region, y = MeanABV)) %>%
hc_title(text = "Average ABV by Region") %>%
hc_add_theme(hc_theme_economist())
charts[['MedianIBUbyRegion']] <-
hchart(bbIBU.cleanmed, "column", hcaes(x = Region, y = MedianIBU)) %>%
hc_title(text = "Median IBU by Region") %>%
hc_add_theme(hc_theme_economist())
charts[['top10byCity']] <-
hchart(Top10Cities, "column", hcaes(x = City, y = Breweries)) %>%
hc_title(text = "Top 10 Cities with Most Breweries") %>%
hc_add_theme(hc_theme_economist())
charts[['top10byBrewery']] <-
hchart(Top10Breweries, "column", hcaes(x = Brewery, y = Beers)) %>%
hc_title(text = "Top 10 Breweries with Most Original Beers") %>%
hc_add_theme(hc_theme_economist())
```
### Visualization of our additional research!
```{r section 9 display plots}
htmltools::tagList(charts)
```