https://atnd.org/events/54680

ライブラリの読み込み

library(ggplot2)

データの準備

仮想データの作成

商品1の月次売上データ

sales.data1 <- data.frame(
  item="item1",
  m=4:9,
  month=paste0("2014-0",c(4:9)),
  sales=c(800,1000,1800,1500,1400,2000))

sales.data1
##    item m   month sales
## 1 item1 4 2014-04   800
## 2 item1 5 2014-05  1000
## 3 item1 6 2014-06  1800
## 4 item1 7 2014-07  1500
## 5 item1 8 2014-08  1400
## 6 item1 9 2014-09  2000

商品1,2の月次売上データ

sales.data2 <- data.frame(
  item="item2",
  m=4:9,
  month=paste0("2014-0",c(4:9)),
  sales=c(700,900,1500,1700,1800,2400))
sales.data12 <- rbind(sales.data1, sales.data2)
sales.data12
##     item m   month sales
## 1  item1 4 2014-04   800
## 2  item1 5 2014-05  1000
## 3  item1 6 2014-06  1800
## 4  item1 7 2014-07  1500
## 5  item1 8 2014-08  1400
## 6  item1 9 2014-09  2000
## 7  item2 4 2014-04   700
## 8  item2 5 2014-05   900
## 9  item2 6 2014-06  1500
## 10 item2 7 2014-07  1700
## 11 item2 8 2014-08  1800
## 12 item2 9 2014-09  2400

店舗1〜3の商品3,4の日次売上データ

set.seed(10)
sales.data34 <- data.frame(
  shop=sample(1:3,100,replace=T),
  item3=rnorm(100,100,30)
  )

sales.data34$item4 <- round(0.9 * sales.data34$item3 + 
                              80 * sales.data34$shop + 
                              rnorm(100,30,15))

sales.data34$shop <- paste0("shop",sales.data34$shop)
head(sales.data34)
##    shop  item3 item4
## 1 shop2  87.98   263
## 2 shop1  89.96   187
## 3 shop2 141.04   334
## 4 shop3 164.13   405
## 5 shop1 115.17   201
## 6 shop1 123.59   186

棒グラフを描く

基本的な棒グラフ

ggplot(sales.data1, aes(x=month, y=sales)) +
  geom_bar(stat="identity")

plot of chunk unnamed-chunk-5

背景色の変更

# 背景を白にする
ggplot(sales.data1, aes(x=month, y=sales)) +
  geom_bar(stat="identity") +
  theme_bw()

plot of chunk unnamed-chunk-6

グラフタイトルとラベルの追加

## タイトルとx軸とy軸のラベルを付ける
ggplot(sales.data1, aes(x=month, y=sales)) +
  geom_bar(stat="identity") +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  theme_bw()

plot of chunk unnamed-chunk-7

日本語フォントの設定

ggplot(sales.data1, aes(x=month, y=sales)) +
  geom_bar(stat="identity") +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-8

数値フォーマットの変更

# 数値フォーマット用のライブラリ
library(scales)

ggplot(sales.data1, aes(x=month, y=sales)) +
  geom_bar(stat="identity") +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-9

棒の上に値を表示する

## 数値を出力する
ggplot(sales.data1, aes(x=month, y=sales)) +
  geom_bar(stat="identity") +
  geom_text(aes(label=sales), vjust=-0.2) +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-10

## 数値のフォーマットを3ケタ区切りにする
ggplot(sales.data1, aes(x=month, y=sales)) +
  geom_bar(stat="identity") +
  geom_text(aes(label=comma(sales)), vjust=-0.2) +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-11

積み上げ棒グラフ

# 積み上げ棒グラフ
ggplot(sales.data12, aes(x=month, y=sales, fill=item)) +
  geom_bar(stat="identity") +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-12

色セットの変更

library(RColorBrewer)
rownames(brewer.pal.info)
##  [1] "BrBG"     "PiYG"     "PRGn"     "PuOr"     "RdBu"     "RdGy"    
##  [7] "RdYlBu"   "RdYlGn"   "Spectral" "Accent"   "Dark2"    "Paired"  
## [13] "Pastel1"  "Pastel2"  "Set1"     "Set2"     "Set3"     "Blues"   
## [19] "BuGn"     "BuPu"     "GnBu"     "Greens"   "Greys"    "Oranges" 
## [25] "OrRd"     "PuBu"     "PuBuGn"   "PuRd"     "Purples"  "RdPu"    
## [31] "Reds"     "YlGn"     "YlGnBu"   "YlOrBr"   "YlOrRd"
display.brewer.all()

plot of chunk unnamed-chunk-14

ggplot(sales.data12, aes(x=month, y=sales, fill=item)) +
  geom_bar(stat="identity") +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  scale_fill_brewer(palette = "Paired") +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-15 #### 棒に枠をつける

# 積み上げ棒グラフで枠を付ける
ggplot(sales.data12, aes(x=month, y=sales, fill=item)) +
  geom_bar(stat="identity", col="black") +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  scale_fill_brewer(palette = "Paired") +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-16

100%積み上げ棒グラフ

