Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

enter image description here

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.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
221 views
Welcome To Ask or Share your Answers For Others

1 Answer

Almost there... The tilde symbol, ~, doesn't print with either bquote or parse_expr (because in R it signifies a formula?). So I've substituted it with a dash, -.

library("rlang")
library("tidyverse")

ab <- "alpha[beta]"
hello <- "hello[hello]"

ggplot(data.frame(x = c(1), y = c(1)), aes(x, y)) +
  geom_point() +
  labs(x = parse_expr(paste(ab, "-", hello))) +
  labs(y = bquote(alpha[beta] ~ hello[hello]))

Created on 2019-11-04 by the reprex package (v0.3.0)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...