Skip to content

Instantly share code, notes, and snippets.

@KasperSkytte
Created July 26, 2019 08:29
Show Gist options
  • Save KasperSkytte/f90c22257dc9ccffca41ec97055650de to your computer and use it in GitHub Desktop.
Save KasperSkytte/f90c22257dc9ccffca41ec97055650de to your computer and use it in GitHub Desktop.
Homebrew: R code to generate a colored table to adjust hydrometer reading for printing (A4). See resulting plot at https://imgur.com/gallery/85Xe1XY
#source adjustHydrometer() function
source("https://gist.githubusercontent.com/KasperSkytte/0b94ae24645d16e97099a201f48bcd8b/raw/0b1676233d0fd380224359badb8584723022d83d/adjustHydrometer.R")
library(ggplot2)
#make a data frame with all combinations of selected ranges of SG and temperature
SG <- seq(1010L, 1110L, 2L)-1000L
tempC = seq(10, 75, 1L)
df <- expand.grid(SG = SG,
tempC = tempC)
df$tempF <- df$tempC*1.8+32
df$CSG <- round(adjustHydrometer(SG = df$SG+1000, temp = df$tempC, calibTemp = 20, unit = "c")-1000, 0)
#generate plot
ggplot(df,
aes(x = SG,
y = tempC,
fill = CSG,
label = CSG)) +
geom_raster() +
geom_text(size = 3) +
#colors are from RColorBrewer::brewer.pal(3, "Set1")[c(2,1)]
scale_fill_gradientn(colors = c("#377EB8", "#E41A1C"), guide = "none") +
scale_x_continuous(breaks = unique(df$SG),
expand = c(0,0),
sec.axis = dup_axis(),
name = "Specific gravity (-1000) [g/L]") +
scale_y_continuous(breaks = tempC,
expand = c(0,0),
name = "Temperature [°C]",
sec.axis = dup_axis(breaks = unique(df$tempC),
labels = unique(df$tempF),
name = "Temperature [°F]")) +
theme(panel.background = element_blank(),
axis.ticks = element_blank(),
axis.title.y.left = element_text(margin = margin(r = 10)),
axis.title.y.right = element_text(margin = margin(l = 10)),
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 = "adjustGravity.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