我想在图上动态标记轴。标签来自数据框,包含希腊字母以及超级/预订。
在静态情况下,如果我会知道标签字母,bquote
会很好。但是,如果标签字符串来自变量,则bquote
失败。
这演示了我想要实现的目标:
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变量转换为符号。
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 package(v0.3.0)在2019-11-04创建