Rのグラフには、凡例やエラーバー・文字による説明があるとわかりやすくなります。グラフを仕上げるためのテクニックをまとめます。凡例・テキストや数式による説明 編。
凡例を書き加える
凡例は、legend関数で付け加えます。凡例の表示位置やマーク・枠の有無などを設定できます。
主な制御パラメータ
| パラメータ | 機能 | 例 |
|---|---|---|
| 場所 | 凡例の表示位置を指定 | “topright”, x=”left”, y=”center” |
| legend | 凡例の内容 | legend=c(“sin”, “cos”), levels(factor) |
| pch/lty | 凡例に付与するマーク | pch=as.integer(iris$Species), lty=1:2 |
R> plot(Sepal.Length ~ Sepal.Width,
data=iris, pch=as.integer(iris$Species))
#pchに対して、系列毎に異なるマークを指定
R> legend("topright", legend=levels(iris$Species), pch = 1:3)

R> plot(sin, type="l", lty=1,
xlim=c(-2, 2), ylim=c(-2, 3), ylab="")
R> par(new=T)
R> plot(cos, type="l", lty=2,
xlim=c(-2, 2), ylim=c(-2, 3), ylab="sin & cos")
# 「右下・枠なし」の凡例
R> legend("bottomright",
lty=1:2, legend=c("sin", "cos"), bty="n")
# 「右・枠あり」の凡例
R> legend(x="right", y="center",
lty=1:2, legend=c("sin", "cos"))
# 「中央・枠あり」の凡例
R> legend("center",
lty=1:2, legend=c("SIN", "COS"))

グラフに文字を書く
グラフに文字を書くには、text関数にて座標指定します。数式を書く場合は、expression関数を使います。expressionの表現内容については、Rのヘルプ機能で?plotmathとすると確認できます。
R> plot(1) R> text(1, 1.2, "hello world!") R> text(0.8, 1, expression(sum(x[i], i=1, n))) R> text(1.2, 1, expression(integral(f(x) * dx, a, b)))

実験データの解析や論文用グラフ作成のヒントになりそうな、プログラミング・統計処理の参考になる書籍をまとめて紹介しています。是非、参考にしてみて下さいね。
![バイオインフォ 道場 [bioinfo-Dojo]](https://bioinfo-dojo.net/wp-content/uploads/2016/03/some_object_luca-bravo-alS7ewQ41M8-unsplash.jpg)



