I want to change scientific notation on ggplot2 axes from a format like 3.23e+6
to 3.23 × 10^6
. Thankfully this question has been answered here: How do I change the formatting of numbers on an axis with ggplot?
It works well for basic cases. However, it does not work when you want to change the formatting of the axis label. This is illustrated by this example:
library(tidyverse)
ggplot(mpg, aes(displ, hwy*10^9)) + geom_point()
#makes the scientific notation using "AeB" explicitly write out Ax10^B
fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l, scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e", "'\\1'e", l)
# turn the 'e+' into plotmath format
l <- gsub("e", "%*%10^", l)
# return this as an expression
parse(text=l)
}
ggplot(mpg, aes(displ, hwy*10^9)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"))
Which yields:
The problem is that the Y axis text is not bold as specified in the call to theme
. When I use browser
to see what is happening inside of fancy_scientific
I see that it returns an object of class "expression" which in this case is printed as
expression('2'%*%10^+01, '3'%*%10^+01, '4'%*%10^+01)
while the function scales::scientific
, which can be used to force scientific notation of the kind I want to avoid but conforms to whatever theme specifications I set, returns a vector of strings directly. When I modify fancy_scientific
to return a vector of strings like '2'%*%10^+01
they are directly rendered to the displayed Y axis.
So the question is how do I make the output of the fancy_scientific
function conform to my theme specification?