【问题标题】:`bquote` is not interpreting the styling elements within a variable`bquote` 没有解释变量中的样式元素
【发布时间】:2019-11-04 08:22:40
【问题描述】:

我问了这个问题here,才发现其实不是我的问题。

问题似乎是,我需要bquote 解释的变量abhello,但它不起作用。如何让bquote 解释变量内容?

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

编辑:

根据 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(...) + ... +
labs(y = ylab)

编辑 2:实际上,paste 在我的情况下是不必要的。

【问题讨论】:

  • 您希望 X 轴和 Y 轴有哪些标签?你的预期输出是什么?
  • y 轴上显示的内容。我希望 bquote 设置变量内容的样式,而不是将其作为简单的字符串...

标签: r ggplot2


【解决方案1】:

差不多了...波浪符号~ 不能与bquoteparse_expr 一起打印(因为在R 中它表示公式?)。所以我用破折号代替了它,-

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]))

reprex package (v0.3.0) 于 2019 年 11 月 4 日创建

【讨论】:

    猜你喜欢
    • 2021-07-27
    • 2019-12-19
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多