ggplot2 110129

Preview:

Citation preview

簑田 高志簑田 高志

ggplot2ggplot2パッケージ作成者:パッケージ作成者: WickhamWickhamさんの話を聞いてさんの話を聞いて

01/29/11 2

目次

1.自己紹介2.統数研でのセミナー3.今日の話の構成4. ggplot2 とはどんなパッケージ?5.インストール方法6.基本的な使い方7.具体的にやってみよう8.まとめ9.参考資料

01/29/11 3

自己紹介

名前 :簑田 高志(みのだ たかし)Twitter : aad34210ブログ : http://pracmper.blogspot.com/出身地 :熊本県出身学部 :法学部仕事 :一般ユーザー向けのWebサービスの

企 画・運営、アナリスト趣味 :テニス(踊れません・歌えません)最近はまってるもの

: MGSPW

01/29/11 4

統数研でのセミナー

01/29/11 5

統数研でのセミナー

11月 25日:統計数理研究所で海外のスピーカーを招いて、セミナーが行われました。今回は 2人の方が参加されました。

ggplot2のパッケージ開発者 Hadley Wickhamさん黄色い本を書かれた Uwe Liggesさん

概要は以下の URLに公開されています。http://jasp.ism.ac.jp/~nakanoj/workshop10/2010Rmeeting.htm

01/29/11 6

統数研でのセミナー

Wickhamさんは (当然 )ggplot2の話。WorkShop形式で話をされて、課題をしながら進めていきました。Liggesさんは CRANの運営の話しとパッケージの話。みなさんがアップロードされたパッケージのメンテナンスをしてくれています。

01/29/11 7

今日の話の構成

01/29/11 8

今日の話の構成

時間は最大 30分〜 40分ぐらい。インストール〜使い方まで。ベースは統数研のセミナーで使われた資料を元にアレンジしています。具体的にデモをやりつつ進めていきます。こんな人向けデフォルトの plotではちょっと物足りない…きれいなグラフを作りたい。

01/29/11 9

ggplot2とはどんなパッケージ?

01/29/11 10

ggplot2とはどんなパッケージ?

ggplot2とは?ggplot2 is a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts. It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics超意訳:グラフ文法をベースにした Rの描画機能。簡単にきれいでパワフルなグラフがかけまっせ!

出典: http://had.co.nz/ggplot2/

01/29/11 11

ggplot2とはどんなパッケージ?

具体的にはどんなグラフが描けるのか?

01/29/11 12

インストール方法

01/29/11 13

インストール方法

他のインストールパッケージと全く同じinstall.packages(“ggplot2”) library(ggplot2)

初めてインストールする場合は、 CRANのロケーションを選んでください。

Japan(Hyogo)Japan(Tsukuba)を選択してください。(画面は MacOS版)

01/29/11 14

基本的な使い方

01/29/11 15

基本的な使い方

ggplot2には 2つの構文があります。ggplot([data] , aes(x = X軸 , y = Y軸 )) :データ定義

+ geom_ () :◯◯◯◯ 出力するグラフ + geom_ () :◯◯◯◯ 出力するグラフ 2 + xlim(最小値 , 最大値 ) + ylim(最小値 , 最大値 ) + xlab(X軸ラベル )+ ylab(Y軸ラベル ) などなど…

例)ggplot(movies, aes(x=mpaa, y=rating)) +geom_jitter(aes(colour=rating))+xlab(“対象年令” ) + ylab(“レーティング” )

01/29/11 16

基本的な使い方

ggplot2には 2つの構文があります。qplot(x , y , data = [data] , geom = グラフ )

例)qplot(mpaa, rating, data=movies,geom=c("boxplot","jitter"))

ggplot~,qplotも構文が違うだけで同じグラフを作ってくれます。

01/29/11 17

具体的にやってみよう

01/29/11 18

具体的にやってみよう

利用するデータ: diamonds (ggplotパッケージに同梱)呼び出し方: data(diamonds)サンプルデータ(ダイアモンドのデータ)carat cut color clarity depth table price x y z1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.432 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.313 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.314 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.635 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.756 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48

01/29/11 19

具体的にやってみよう(棒グラフ)

■ 変数 cut , carat の棒グラフと、 X軸の間隔を調整してみよう。・ qplot(cut, data = diamonds)・ ggplot(diamonds , aes(cut)) + geom_bar()

