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

标签 r plot plotmath

我想知道如何实际显示一个条件计算(见下文) 我正在使用它来获得我在 R 代码中调用的 GG(见代码下面)?

详细信息:

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

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

enter image description here

注意:在上图中,我匆忙使用了“X 2”,但我需要一个实际的类似 x 的数学符号作为乘号

下面是我的 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)

enter image description here

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

关于r - 在动态变化的图中显示分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42566213/

相关文章:

r - 如何使用 R 和 ggplot2 使 annotation_custom() grob 与 scale_y_reverse() 一起显示?

r - R中带有误差线的分组条形图

旋转闭合曲线的 MATLAB 曲面

python - 将 2 个不同年份的 .resample(D).size() 绘制到一张图表中?

python - 使用 Panda 从 CSV 文件中在 python 中绘制两列以上

r - 在 R 中绘制包含 "approximately equal to"的文本

r - 绘图()和do.call(): How to pass expressions to plot title when '...' is used otherwise?

r - 内存使用量一直增长直到崩溃

r - 如何将统计测试的结果作为绘图数学表达式包含在 ggplot2 facet 中

r - 在数据框上应用 sqrt 函数