2017-05-31 49 views
1

我想合併兩個用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 = ""))) 

非常感謝!

回答

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

注意paste在plotmath沒有sep參數。你可能想學習help("plotmath")

+0

太棒了!非常感謝! – user304347