15. data.frame
• 長さのそろった値のリスト
• 列には名前と型(数値、文字など)がある
15
head(mpg)
#> manufacturer model displ year cyl trans drv cty hwy fl class
#> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
#> 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
#> 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
#> 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
#> 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
16. 値のリスト → data.frame
• data.frame()という関数でつくる
• 引数は「列名=配列」の形式
16
data.frame(
x = 1:10,
y = 2^(1:10),
z = paste("day", 1:10)
)
#> x y z
#> 1 1 2 day 1
#> 2 2 4 day 2
#> 3 3 8 day 3
...
17. CSV → data.frame
• read.csv()で読み込む
17
read.csv("ファイル名", stringsAsFactors = FALSE)
#> x y z
#> 1 1 2 day 1
#> 2 2 4 day 2
#> 3 3 8 day 3
...
これはおまじないみたいなものだ
と思ってとりあえず気にしない!
20. aesのイメージ
20
#> manufacturer model displ year cyl trans drv cty hwy fl class
#> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
#> 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
#> 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
#> 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
#> 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
x y colour
x y colour
1 1.8 29 colour1
2 1.8 29 colour1
3 2.0 31 colour1
4 2.0 30 colour1
5 2.8 26 colour1
6 2.8 26 colour1
新しいデータセット
を生成
21. aesのパラメータの例
変数名 意味
x X軸
y Y軸
colour/color 線や点の色
fill 塗りの色
alpha 透明度
size 点のサイズ
21
Hadleyはイギリス英語至上主義!
38. 参考:statとgeomの関係
• 作者の「わかりにくくてごめん!」という懺悔:
Unfortunately, due to any early design
mistake I called these either stat_() or
geom_(). A better decision would have
been to call them layer_() functions:
that's a more accurate description because
every layer involves a a stat and a geom.
(出典:https://github.com/hadley/ggplot2/blob/master/vignettes/extending-
ggplot2.Rmd)
38