1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【R】ヒストグラムを作る

Posted at

はじめに

これまでに散布図箱ひげ図の作成方法をまとめてきたので、今回はその続きとしてヒストグラムの作成方法を記事にまとめてみました。

解析に使うデータ

  • ある低木の樹高(m)のデータを使って、ヒストグラムを作成します。
樹高データ
tree_height = c(0.89,3.35,1.97,1.09,1.34,1.23,2.6,2.66,1.28,3.48,1.53,0.88,4.17,4.13,1.06,3.03,0.57,1.29,1.91,5.53,1.59,4.06,4.67,3.2,5.22,4.28,3.42,4.68,0.99,0.89,2.86,1.68,1.02,4.98,3.02,1.16,1.91,2.75,4.56,1.98,4.21,5.18,1.71,2.15,5.27,2.81,2.68,0.87,0.34,1.05,1.23,3.74,1.25,1.35,2.61,1.97,2.42,1.12,1.63,1.43,1.58,0.84,0.89,2.89,3.18,4.66,1.73,1.68,0.74,0.49,5.17,0.48,1.27,1.11,0.72,3.22,0.99)

単純なヒストグラムの描画

  • hist関数の引数としてベクトルを指定するだけで、あとはRがよしなにヒストグラムを作図してくれます。
    • 横軸は樹高(m)、縦軸は各階級内での出現頻度を示しています。
単純なヒストグラムの描画
hist(tree_height)

histgram_001.png

縦軸を「確率密度」に変える

  • freq=Fというパラメーターを加えると、縦軸を確率密度に変更できます。
    • デフォルトはfreq=Tで、縦軸は出現頻度となっています。
縦軸を「確率密度」に変えたヒストグラム
hist(tree_height, freq=F)

histgram_002.png

横軸の範囲と階級幅を指定する

  • デフォルトだと横軸の範囲と階級幅(分割の幅)は「Rにお任せ」なので、これを変更したい場合はbreaksというパラメーターを使います。
  • 以下の例では、横軸の範囲を0~6、階級幅を1に設定しています。
横軸の範囲と階級幅を指定したヒストグラム
hist(tree_height, breaks=seq(0,6,1))

histgram_003.png

見た目を整える

  • 様々なパラメーターを使うことで、タイトルや縦軸と横軸のラベル、棒の色などを調整することができます。
  • densityの最小値は0(※棒は枠線のみ)ですが、最大値についてはヘルプを見てもよく分かりませんでした。
パラメーター名 意味
xlab 横軸のラベル
ylab 縦軸のラベル
main グラフのタイトル
col 棒の色
density 棒の斜線の密度
見た目を整えたヒストグラム
hist(tree_height, breaks=seq(0,6,1), xlab="Tree height (m)", ylab="Number of individuals", main="Height size class of shrub tree", col="#2ca559", density=20)

histgram_004.png

参考資料

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?