・ qplot(carat, data = diamonds)・ ggplot(diamonds , aes(carat)) + geom_bar()

・ qplot(carat, data = diamonds, binwidth = 1)・ ggplot(diamonds , aes(x = carat)) + geom_bar(binwidth = 1)

・ qplot(carat, data = diamonds, binwidth = 0.1)・ ggplot(diamonds , aes(x = carat)) + geom_bar(binwidth = 0.1)

・ qplot(carat, data = diamonds, binwidth = 0.01)・ ggplot(diamonds , aes(x = carat)) + geom_bar(binwidth = 0.1)

01/29/11 20

具体的にやってみよう(棒グラフ・六角形)

■ 変数 depth を Cutごとでみてみよう・ qplot(depth, data = diamonds, binwidth = 0.2, fill = cut) + xlim(55, 70)・ ggplot(diamonds , aes(depth , fill = cut)) + geom_bar(binwidth = 0.2) + xlim (55 , 70)

■ 変数 depth を Cutごとでみてみよう・ qplot(depth, data = diamonds, binwidth = 0.2) + xlim(55, 70) + facet_wrap(~ cut)・ ggplot(diamonds , aes(depth)) + geom_bar(binwidth = 0.2) + xlim(55 , 70) + facet_wrap(~cut)

■ もうちょっとかっこ良く (hexbinパッケージが必要かも )六角形・ qplot(log10(carat), log10(price), data = diamonds,geom = "hex", bins = 10) + facet_wrap(~ cut)

01/29/11 21

具体的にやってみよう(散布図・平均直線・回帰直線)

■ 変数 price と carat の関係をみてみたい。(散布図)・ qplot(price, carat, data = diamonds, colour = I("blue"))・ ggplot(diamonds , aes(x = price , y = carat)) + geom_jitter(colour = I("blue") )

■ ラインを引いてみよう- 条件つき平均・ ggplot(diamonds , aes(price , carat)) + geom_jitter(colour = I("blue") ) + geom_smooth(colour = "red") - 回帰直線・ ggplot(diamonds , aes(price , carat)) + geom_jitter() + geom_smooth(method = "lm")このラインの詳細はこちらhttp://had.co.nz/ggplot2/geom_smooth.html

01/29/11 22

具体的にやってみよう(透過グラフ)

■ 変数 price と carat の関係の強い所だけ見てみたい。 (グラフを透過的にしてみる)・ qplot(price, carat, data = diamonds, alpha = I(1/10))・ qplot(price, carat, data = diamonds, alpha = I(1/50))・ qplot(price, carat, data = diamonds, alpha = I(1/100))・ qplot(price, carat, data = diamonds, alpha = I(1/250))

01/29/11 23

具体的にやってみよう(箱ヒゲ図)

■ 最後に Boxplotとの重ね書き(箱ヒゲ図)※ データを切り替えます: movie・ ggplot(movies , aes(x = mpaa , y = rating)) + geom_jitter()・ ggplot(movies , aes(x = mpaa , y = rating)) + geom_jitter(aes(colour = rating))・ ggplot(movies , aes(x = mpaa , y = rating)) + geom_jitter(aes(colour = rating)) + geom_boxplot()

01/29/11 24

まとめ

01/29/11 25

まとめ

➔ggplotの構文➔ ggplot(data , aes(x軸 , y軸 )) + geom_(グラフ種類 )~~ ➔ qplot(x軸 , y軸 , data , geom = (グラフ種類 ))~~

➔様々なオプション➔ Xlab , ylab , xlim , ylim, logなど…

➔グラフの重ね書き➔ ggplotの場合: ~+ geom_jitter() + geom_boxplot()のように連結していけばよい。

➔ qplot の場合: geom = c(“jitter” , “boxplot”) のように c()で追加

01/29/11 26

参考文献

01/29/11 27

今日の話の構成

ggplot2マニュアルhttp://had.co.nz/ggplot2/利用する時にとても参考になります。

統数研セミナー概要http://jasp.ism.ac.jp/~nakanoj/workshop10/2010Rmeeting.htm

セミナー資料http://had.co.nz/courses/10-tokyo/

SlideShare(Hadly Wickham)http://www.slideshare.net/hadley/presentations

01/29/11 28

ご清聴ありがとうございました

m(__)m