What formatting language does ggtext use to format text?

Viewed 450

I am trying to display scientific notation on a ggplot2 axis in boldface, with the literal "Ax10^B" format, not "AeB" format that is the default of ggplot2. When this code is run

library(tidyverse)
library(ggtext)
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"), 
        axis.text.y = element_markdown(face = "bold")) 


this is the result: Output of code

I use element_markdown() from ggtext because it allows the bold face to be transferred as I discovered here: How do I make ggplot2 custom text formats from axis scale functions follow format specifications set in theme()?

I can fix the double quotes by changing '\\1' to \\1 (deleting the single quotes). But I am having trouble getting the multiplication sign to display. I could just use a lowercase x but that is lazy.

When I try to use $\times$ as suggested here https://rstudio-pubs-static.s3.amazonaws.com/18858_0c289c260a574ea08c0f10b944abc883.html I get an error. A vignette for ggtext seems to use html: https://cran.r-project.org/web/packages/ggtext/vignettes/theme_elements.html but they use <sup> tags which seems to go against the use of ^ to make an exponenent here, and the tags don't work when I use them, and all resources for "multiplication sign in html" that I searched for haven't yielded a solution. So my question is: Where can I find a good resource to learn the proper formatting language that ggtext/ ggplot2 uses for axis tick labels? Would also like to know the solution to the specific problems I'm having.

2 Answers

{ggtext} uses Markdown/HTML. You can insert special characters either by just using unicode characters or by using HTML entities. Here, you probably want &times;.

Also, don't parse strings into expressions when working with {ggtext}.

library(tidyverse)
library(ggtext)

#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", "\\1e", l)
  # turn the 'e+' into plotmath format
  l <- gsub("e", "&times;10^", l)
  # return this as a string
  l
}


ggplot(mpg, aes(displ, hwy*10^9)) + 
  theme_classic() +
  geom_point() + 
  scale_y_continuous(labels= fancy_scientific)  +
  theme(text = element_text(face = "bold"), 
        axis.text.y = element_markdown(face = "bold")) 

Created on 2020-08-20 by the reprex package (v0.3.0)

Here is a version with just plotmath expressions:

library(dplyr)
library(ggplot2)

fancy_scientific <- function(l) {
    l <- format(l, scientific = TRUE)
    parse(text=gsub("(.*)e(\\+?)(\\-?[0-9]+)", 
        "bold('\\1') * bold(' * ') * bold('10')^bold('\\3')", l))
}

mpg %>% dplyr::mutate(hwy = hwy * 1e9) %>% 
    ggplot(aes(displ, hwy)) + 
    theme_classic() +
    geom_point() + 
    scale_y_continuous(labels= fancy_scientific) +
    theme(text = element_text(face = "bold"))

... and here is a version with ggtext:

library(dplyr)
library(ggplot2)
library(ggtext)

fancy_scientific <- function(l) {
    l <- format(l, scientific = TRUE)
    parse(text=gsub("(.*)e(\\+?)(\\-?[0-9]+)", "\\1 * 10^(\\3)", l))

    ## this would also work, instead of the line above:
    # gsub("(.*)e(\\+?)(\\-?[0-9]+)", "**\\1 \\* 10<sup>\\3</sup>**", l)
}

mpg %>% dplyr::mutate(hwy = hwy * 1e9) %>% 
    ggplot(aes(displ, hwy)) + 
    theme_classic() +
    geom_point() + 
    scale_y_continuous(labels= fancy_scientific)  +
    theme(text = element_text(face = "bold"), 
          axis.text.y = element_markdown(face = "bold"))

Created on 2020-08-20 by the reprex package (v0.3.0)

Related