合并表达式以进行绘图

时间:2017-05-31 11:28:47

标签: r plotmath

我正在尝试合并用bquote制作的两个表达式。例如:

a = 1
b = 2

x1 = as.expression(bquote(paste("Something ", alpha, " = ", .(a), sep = "")))
x2 = as.expression(bquote(paste("Something else ", beta, " = ", .(b), sep = "")))

有没有办法在不做的情况下执行与x12 = paste(x1, x2, collapse = "some symbol")类似的操作:

x12 = as.expression(bquote(paste("Something ", alpha, " = ", .(a)," some symbol ",
"Something else ", beta, " = ", .(b), sep = "")))

非常感谢!

1 个答案:

答案 0 :(得分:0)

你可以写一个结合了plotmath表达式的小函数:

a = 1
b = 2

x1 = bquote("Something " * alpha == .(a))
x2 = bquote("Something else " * beta == .(b))

comb_plotmath <- function(...) {
  Reduce(function(x, y) substitute(x * y, env = list(x = x, y = y)), 
         list(...))
}


plot.new()
text(0.5, 0.5, comb_plotmath(x1, " some symbol ", x2))

结果:

resulting plot displaying the expression

请注意,plotmath中的paste没有sep参数。您可能想要学习help("plotmath")