2
votes

After looking at many examples and lots of trying, I'm still failing to combine text strings and an expression into ggplot2 axis labels to exactly what I want.

what I am trying to get here is the x-axis label to be: axis label

the ingredients:

parname <- 'FL.Red.Total'
xmean <- 123.34
xsigma <- 2580.23

to change the numbers to 10^n notations I use this formula:

sci_form10 <- function(x) {
    paste(gsub("e\\+", " \xB7 10^", scientific_format()(x)))
}

the name would then be build by:

  labs( x = bquote(.(gsub('\\.', '\\ ', parname)) ~ " (a.u.) (" ~ mu ~ "=" ~ .(sci_form10(xmean)) ~ ", " ~ sigma ~ " =" ~ .(sci_form10(xsigma)) ~ ")" ))

I'm hoping to replace 10^04 with 10 followed by a 4 in superscript and to add a linebreak to the labels as the first image shows

The test code:

 library(ggplot2)
 library(scales)
 sci_form10 <- function(x) {
        paste(gsub("e\\+", " * 10^", scientific_format()(x)))
 }

 parname <- 'FL.Red.Total'
 xmean <- 123.34
 xsigma <- 2580.23
 ggplot(mtcars, aes(x=mpg,y=cyl)) +
        geom_point() +  
        labs( x = bquote(.(gsub('\\.', '\\ ', parname)) ~ " (a.u.) (" ~ mu ~ "=" ~ .(sci_form10(xmean)) ~ ", " ~ sigma ~ " =" ~ .(sci_form10(xsigma)) ~ ")" ))

gives:

best effort

p.s. I also tried

sci_form10 <- function(x) {
        paste(gsub(".*e\\+", "10^", scientific_format()(x)))
  }

which only gives the 10^03 part to see if that would change the outcome of my label, but no.

2
sorry, typo there. should be a . instead of , and also the name of the equation should be the same. Making correction now.Mark
I tested your code. But, it gives an error Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : invalid multibyte string at '<fc><be><8d><b6><88><bc> 10^02'akrun
Hmmm I just tested the updated code block after restarting R, I do not get the same error (without loading any other packages than the ones in the question), but sometimes the \xB7 floating point seems to give problems. You can try to remove that from the function sci_form10()Mark
Though I can't test it, an option is bquote(atop(akrun
I removed the problematic symbol in the question code Akrun. Perhaps it works now.Mark

2 Answers

2
votes

An option would be wrap with atop to create line breaks

sci_form10 <- function(x) {
  paste(gsub("e\\+", " \u00B7 10^", scientific_format()(x)))
    }

x1 <-  sci_form10(xmean)
x2 <-  sci_form10(xsigma)
lst1 <- strsplit(c(x1,x2), "\\s(?=10)", perl = TRUE)
pre <- sapply(lst1, `[`, 1)
post <- sapply(lst1, `[`, 2)  
xmean1 <- parse(text = paste0("'", pre[1], "'"))[[1]]
xsigma1 <- parse(text = paste0("'", pre[2], "'"))[[1]]
post1 <- parse(text = post[1])[[1]]
post2 <- parse(text = post[2])[[1]]
ggplot(mtcars, aes(x=mpg,y=cyl)) +
                  geom_point() +  
                    labs( x = bquote(atop(.(gsub("\\.", "\\ ", 
                      parname))~"(a.u.)"~phantom(), "(" ~ mu~ " = "~ .(xmean1) ~ .(post1) ~ ", " ~ sigma ~ " = " ~ .(xsigma1) ~ .(post2)~ ")")))

-output

enter image description here

0
votes

I have something that does most of what you wanted.

changeSciNot <- function(n) {
  output <- format(n, digits=3, scientific = TRUE) # Transforms the number into scientific notation even if small
  output <- sub("e", "*10^", output) # Replace e with 10^
  output <- sub("\\+0?", "", output) # Remove + symbol and leading zeros on exponent, if > 1
  output <- sub("-0?", "-", output) # Leaves - symbol but removes leading zeros on exponent, if < 1
  output
}

# example data
parname <- "FL.Red.Total"
xmean <- 123.34
xsigma <- 2580.23
label <- bquote(atop(.(gsub("\\.", "\\ ", parname)) ~ "(a.u.)",
                     mu*"="*.(changeSciNot(xmean))*"," ~ sigma*"="*.(changeSciNot(xsigma))))

ggplot(mtcars, aes(x=mpg,y=cyl)) +
  geom_point() +  
  labs(x = label)

The changeSciNot function came from this thread. I had some problems using \xB7 for the multiplication, so I left *. I also hard coded the number of digits for the format, but you can also make it into an argument. Hopefully, this will get you closer to the exact desired output.