数式表記

R で描いたグラフ上に数式を挿入することができる。expression 関数を利用する。

基本数式

四則演算や等号・不等号を表す数式、数学記号など。

plot(0, 0, type = "n", xlim = c(0, 3), ylim = c(0, 9), xlab = "", ylab = "")

# 算術演算子
text(1, 8, expression(a + b), cex = 1.3)
text(1, 7, expression(a - b), cex = 1.3)
text(1, 6, expression(a * b), cex = 1.3)
text(1, 5, expression(a / b), cex = 1.3)
text(1, 4, expression(a %+-% b), cex = 1.3)
text(1, 3, expression(a %*% b), cex = 1.3)
text(1, 2, expression(a %.% b), cex = 1.3)
text(1, 1, expression(a %/% b), cex = 1.3)

# 不等号など
text(2, 8, expression(a == b), cex = 1.3)
text(2, 7, expression(a != b), cex = 1.3)
text(2, 6, expression(a < b), cex = 1.3)
text(2, 5, expression(a >= b), cex = 1.3)
text(2, 4, expression(a %~~% b), cex = 1.3)
text(2, 3, expression(a %=~% b), cex = 1.3)
text(2, 2, expression(a %==% b), cex = 1.3)
text(2, 1, expression(a %prop% b), cex = 1.3)
expressionで表示するできる算術演算子と符号など

数式

根号や理論記号などを R の expression 関数で描く例。

plot(0, 0, type = "n", xlim = c(0, 4), ylim = c(0, 9), xlab = "", ylab = "")

# 数式
text(1, 8, expression(x[i]), cex = 1.3)
text(1, 7, expression(x^2), cex = 1.3)
text(1, 6, expression(paste(x, y, z)), cex = 1.3)
text(1, 5, expression(sqrt(x)), cex = 1.3)
text(1, 4, expression(sqrt(x, 2)), cex = 1.3)
text(1, 3, expression(hat(x)), cex = 1.3)
text(1, 2, expression(tilde(x)), cex = 1.3)
text(1, 1, expression(dot(x)), cex = 1.3)

# 数学文字
text(2, 8, expression(ring(x)), cex = 1.3)
text(2, 7, expression(plain(x)), cex = 1.3)
text(2, 6, expression(bold(x)), cex = 1.3)
text(2, 5, expression(italic(x)), cex = 1.3)
text(2, 4, expression(bolditalic(x)), cex = 1.3)
text(2, 3, expression(symbol(x)), cex = 1.3)
text(2, 2, expression(list(x, y, z)), cex = 1.3)
text(2, 1, expression(cdots), cex = 1.3)

# 理論記号など
text(3, 8, expression(x %subset% y), cex = 1.3)
text(3, 7, expression(x %subseteq% y), cex = 1.3)
text(3, 6, expression(x %notsubset% y), cex = 1.3)
text(3, 5, expression(x %supset% y), cex = 1.3)
text(3, 4, expression(x %<->% y), cex = 1.3)
text(3, 3, expression(x %=>% y), cex = 1.3)
text(3, 2, expression(x %dblup% y), cex = 1.3)
text(3, 1, expression(x %dbldown% y), cex = 1.3)
Rのexpressionで平方根や理論記号などを表示させる方法

その他

積分記号や和記号などを R の expression 関数で描く例。

plot(0, 0, type = "n", xlim = c(0,4), ylim = c(0, 9), xlab = "", ylab = "")

# 積分
text(1, 8, expression(paste(
  integral(f(x), 0, infinity),
  dx == 1
)), cex = 1.5)

# 分数と極限
text(1, 6, expression(
  lim(over(sin(x), x), x %->% 0) == 1  
), cex = 1.5)

# 和記号
text(1, 4, expression(
  sum(j, j == 1, n) == over(n(n+1), 2)
), cex = 1.5)

# 積記号
text(1, 2, expression(
  prod(plain(P)(X == x), x)
), cex = 1.5)

# 複雑な数式
text(3, 8, expression(
  lim(
    sum(
      ((over(k, n))^2 %.% over(1, n)),
      k == 1,
      n
    ),
    n %->% infinity
  )
  ==
  integral(x^2, 0, 1) * dx
), cex = 1.5)


# 複雑な数式
text(3, 6, expression(
  hat(f(xi)) %==%
  integral(
    g(xi),
    - infinity,
    infinity
  )
  *
  e^(2 * pi * i * x * xi)
  *
  d * xi
), cex = 1.5)

# 複雑な数式
text(3, 4, expression(
  f(x) ==
  sum(
    over(f^(n) * (a), n)
    ,n == 0
    ,infinity
  )
  *
  (x - a)^n + R[n]
), cex = 1.5)
Rのexpressionで複雑な数式を描くサンプル