Bus errorとSegmentation faultに困ったら見るブログ

物理の研究者による日々の研究生活のメモ書きです ( python/emacs/html/Japascript/シェルスクリプト/TeX/Mac/C言語/Linux/git/tmux/R/ポケモンGO)

【R】ヒストグラム関連メモ

ここがわかりやすかった
といってもヒストグラムを1から書いたわけではなくて、どういうオプションが使えるのかをちょっと知りたかっただけ
data-science.gr.jp

グラフの中に最尤推定法で得られたパラメーターを書き込みたい

Rの数式描画の中で変数を展開したいときはbquoteを使う

hist(dat, freq = FALSE, ylim = c(0, max(max(h$density),max(y))), xlab = "z", ylab = "f(z)",
main = paste("Density Plot\nmu = ", round(a[1], 3), "\ntheta = ", round(a[2], 3), "\ngamma = ", round(a[3], 3) ), cex.main=1 )
cex.mainでmainの文字サイズを指定できる

pasteで文字列と変数を繋げた文字列を作ることができる
これで、グラフの中に最尤推定法で得られたパラメーターを書き込める

pasteでつなげたときに間に空白を入れたくないときは

paste("AAA", "BBB", sep="")

round(x, 2)
は桁数を短くするための関数
これで出力する小数点以下の数を2桁にできる






(2019/08/02 追記)

とりあえずRで困ったらhelp(hist)って打てば大体わかるから

ヒストグラムのbin数を変えたくてあれこれ調べた
ビン数を30にしたかったら、break=30

y軸をログにするには、result = hist()で結果を一度格納してから、そのresultのlogを取るしかないっぽい


↓もっと詳細なヘルプは実際にRで見てください

hist package:graphics R Documentation

Histograms

Description:

The generic function ‘hist’ computes a histogram of the given
data values. If ‘plot = TRUE, the resulting object of class
‘"histogram"’ is plotted by ‘plot.histogram’, before it is
returned.

Usage:

hist(x, ...)

## Default S3 method:
hist(x, breaks = "Sturges",
freq = NULL, probability = !freq,
include.lowest = TRUE, right = TRUE,
density = NULL, angle = 45, col = NULL, border = NULL,
main = paste("Histogram of" , xname),
xlim = range(breaks), ylim = NULL,
xlab = xname, ylab,
axes = TRUE, plot = TRUE, labels = FALSE,
nclass = NULL, warn.unused = TRUE, ...)