I asked this question here, only to discover that it is actually not quite my problem.
The problem seems to be, that I need the variable ab
and hello
interpreted by bquote
, but it doesn't work. How can I make bquote
to interprete the variable content?
a <- "alpha"
b <- "beta"
ab <- "alpha[beta]"
hello <- "hello[hello]"
ggplot(data.frame(x=c(1), y=c(1)), aes(x, y)) +
geom_point() +
labs(x = bquote(.(sym(a))[.(sym(b))]~.(sym(ab))~.(hello))) + ## will output the greek letters by "name"
labs(y = bquote(alpha[beta]~hello[hello])) ## the greek letter-names are replaces by the symbols
Edit:
With dipetkov's answer I did the following to define my label, which can then be simply used in ggplot's labs
function. Note that the whole thing is wrapped in bquote
while the the variable which needs styling is additional wrapped in .(parse_expr(paste(...))
.
ylab <- bquote(.(data$y_name[1])
~.(parse_expr(paste(f(data$y_symbol[1]))))
~"="~
"["*
.(data$y_unit[1])
*"]")
ggplot(...) + ... +
labs(y = ylab)
Edit 2: actually, the paste
is unnecessary in my case.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…