ggplot2 棒グラフ、帯グラフメモ

利用するデータセット mtcars

# http://docs.ggplot2.org/0.9.3.1/geom_bar.html
# help(mtcars) # データについてのヘルプ
data(mtcars) # データ読み込み
head(mtcars) # 先頭行の表示
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
str(mtcars) # データ構造表示
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
summary(mtcars) # データサマリ
##       mpg            cyl            disp             hp       
##  Min.   :10.4   Min.   :4.00   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.4   1st Qu.:4.00   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.2   Median :6.00   Median :196.3   Median :123.0  
##  Mean   :20.1   Mean   :6.19   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.8   3rd Qu.:8.00   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.9   Max.   :8.00   Max.   :472.0   Max.   :335.0  
##       drat            wt            qsec            vs       
##  Min.   :2.76   Min.   :1.51   Min.   :14.5   Min.   :0.000  
##  1st Qu.:3.08   1st Qu.:2.58   1st Qu.:16.9   1st Qu.:0.000  
##  Median :3.69   Median :3.33   Median :17.7   Median :0.000  
##  Mean   :3.60   Mean   :3.22   Mean   :17.8   Mean   :0.438  
##  3rd Qu.:3.92   3rd Qu.:3.61   3rd Qu.:18.9   3rd Qu.:1.000  
##  Max.   :4.93   Max.   :5.42   Max.   :22.9   Max.   :1.000  
##        am             gear           carb     
##  Min.   :0.000   Min.   :3.00   Min.   :1.00  
##  1st Qu.:0.000   1st Qu.:3.00   1st Qu.:2.00  
##  Median :0.000   Median :4.00   Median :2.00  
##  Mean   :0.406   Mean   :3.69   Mean   :2.81  
##  3rd Qu.:1.000   3rd Qu.:4.00   3rd Qu.:4.00  
##  Max.   :1.000   Max.   :5.00   Max.   :8.00
pairs(mtcars) # 散布図行列 各変量の相関をざっくり確認。

plot of chunk unnamed-chunk-1

hist(mtcars$cyl) # cyl の度数分布表 4,6,8 で分かれてる

plot of chunk unnamed-chunk-1


library("ggplot2") # ggplot2パッケージでの描画
qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(cyl))

plot of chunk unnamed-chunk-1

# 積み上げ(x軸:cyl, 縦:データ数, 色:gear)
p <- qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear))
p

plot of chunk unnamed-chunk-1

# 色変更1 ちょい見にくい。
p <- qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear)) 
p <- p + scale_fill_brewer()
p

plot of chunk unnamed-chunk-1

# 色変更1 背景変更
p <- qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear)) 
p <- p + scale_fill_brewer()
p <- p + theme_bw()
p

plot of chunk unnamed-chunk-1

# 色変更2
p <- qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear)) 
p <- p + scale_fill_grey()
p

plot of chunk unnamed-chunk-1

# gearの順序を変える
mtcars$gear <- factor(mtcars$gear,levels=c(4,3,5))
p <- qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear)) 
p <- p + scale_fill_grey()
p

plot of chunk unnamed-chunk-1

# y軸のスケールをあわせる
# ホントは縦軸をパーセントにしたかったがデータの事前整形が必要そう。
p <- qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear)) 
p <- p + facet_wrap(~cyl,scale='free')
p

plot of chunk unnamed-chunk-1