Skip to content

Instantly share code, notes, and snippets.

@founddrama
Created August 24, 2019 15:37
Show Gist options
  • Save founddrama/ece13e36fbdf4db9299f394243a72a3b to your computer and use it in GitHub Desktop.
Save founddrama/ece13e36fbdf4db9299f394243a72a3b to your computer and use it in GitHub Desktop.
Exploring the BJCP style data by way of R and ggplot
---
title: "BJCP style exploration"
author: "Rob Friesel"
date: "10/20/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library("ggplot2")
setwd("~/Desktop")
bjcp.styles <- read.csv("https://docs.google.com/spreadsheets/d/1G56sXHJn4HMaoaFN8chFPyxDfSoz5gFN-w8z02EWJQU/gviz/tq?tqx=out:csv&sheet=2015")
# FIX FACTOR ORDER
bjcp.styles$Style.Name <- factor(as.character(bjcp.styles$Style.Name),
levels = as.character(bjcp.styles$Style.Name),
ordered = TRUE)
srm.scale <- c("#FEE287", "#FED265", "#FEC048", "#FDB335", "#F9A21C",
"#F49708", "#ED8A09", "#E37C07", "#DE7206", "#D46806",
"#CC5E06", "#C35404", "#BC4D03", "#B44605", "#AB3C04",
"#A53904", "#9E3203", "#932C02", "#8D2601", "#882203",
"#811F02", "#7A1C02", "#721701", "#6D1301", "#671002",
"#630F00", "#5C0C00", "#560601", "#520500", "#4B0601",
"#470403", "#4C0102", "#410409", "#390005", "#350106",
"#330109", "#2F0309", "#2B0309", "#2B050B", "#28060A")
formatGravity <- function(g) sprintf("%.3f", g)
#reorder.factor.by <- function(df, f, d) {
# df[[f]] <- factor(df[[f]], levels = df[[f]][order(-df[[d]])])
# df[order(-df[[d]]),]
#}
#beers.for.pie <- reorder.factor.by(beers.for.pie, "styles", "total")
```
```{r}
ggplot(bjcp.styles, aes(IBU.max, OG.max, group = Category.Name, color = Category.Name)) +
geom_point(aes(IBU.max, OG.max, group = Category.Name, color = Category.Name)) +
geom_point(aes(IBU.min, OG.min, group = Category.Name, color = Category.Name)) +
geom_segment(aes(x = IBU.min, y = OG.min,
xend = IBU.max, yend = OG.max,
group = Category.Name, color = Category.Name)) +
scale_color_manual(values = srm.scale) +
ylab("Original Gravity Range") +
xlab("IBU Range") +
theme(legend.position = "none")
ggplot(bjcp.styles, aes(IBU.max, ABV.max, group = Category.Name, color = Category.Name)) +
geom_point(aes(IBU.max, ABV.max, group = Category.Name, color = Category.Name)) +
geom_point(aes(IBU.min, ABV.min, group = Category.Name, color = Category.Name)) +
geom_segment(aes(x = IBU.min, y = ABV.min,
xend = IBU.max, yend = ABV.max,
group = Category.Name, color = Category.Name)) +
scale_color_manual(values = srm.scale) +
ylab("% ABV Range") +
xlab("IBU Range") +
theme(legend.position = "none")
```
```{r}
ggplot(bjcp.styles, aes(SRM.max, ABV.max, group = Category.Name, color = Category.Name)) +
geom_point() +
xlab("SRM Max Range") +
ylab("ABV Max Range") +
scale_color_manual(values = srm.scale) +
theme(legend.position = "none")
```
```{r cat6, echo=FALSE, eval=FALSE}
ibu.vs.og <- function(cn) {
cat <- bjcp.styles[bjcp.styles$Category == cn,]
ggplot(cat, aes(IBU.min, OG.min, group = Style.Name, color = Style)) +
geom_point(aes(IBU.min, OG.min, group = Style.Name, color = Style)) +
scale_color_manual(values = srm.scale[cat$SRM.min],
labels = cat$Style.Name) +
geom_point(aes(IBU.max, OG.max, group = Style.Name, color = Style),
stat = "identity", color = srm.scale[cat$SRM.max]) +
geom_segment(aes(x = IBU.min, y = OG.min,
xend = IBU.max, yend = OG.max,
group = Style, color = Style)) +
annotate("text", x = cat$IBU.min + 0.5, y = cat$OG.min, hjust=0,
label = cat$Style.Name, color=srm.scale[cat$SRM.max], size = 3) +
scale_y_continuous(labels = formatGravity) +
ylab("Original Gravity Range") +
xlab("IBU Range") +
ggtitle(paste0("Category ", cn, ". ", head(cat$Category.Name, 1)))
}
lapply(1:27, ibu.vs.og)
abv.vs.og <- function(cn) {
cat <- bjcp.styles[bjcp.styles$Category == cn,]
ggplot(cat, aes(IBU.min, ABV.min, group = Style.Name, color = Style)) +
geom_point(aes(IBU.min, ABV.min, group = Style.Name, color = Style)) +
scale_color_manual(values = srm.scale[cat$SRM.min],
labels = cat$Style.Name) +
geom_point(aes(IBU.max, ABV.max, group = Style.Name, color = Style),
stat = "identity", color = srm.scale[cat$SRM.max]) +
geom_segment(aes(x = IBU.min, y = ABV.min,
xend = IBU.max, yend = ABV.max,
group = Style, color = Style)) +
annotate("text", x = cat$IBU.min + 0.5, y = cat$ABV.min, hjust=0,
label = cat$Style.Name, color=srm.scale[cat$SRM.max], size = 3) +
ylab("% ABV Range") +
xlab("IBU Range") +
ggtitle(paste0("Category ", cn, ". ", head(cat$Category.Name, 1)))
}
lapply(1:27, abv.vs.og)
```
```{r cat8, echo=FALSE}
catNo <- 1
cat8 <- bjcp.styles[bjcp.styles$Category == catNo,]
ggplot(cat8, aes(IBU.min, OG.min, group = Style.Name, color = Style)) +
geom_point(aes(IBU.min, OG.min, group = Style.Name, color = Style)) +
scale_color_manual(values = srm.scale[cat8$SRM.min],
labels = cat8$Style.Name) +
geom_point(aes(IBU.max, OG.max, group = Style.Name, color = Style),
stat = "identity", color = srm.scale[cat8$SRM.max]) +
geom_segment(aes(x = IBU.min, y = OG.min,
xend = IBU.max, yend = OG.max,
group = Style, color = Style)) +
scale_y_continuous(labels = formatGravity) +
annotate("text", x = cat8$IBU.max + c(0, 0.25, -3.5, -6.5),
y = cat8$OG.max + c(-0.001, 0, 0, 0),
hjust=0, label = cat8$Style.Name,
color=srm.scale[cat8$SRM.max], size = 3) +
ylab("Original Gravity Range") +
xlab("IBU Range") +
ggtitle(paste0("Category ", catNo, ". ", head(cat8$Category.Name, 1)))
```
```{r all.cat.og.range, echo=FALSE}
base.styles <- subset(bjcp.styles, bjcp.styles$Category <= 27)
ggplot(base.styles, aes(Style.Name)) +
geom_linerange(aes(x = Style.Name, color = Style.Name,
ymax = OG.max, ymin = OG.min),
size = 1.5) +
scale_color_manual(values = srm.scale[base.styles$SRM.min],
labels = base.styles$Style.Name) +
scale_y_continuous(labels = formatGravity) +
ylab("O.G. Range") +
xlab("Style") +
theme(legend.position = "none",
panel.grid.major.x = element_blank(),
axis.text.x = element_text(angle = 90, size = 4, hjust = 1))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment