Skip to content

Instantly share code, notes, and snippets.

@KasperSkytte
Created July 26, 2019 08:43
Show Gist options
  • Save KasperSkytte/f1c65a1872497a2fc5ceca31187b2e61 to your computer and use it in GitHub Desktop.
Save KasperSkytte/f1c65a1872497a2fc5ceca31187b2e61 to your computer and use it in GitHub Desktop.
Homebrew: R code to generate a colored table to estimate alcohol by volume (ABV) for printing (A4). See resulting plot at https://imgur.com/gallery/wmd4zzJ
library(ggplot2)
library(purrr)
#make a data frame with all combinations of selected ranges of OG and SG
OG = seq(1030L, 1110L, 2L)
FG = seq(1005L, 1030L, 1L)
df <- expand.grid(OG = OG,
FG = FG)
#calculate ABV with all combinations of OG and SG
df$ABV <- 1.05*(df$OG-df$FG)/(df$FG*0.79)*100
#generate plot
ggplot(df,
aes(x = FG,
y = OG,
#ensure labels have the same width
label = map(ABV,
function(x) {
if(x >= 10.00)
x <- format(round(x, 1), nsmall = 1)
else if(x < 10.00)
x <- format(round(x, 2), nsmall = 2)
return(as.character(x))
}),
fill = ABV)) +
geom_raster() +
geom_text() +
#colors are from RColorBrewer::brewer.pal(3, "Set1")[c(2,1)]
scale_fill_gradientn(colors = c("#377EB8", "#E41A1C"), guide = "none") +
scale_x_continuous(breaks = FG, expand = c(0,0), sec.axis = dup_axis(), name = "Final gravity [g/L]") +
scale_y_continuous(breaks = OG, expand = c(0,0), sec.axis = dup_axis(), name = "Original gravity [g/L]") +
theme(panel.background = element_blank(),
axis.ticks = element_blank(),
axis.title.y.left = element_text(margin = margin(r = 10)),
axis.title.y.right = element_blank(),
axis.title.x.bottom = element_text(margin = margin(t = 10)),
axis.title.x.top = element_blank(),
axis.text.x.top = element_text(angle = 90, vjust = 0.5, hjust = 0),
axis.text.x.bottom = element_text(angle = 90, vjust = 0.5, hjust = 0))
#Save in A4 format for printing
ggsave(file = "ABVtable.png",
width = 297,
height = 210,
units = "mm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment