如何让 `bquote` 用符号替换存储在变量中的希腊字母? [英] How can I make `bquote` replace the greek letter stored in a variable with the symbol?

查看:17
本文介绍了如何让 `bquote` 用符号替换存储在变量中的希腊字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态标记绘图上的轴.标签来自数据框,包含希腊字母以及超级/订阅.

I want to label the axis on a plot dynamically. The labels come from a data frame and contain greek letters as well as super/sub scription.

在静态情况下,我会知道标签的字母,bquote 会很好用.但在标签字符串来自变量的情况下,bquote 失败.

In a static case, where I would know the letters of my labels, bquote would work well. But in the case, where the label-string comes from a variable, bquote fails.

这展示了我想要实现的目标:

This demonstrates what I want to achieve:

a <- "alpha"
b <- "beta"
ggplot(data.frame(x=c(1), y=c(1)), aes(x, y)) + 
  geom_point() +
  labs(x = bquote(.(a)[.(b)])) + ## will output the greek letters by "name"
  labs(y = bquote(alpha[beta]))  ## the greek letter-names are replaces by the symbols

推荐答案

rlang::sym将a和b变量转成符号.

Turn the a and b variable into symbols with rlang::sym.

library("tidyverse")

a <- "alpha"
b <- "beta"

ggplot(data.frame(x = c(1), y = c(1)), aes(x, y)) +
  geom_point() +
  labs(x = bquote(.(sym(a))[.(sym(b))])) +
  labs(y = bquote(alpha[beta]))

reprex 包 (v0.3.0) 于 2019 年 11 月 4 日创建支持>

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

这篇关于如何让 `bquote` 用符号替换存储在变量中的希腊字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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