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:
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:
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.
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'
– akrunbquote(atop(
– akrun