# 100%積み上げグラフ
ggplot(sales.data12, aes(x=month, y=sales, fill=item)) +
  geom_bar(position="fill",stat="identity", col="black") +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=percent) +
  scale_fill_brewer(palette = "Paired") +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-17

棒グラフを横に並べる

## 水平に棒グラフを並べる
ggplot(sales.data12, aes(x=month, y=sales, fill=item)) +
  geom_bar(position="dodge",stat="identity",col="black") +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  scale_fill_brewer(palette = "Paired") +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-18

折れ線グラフを描く

基本的な折れ線グラフ

xが数値

# xが数値
ggplot(sales.data1, aes(x=m, y=sales)) +
  geom_line() +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-19

xがファクター

# xがファクター
ggplot(sales.data1, aes(x=month, y=sales,group=1)) +
  geom_line() +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-20

y軸の範囲の指定

ylimit <- c(0,max(sales.data1$sales))
ggplot(sales.data1, aes(x=month, y=sales,group=1)) +
  geom_line() +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma,limits=ylimit) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-21

点の追加

# 点を追加
ggplot(sales.data1, aes(x=month, y=sales,group=1)) +
  geom_line() +
  geom_point() +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma,limits=ylimit) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-22

グループ別の折れ線グラフ

# グループごとに折れ線グラフ
ylimit <- c(0,max(sales.data12$sales))
ggplot(sales.data12, aes(x=month, y=sales,group=item,col=item)) +
  geom_line() +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma,limits=ylimit) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-23

# グループごとに線の種類を変える
ggplot(sales.data12, aes(x=month, y=sales,group=item,col=item,lty=item)) +
  geom_line() +
  xlab("月") +
  ylab("売上") +
  ggtitle("売上推移") +
  scale_y_continuous(label=comma,limits=ylimit) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-24

散布図を描く

基本的な散布図

# 基本的な散布図
ggplot(sales.data34, aes(x=item3, y=item4)) +
  geom_point() +
  xlab("item3") +
  ylab("item4") +
  ggtitle("item3とitem4の散布図") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-25

点の形を変える

# 塗りつぶしなしの散布図
ggplot(sales.data34, aes(x=item3, y=item4)) +
  geom_point(shape=21) +
  xlab("item3") +
  ylab("item4") +
  ggtitle("item3とitem4の散布図") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-26

グループごとに色や形を変える

# グループごとに色と形を変える
ggplot(sales.data34, aes(x=item3, y=item4, col=shop, shape=shop)) +
  geom_point() +
  xlab("item3") +
  ylab("item4") +
  ggtitle("item3とitem4の散布図") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-27

回帰直線

# 回帰直線の追加
ggplot(sales.data34, aes(x=item3, y=item4)) +
  geom_point() +
  geom_smooth(method="lm") +
  xlab("item3") +
  ylab("item4") +
  ggtitle("item3とitem4の散布図") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-28

グループごとに回帰直線

# グループごとに回帰直線を引く
ggplot(sales.data34, aes(x=item3, y=item4, col=shop, shape=shop,group=shop)) +
  geom_point() +
  geom_smooth(method="lm") +
  xlab("item3") +
  ylab("item4") +
  ggtitle("item3とitem4の散布図") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-29

ヒストグラムを描く

基本的なヒストグラム

# 基本
ggplot(sales.data34, aes(x=item3)) +
  geom_histogram() +
  ggtitle("item3の売上分布") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-30

棒に枠をつけて、白色に

ggplot(sales.data34, aes(x=item3)) +
  geom_histogram(fill="white",col="black") +
  ggtitle("item3の売上分布") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-31

棒の幅の設定

# ビン幅を10に設定
ggplot(sales.data34, aes(x=item3)) +
  geom_histogram(fill="white",col="black",binwidth=10) +
  ggtitle("item3の売上分布") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-32

グループ別のヒストグラム

# グループごとに色を付ける
ggplot(sales.data34, aes(x=item3,fill=shop)) +
  geom_histogram(position="dodge",col="black",binwidth=10) +
  ggtitle("item3の売上分布") +
  scale_y_continuous(label=comma) +
  scale_fill_brewer(palette = "Paired") +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-33

箱ひげ図を描く

基本的な箱ひげ図

ggplot(sales.data34, aes(x=shop,y=item3)) +
  geom_boxplot() +
  ggtitle("店舗ごとのitem3の売上分布") +
  scale_y_continuous(label=comma) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-34

グループごとに箱ひげ図を描く

library(reshape2)

sales.data34.melt <- 
  melt(sales.data34,id.vars=c("shop"),value.names=c("item3","item4"))

ggplot(sales.data34.melt, aes(x=variable,y=value)) +
  geom_boxplot() +
  xlab("") +
  ylab("売上") +
  ggtitle("店舗ごとのitem3,4の売上分布") +
  scale_y_continuous(label=comma) +
  facet_grid(.~shop) +
  theme_bw(base_family="Osaka")

plot of chunk unnamed-chunk-35

参考

戦略的データマイニング (シリーズ Useful R 4) の第3章