箱ひげ図

縦箱ひげ図

小樽市の地域ごとの家庭数の箱ひげ図。

  1. #変数abcにggplotをdataデータを基にして適用
  2. abc <- ggplot()+
  3. #geom_boxplotで箱ひげ図の指定、横軸はdistrictをhouseholdsの値を参照し昇順に並び替えたもの、縦軸はH27のデータを用いる。
  4. geom_boxplot(data = data,mapping = aes(x =reorder(district,-households),y=households))
  5. #boxplot0.pngとして出力
  6. ggsave(file="boxplot0.png",plot = abc,width = 5,height = 5)

横箱ひげ図

上の箱ひげ図に横に倒したバージョン。


  1. abc <- ggplot()+
  2. geom_boxplot(data = data,mapping = aes(x =reorder(district,households),y=households))+
  3. #coord_flipを追加
  4. coord_flip()
  5. ggsave(file="boxplot1.png",plot = abc,width = 5,height = 5)

色付き+散布図箱ひげ図

上の箱ひげ図に色と散布図をつけたもの。


  1. abc <- ggplot(data = data,mapping = aes(x =reorder(district,-households),y=households))+
  2. #地方ごとに色を区別するfill=districtを追加
  3. geom_boxplot(mapping = aes(fill=district))+
  4. #geom_litter()で散布図を追加
  5. geom_jitter()
  6. ggsave(file="boxplot2.png",plot = abc,width = 5,height = 5)