ヒストグラム

ggplot2 を使ったデータ可視化入門

Rick Scavetta

Founder, Scavetta Academy

主なグラフの種類

グラフの種類 使用できる幾何要素
散布図 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()
  • データをビンごとにまとめて表示したグラフ
    • つまり、統計処理を伴うグラフ
# Default bin width:
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...