ヒストグラム

ggplot2 で始めるデータ可視化入門

Rick Scavetta

Founder, Scavetta Academy

一般的なプロット種別

プロット種別 可能なGeom
散布図 points, jitter, abline, smooth, count
棒グラフ histogram, bar, col, errorbar
折れ線グラフ line, path
ggplot2 で始めるデータ可視化入門

ヒストグラム

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram()
  • 値をビン分割したプロット
    • すなわち統計関数
`stat_bin()` using `bins = 30`.
Pick better value with `binwidth`.

ggplot2 で始めるデータ可視化入門

既定は等間隔30ビン

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram()
  • 値をビン分割したプロット
    • すなわち統計関数
# 既定のビン幅:
diff(range(iris$Sepal.Width))/30
[1] 0.08

ggplot2 で始めるデータ可視化入門

直感的で意味のあるビン幅

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.1)
  • データに合う意味のあるビン幅を設定。

  • 棒の間に空白なし。

ggplot2 で始めるデータ可視化入門

目盛り位置の再配置

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.1,
                 center = 0.05)
  • データに合う意味のあるビン幅を設定。

  • 棒の間に空白なし。

  • X軸ラベルは棒の間に配置。

ggplot2 で始めるデータ可視化入門

種別の比較

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05)

ggplot2 で始めるデータ可視化入門

デフォルト位置は「stack」

ggplot(iris, aes(x = Sepal.Width,
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05,
                 position = "stack") 

ggplot2 で始めるデータ可視化入門

position = "dodge"

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05, 
                 position = "dodge")

ggplot2 で始めるデータ可視化入門

position = "fill"

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05, 
                 position = "fill")  

ggplot2 で始めるデータ可視化入門

最終スライド

ggplot2 で始めるデータ可視化入門

Preparing Video For Download...