在动态变化的图中显示分数

时间:2017-03-02 21:21:51

标签: r plot plotmath

我想知道如何实际显示条件计算(见下文)我用来获取我在R代码中调用GG的内容(参见下面的代码)?

详细信息:

准确地说,我想在我的情节中显示GG = G1 / G2(显示为IF type = 1)和ELSE GG =(G1 / G2)* 2(显示)如果type == 2)。

例如,如果G1 = 1,G2 = 2,我希望根据“类型”显示的内容是以下之一:

enter image description here

注意:在上图中,我快速使用了“X 2”,但我需要一个实际的 x-like数学符号作为乘法符号

以下是我的R代码:

G1 = 1  ## Can be any other number, comes from a function
G2 = 2  ## Can be any other number, comes from a function
type = 1 ## Can be either 1 or 2

plot(1:10,ty="n",ann=F, bty="n")

text( 6, 6, expression(paste("GG = ", ifelse(type==1, frac(G1, G2), frac(G1, G2)*2 ), " = ", 
           G1/G2, sep = "")), col = "red4", cex = 1.6 )

2 个答案:

答案 0 :(得分:3)

使用bquote,使用.()将动态值添加到表达式中:

type = 1
plot(1:10, ty = "n", ann = F, bty = "n")
text(6, 6,
     bquote(paste("GG = ", frac(.(G1), .(G2)),
                  .(ifelse(type == 1, " × 2", "")),
                  " = ", 
                  .(ifelse(type == 1, G1/G2 * 2, G1/G2)), sep = "")),
     col = "red4", cex = 1.6)

enter image description here

注意:乘法符号(×)的ASCII代码为Alt 0215

答案 1 :(得分:0)

很棒的@ zx8754,非常好。

我误解了你的问题,并认为你想打印公式和结果,所以我制作了这个。

plot(1:10,ty="n",ann=F, bty="n")
text( 6, 6, if (type==1) substitute(paste("GG = ", frac(G1, G2)) == list(x), list(x=G1/G2)) else
               substitute(paste("GG = ", frac(G1, G2), " x 2") == list(x), list(x=G1/G2 * 2)), col = "red4", cex = 1.6 )

enter image description here