続・グラフ(実践編)
(1)データフレームの作成(サイコロの総和サンプル)
# サイコロ
dice = 1:6
# 10回サイコロを振って総和を求める。
# 3つのモードを作る
## mode1(ランダム)、mode2(1が多く出る)、## mode3(偶数が多く出る)
roll.dice = function(m){
s = 0
if(m==1){
s = sum(sample(dice,10,T))
}
if(m==2){
s = sum(sample(dice,10,T,prob=c(0.5,0.1,0.1,0.1,0.1,0.1)))
}
if(m==3){
s = sum(sample(dice,10,T,prob=c(0.01,0.3,0.01,0.3,0.01,0.3)))
}
s
}
# モード変数
mode0 = sample(1:3,1000,T)
mode0
#[1] 1 1 2 1 3 2 3 1 3 1 2 1 1 1 1 2 1 1 3 3 2 2 2 2 1 2 2 2 3 2
#[31] 2 2 1 1 1 2 2 1 1 1 3 3 1 3 3 1 3 3 1 2 1 2 1 1 1 3 1 3 3 3
# 総和変数
sum0 = vector("integer",1000)
for(i in 1:1000){
sum0[i] = roll.dice(mode0[i])
}
#[1] 22 38 29 28 40 19 33 36 45 32 20 33 33 36 43 25 31
#[18] 30 42 38 26 21 23 18 33 22 30 15 35 23 23 23 47 31
sum0
# 奇数か偶数か
evenodd0 = sum0 %% 2
#[1] 0 0 1 0 0 1 1 0 1 0 0 1 1 0 1 1 1 0 0 0 0 1 1 0 1 0
#[27] 0 1 1 1 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 1 0 1 0 0 0
# 回数
order0 = 1:1000
# 以上をデータフレーム化(1000 x 4)します。
dsample = data.frame(order=order0,sum=sum0,
evenodd=evenodd0,mode=mode0)
head(dsample)
#. order sum evenodd mode
#1 1 22 0 1
#2 2 38 0 1
#3 3 29 1 2
#4 4 28 0 1
#5 5 40 0 3
#6 6 19 1 2
(2)折れ線グラフ・散布図による可視化
# 授業の視認性を高めるため、
# 全てのグラフで以下を追加し、
# 軸の文字サイズを上げます
ex = theme(
title = element_text(size = 30),
legend.title = element_text(size = 30),
axis.title.x = element_text(size = 30),
axis.title.y = element_text(size = 30),
axis.text.x = element_text(size = 30),
axis.text.y = element_text(size = 30),
legend.text = element_text(size=15)
)
# 折れ線グラフ・散布図
## xを順番、yに総和をマッピング
gp = ggplot(dsample,aes(x=order,y=sum))
gp = gp + geom_line();gp+ex
## 散布図(xの範囲を最初の200とする)
gp = ggplot(dsample,aes(x=order,y=sum))
gp = gp + geom_point(size=10)
gp = gp + scale_x_continuous(limits=c(0,200));gp+ex
## 散布図(塗りにmodeをマッピング)
## shape=21:塗りのあるshapeを選ぶ
gp = ggplot(dsample,aes(x=order,y=sum,fill=mode))
gp = gp + geom_point(size=8,shape=21)
gp = gp + scale_x_continuous(limits=c(0,200));gp+ex
### modeが数値のため、判例はカラーバーとなっていることに注意
## modeをファクタとする
gp = ggplot(dsample,aes(x=order,y=sum,fill=factor(mode)))
gp = gp + geom_point(size=8,shape=21)
gp = gp + scale_x_continuous(limits=c(0,200));gp+ex
### 凡例が離散表記となります。
## 範囲を全体として、凡例のタイトルを変更
gp = ggplot(dsample,aes(x=order,y=sum,fill=factor(mode)))
gp = gp + geom_point(size=6,shape=21)
gp = gp + scale_fill_discrete(
name = "Mode",labels=c("Normal","Bias-1","Bias-246"));gp+ex
(3)棒グラフ(数え上げ)による可視化
## 出現回数を比較します。
## ランダムにモード間を遷移するため、差はあまり無いはずです。
gp = ggplot(dsample,aes(x=factor(mode),fill=factor(mode)))
gp = gp + geom_bar()
gp = gp + scale_x_discrete(
name="Mode",labels=c("Normal","Bias-1","Bias-246"))
gp = gp + scale_fill_discrete(guide=FALSE);gp+ex
### 凡例はX軸と同じなので消します(最後の行)
## 出現回数を奇数と偶数別に比較します。
## mode=3のとき、偶数の比率が格段に上がるはずです。
## position="fill"で100%積み上げグラフを指定します。
gp = ggplot(dsample,aes(x=factor(mode),fill=factor(evenodd)))
gp = gp + geom_bar(position = position_fill())
gp = gp + scale_x_discrete(
name="Mode",labels=c("Normal","Bias-1","Bias-246"))
gp = gp + scale_fill_discrete(
name="Characteristics",labels=c("Even","Odd"));gp+ex
## 横並び方式はこちら(position_dodge())
gp = ggplot(dsample,aes(x=factor(mode),fill=factor(evenodd)))
gp = gp + geom_bar(position = position_dodge())
gp = gp + scale_x_discrete(
name="Mode",labels=c("Normal","Bias-1","Bias-246"))
gp = gp + scale_fill_discrete(
name="Characteristics",labels=c("Even","Odd"));gp+ex
(4)ボックスプロットによる可視化
## ModeをX軸として、10の総和を比較します
## Bias1が低い平均値を、Bias246が高い平均値をとるはずです。
gp = ggplot(dsample,aes(x=factor(mode),y=sum,fill=factor(mode)))
gp = gp + geom_boxplot()
gp = gp + scale_x_discrete(
name="Mode", labels=c("Normal","Bias1","Bias246"))
gp = gp + scale_y_continuous(name="Sum of 10 Dice Rolls")
gp = gp + scale_fill_discrete(guide=FALSE);gp+ex
## ドットプロットを積み重ねます。
gp + geom_dotplot(binaxis="y", binwidth=0.3, stackdir="center") + ex
(5)ヒストグラムによる可視化
library(gcookbook)
## モードの違いを無視して、総和のヒストグラムを表示
gp = ggplot(dsample,aes(x=sum))
gp = gp + geom_histogram(binwidth=1)
gp = gp + scale_x_continuous(name="Sum of 10 Dice Rolls");gp+ex
## モードの違いで色を分ける(デフォルト:積み上げ方式)
gp = ggplot(dsample,aes(x=sum,fill=factor(mode)))
gp = gp + geom_histogram()
gp = gp + scale_x_continuous(name="Sum of 10 Dice Rolls")
gp = gp + scale_fill_discrete(
name="Mode",labels=c("Normal","Bias-1","Bias-246"));gp+ex
## モードの違いで色を分ける(横並び方式)
gp = ggplot(dsample,aes(x=sum,fill=factor(mode)))
gp = gp + geom_histogram(position = position_dodge())
gp = gp + scale_x_continuous(name="Sum of 10 Dice Rolls")
gp = gp + scale_fill_discrete(
name="Mode",labels=c("Normal","Bias-1","Bias-246"));gp+ex
## モードの違いで色を分ける(半透明で重ねる)
gp = ggplot(dsample,aes(x=sum,fill=factor(mode)))
gp = gp + geom_histogram(position = position_identity(),alpha=0.5)
gp = gp + scale_x_continuous(name="Sum of 10 Dice Rolls")
gp = gp + scale_fill_discrete(
name="Mode",labels=c("Normal","Bias-1","Bias-246"));gp+ex
## ファセットを使う方法
gp = ggplot(dsample,aes(x=sum,fill=factor(mode)))
gp = gp + geom_histogram()
gp = gp + facet_grid(mode ~ .)
gp = gp + theme(strip.text = element_text(size=20)) #ファセットのラベル
gp = gp + scale_fill_discrete(guide=FALSE);gp+ex
## ファセットラベルを変更するためには、
## fillで指定する変数の値から修正する必要があります。
# install.packages(dplyr)
library(dplyr)
# dplyrライブラリのrecode関数を使うと、
# 文字列を異なる文字列に一括に変換してくれます。
dsample$mode2 =
recode(as.character(dsample$mode),
"1"="Normal","2"="Bias-1","3"="Bias-246")
as.character(dsample$mode)
#[1] "1" "1" "2" "1" "3" "2" "3" "1" "3" "1" "2" ...
dsample$mode2
#[1] Normal Normal Bias-1 Normal Bias-246 Bias-1 ...
## 新たにmode2をfacetの分割にマッピングします
gp = ggplot(dsample,aes(x=sum,fill=mode2))
gp = gp + geom_histogram()
gp = gp + facet_grid(mode2 ~ .)
gp = gp + theme(strip.text = element_text(size=20))
gp = gp + scale_fill_discrete(guide=FALSE);gp+ex