【发布时间】:2019-11-04 08:22:40
【问题描述】:
我问了这个问题here,才发现其实不是我的问题。
问题似乎是,我需要bquote 解释的变量ab 和hello,但它不起作用。如何让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 设置变量内容的样式,而不是将其作为简单的字符串...