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

Display a fraction in a dynamically changing plot

我想知道如何实际显示 条件计算(见下文) 我正在使用我的 R 代码获取我所谓的 GG (请参阅下面的代码)?

详情:

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

比如G1=1,G2=2,我想根据"type"显示的是以下之一:

注意:在上图中,我匆忙使用了"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 )

使用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)

注:乘号(×)的ASCII码为Alt 0215

很棒@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 )