Mercurial
Mercurial

Reputation: 195

Bar notation in base-10 log scale in ggplot2

Many years ago, engineers used a bar notation for negative base-10 logarithms (bar over the characteristic). Is there a way to use the bar notation in ggplot2? If not, how can I create custom breaks with Unicode, as in "\bar{2}.07918" or something along those lines?

Upvotes: 3

Views: 131

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174586

You can use the labels argument to convert each label to a plotmath expression. If I understand you correctly, you are looking for the exponent-mantissa representation of numbers as used in the days of slide rules and log tables, where the negative values of the exponent were represented by bars above the numbers. This requires a little function to generate the labels:

bar_notation <- function(x) {
  mantissa <- log10(x) - floor(log10(x))
  exponent <- floor(log10(x))
  e <- ifelse(exponent < 0, paste0("bar(", -exponent, ")"), paste(exponent))
  m <- ifelse(mantissa == 0, "000", substr(as.character(mantissa), 3, 6))
  parse(text = paste(e, "*.*", m))
}

We can pass this function directly to the labels argument of scale_y_log10. For demonstration purposes, let's use the powers of e as breaks on our axis:

library(ggplot2)

ggplot(df, aes(x, y)) + 
  geom_boxplot(fill = "lightblue") +
  scale_y_log10(labels = bar_notation, breaks = exp(seq(-4, 4))) +
  theme_minimal(base_size = 16)

enter image description here


To interpret these numbers, we simply add the values on each side of the period (with the negative values denoted by the bar). For example, we can see on our plot that the lowest value of y is just below the bar(2).2628. To find out what this number represents, we can do -2 + 0.2628 = -1.7372. This means the value on the y axis is 10^-1.7372, or 0.01831471. We can confirm the lowest value of y is just below this:

min(df$y)
#> [1] 0.01766628

Data used

set.seed(1)

df <- data.frame(x = rep(c("A", "B"), each = 10), y = 10^runif(20, -2, 2))

Upvotes: 4

Related Questions