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

查看:180
本文介绍了如何使`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-04 sup>

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

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

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