Skip to content

Instantly share code, notes, and snippets.

@mikebirdgeneau
Created September 6, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebirdgeneau/75e3617b6db7e1b04bbe to your computer and use it in GitHub Desktop.
Save mikebirdgeneau/75e3617b6db7e1b04bbe to your computer and use it in GitHub Desktop.
library(ggplot2)
library(data.table)
# Metric Table
dt.forceCarb<-data.table(expand.grid(Pressure_bar=seq(0,2,by=0.1),Temperature_C=seq(0,25,by=1)))
dt.forceCarb[,CO2_gl := (Pressure_bar+1.013)*(2.71828182845904^(-10.73797+(2617.25/(Temperature_C+273.15))))*10,]
dt.forceCarb[,CO2_vol:=CO2_gl/1.96,]
dt.forceCarb[,Carbonation:="Over-Carbonated",]
dt.forceCarb[CO2_vol<4,Carbonation:="Highly Carbonated (Lambics / Wheat Beers)",]
dt.forceCarb[CO2_vol<2.6,Carbonation:="Moderate Carbonation (Lagers, Ales, Ambers)",]
dt.forceCarb[CO2_vol<2.2,Carbonation:="Low Carbonation (Stouts / Porters)",]
dt.forceCarb[CO2_vol<1.4,Carbonation:="Undercarbonated",]
dt.forceCarb[,Pressure_kPa:=100*Pressure_bar,]
dt.forceCarb[,Carbonation:=factor(Carbonation,levels = unique(dt.forceCarb[order(-CO2_vol),Carbonation]),ordered = TRUE),]
p1<-ggplot(dt.forceCarb,aes(x=Pressure_kPa,y=Temperature_C))+geom_tile(aes(fill=Carbonation))+theme_bw()+theme(legend.position="bottom")+geom_text(aes(label=formatC(round(CO2_vol,2),digits=2,format = 'f',width=2,drop0trailing=FALSE)))+scale_fill_brewer(palette="Spectral")+ggtitle("Beer - Force Carbonation Chart")+scale_y_reverse(breaks = seq(0, 25, 1))+scale_x_continuous(breaks=seq(0,200,by=10))+ylab("Temperature (C)")+xlab("Pressure (kPa)")
# Metric with PSI (for those of us with imperial gauges)
dt.forceCarb<-data.table(expand.grid(Pressure_psi=seq(0,30,by=1),Temperature_C=seq(0,25,by=1)))
dt.forceCarb[,Pressure_bar:=0.069*Pressure_psi,]
dt.forceCarb[,CO2_gl := (Pressure_bar+1.013)*(2.71828182845904^(-10.73797+(2617.25/(Temperature_C+273.15))))*10,]
dt.forceCarb[,CO2_vol:=CO2_gl/1.96,]
dt.forceCarb[,Carbonation:="Over-Carbonated",]
dt.forceCarb[CO2_vol<4,Carbonation:="Highly Carbonated (Lambics / Wheat Beers)",]
dt.forceCarb[CO2_vol<2.6,Carbonation:="Moderate Carbonation (Lagers, Ales, Ambers)",]
dt.forceCarb[CO2_vol<2.2,Carbonation:="Low Carbonation (Stouts / Porters)",]
dt.forceCarb[CO2_vol<1.4,Carbonation:="Undercarbonated",]
dt.forceCarb[,Carbonation:=factor(Carbonation,levels = unique(dt.forceCarb[order(-CO2_vol),Carbonation]),ordered = TRUE),]
p2<-ggplot(dt.forceCarb,aes(x=Pressure_psi,y=Temperature_C))+geom_tile(aes(fill=Carbonation))+theme_bw()+theme(legend.position="bottom")+geom_text(aes(label=formatC(round(CO2_vol,2),digits=2,format = 'f',width=2,drop0trailing=FALSE)))+scale_fill_brewer(palette="Spectral")+ggtitle("Beer - Force Carbonation Chart")+scale_y_reverse(breaks = seq(0, 25, 1))+scale_x_continuous(breaks=seq(0,30,by=1))+ylab("Temperature (C)")+xlab("Pressure (psi)")
print(p1)
print(p2)
png(filename = "force_carbonation.png",width=2880,height=1800,res=150)
print(p1)
dev.off()
png(filename = "force_carbonation_psi.png",width=2880,height=1800,res=150)
print(p2)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment