bquote没有解释变量中的样式元素 [英] `bquote` is not interpreting the styling elements within a variable

查看:50
本文介绍了bquote没有解释变量中的样式元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了这个问题

使用dipetkov的答案,我做了以下操作来定义标签,然后可以将其简单地用于ggplot的 labs 函数中.请注意,整个内容都包装在 bquote 中,而需要样式化的变量还包装在.(parse_expr(paste(...)).

中.

  ylab<-bquote(.(data $ y_name [1])〜.(parse_expr(paste(f(data $ y_symbol [1])))))〜"="〜"[" *.(data $ y_unit [1])*]")ggplot(...)+ ... +实验室(y = ylab) 

实际上,对于我来说,粘贴是不必要的.

解决方案

几乎在......代字号不会与 bquote parse_expr (因为在R中,它表示公式?).所以我用破折号-代替了它.

  library("rlang")图书馆("tidyverse")ab <-αβ"你好<-你好[你好]"ggplot(data.frame(x = c(1),y = c(1)),aes(x,y))+geom_point()+实验室(x = parse_expr(paste(ab,-",hello)))+实验室(y = bquote(alphaβ〜hello [hello])) 

reprex软件包(v0.3.0)创建于2019-11-04 sup>

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.

解决方案

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)

这篇关于bquote没有解释变量中的样式元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