fancy ggplot2 axis format

Prerequisite

knitr::opts_chunk$set(fig.dim = c(8, 8),
                      message = FALSE,
                      warning = FALSE)
library(tidyverse)

Fancy the axis output

We take the diamonds dataset from ggplot2 package as an example. The default scales::scientific_format() generates a little ugly axis format that is not nice to human instinct, such as ‘1.5e+04’.

ggplot(diamonds, aes(color, price)) + 
  geom_point() + 
  facet_grid(cut ~ clarity) + 
  scale_y_continuous(labels = scales::scientific_format())

The format ‘1.5 x 104’ is preferable for us, at least for me. To achieve this, we first create a fancy_axis function.

fancy_axis <- function(x) {
    ifelse(x == 0, "0", parse(text = gsub("[+]", "", gsub("e", " %*% 10^", scales::scientific_format()(x)))))
}

Now we just need specify the scale_y_continuous(labels = fancy_axis) and get a pretty nicer look.

ggplot(diamonds, aes(color, price)) + 
  geom_point() + 
  facet_grid(cut ~ clarity) + 
  scale_y_continuous(labels = fancy_axis)