Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to ggplot2
Search
nonki1974
April 06, 2019
Technology
540
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Introduction to ggplot2
nonki1974
April 06, 2019
More Decks by nonki1974
See All by nonki1974
GTFS with Tidytransit package
nonki1974
0
340
TokyoR#84_Rexams
nonki1974
0
230
都道府県別焼き鳥屋ランキングの作成
nonki1974
1
930
Introduction to R
nonki1974
0
390
Introduction to dplyr
nonki1974
0
560
Analyzing PSB tracks with R
nonki1974
0
620
introduction to fukuoka.R @ Fukuoka.LT
nonki1974
0
81
所要時間のヒートマップを作成する
nonki1974
0
600
gtfsr package @ fukuoka.R #11
nonki1974
0
360
Other Decks in Technology
See All in Technology
“それは自分の仕事じゃない"を越えて行け
yuukiyo
1
520
書籍セキュアAPIについて
riiimparm
0
250
ダッシュボード"開発"について 〜使われるダッシュボードのつくりかた〜
kimichan
0
200
2年前に削除したPHPクラスが、 ある日突然決済をエラーにした
ykagano
1
790
インシデント事例と パッケージの全量解析に学ぶ ソフトウェアサプライチェーンの守り方 / supply-chain-attack-defense
flatt_security
0
900
穢れた技術選定について
watany
19
6.2k
全社でのソフトウェアサプライチェーン攻撃対策をやってみた with Takumi Guard
z63d
0
280
ここは地獄!つらい朝会を体験することで、チームとしてのより良い振る舞いに気づくワークショップ / The stand-up meeting from hell in the game industry
scrummasudar
0
230
アップデートで何が変わった?デモで学んで使いこなすIBM Bob2.0
muehara
0
230
AIツールを導入しても生産性はあがらない? カオナビが直面した 3つの壁と乗り越え方。/ Overcoming 3 Barriers to AI-Driven Productivity at kaonavi
kaonavi
0
190
20260720_クラウド女子会×PyLadiesTokyoコラボ Amazon Bedrock ハンズオン用資料
yuuka51
1
110
非定型なドキュメントを効率よくリファクタする 〜えぇ!?仕様書27本の移行が1日で終わったって!?〜
subroh0508
2
620
Featured
See All Featured
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
310
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
880
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
610
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.7k
The Spectacular Lies of Maps
axbom
PRO
1
870
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
740
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Optimizing for Happiness
mojombo
378
71k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Transcript
Introduction to ggplot2 fukuoka.R #13 @nonki1974 April 7, 2019
ggplot2 package → Wilkinson(2005) による “Grammar of Graphics” の R
への実 装 → データの可視化のための一貫した文法と洗練された出力を提 供 → 開発者は dplyr と同じ Hadley Wickham 2
インストールとロードについて → tidyverse に含まれているため,tidyverse のインストー ルとロードができていれば OK → 個別にインストールする場合は以下の通り install.packages("ggplot2")
library(ggplot2) 3
本資料におけるバージョン packageVersion("ggplot2") ## [1] '3.1.0' 4
利用するデータ → ggplot2 に含まれる mpg データ → アメリカの環境保護局がまとめた 1999 年と
2008 年に発売 された新車の燃費データ 5
mpg データ mpg[1:6, 2:8] %>% knitr ::kable(booktabs = TRUE) model
displ year cyl trans drv cty a4 1.8 1999 4 auto(l5) f 18 a4 1.8 1999 4 manual(m5) f 21 a4 2.0 2008 4 manual(m6) f 20 a4 2.0 2008 4 auto(av) f 21 a4 2.8 1999 6 auto(l5) f 16 a4 2.8 1999 6 manual(m5) f 18 6
ggplot2 の基本 グラフ作成テンプレート ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
→ ggplot() 関数でデータフレームを指定:座標平面が作成さ れる → GEOM_FUNCTION でプロットのレイヤーを追加 → MAPPINGS でプロットの要素とデータの対応を記述 7
散布図の例 ggplot(data = mpg) + geom_point(mapping = aes(x = cty,
y = hwy)) 20 30 40 10 15 20 25 30 35 cty hwy 8
他の記述法 # 引数名の省略 ggplot(mpg) + geom_point(aes(cty, hwy)) # aes の指定場所
ggplot(mpg, aes(cty, hwy)) + geom_point() → aes() で指定した対応関係は,以降のレイヤーに引き継がれ る 9
プロット要素とデータの対応の記述 → aesthetic mapping p <- ggplot(mpg, aes(cty, hwy)) p
+ geom_point(aes(color = class)) 20 30 40 10 15 20 25 30 35 cty hwy class 2seater compact midsize minivan pickup subcompact suv 10
プロット要素とデータの対応の記述 p + geom_point(aes(size = displ)) 20 30 40 10
15 20 25 30 35 cty hwy displ 2 3 4 5 6 7 11
1 変数に対するプロット 12
量的変数の場合(ヒストグラム) p <- ggplot(mpg) p + geom_histogram(aes(hwy)) ## `stat_bin()` using
`bins = 30`. Pick ## better value with `binwidth`. 0 10 20 30 40 10 20 30 40 hwy count 13
色の指定 p + geom_histogram(aes(hwy), binwidth = 2, fill = "cadetblue",
color = "black") 0 10 20 30 40 10 20 30 40 hwy count 14
質的変数の場合(棒グラフ) p + geom_bar(aes(class)) 0 20 40 60 2seater compact
midsize minivan pickup subcompact suv class count 15
降順にソート forcats ::fct_infreq():因子型の水準の順序を値の出現 頻度順に並べ替える p + geom_bar(aes(fct_infreq(class))) + xlab("class") 0
20 40 60 suv compact midsize subcompact pickup minivan 2seater class count 16
帯グラフ p + geom_bar(aes(fct_infreq(class), fill = fl), position = "fill")
+ xlab("class") 0.00 0.25 0.50 0.75 1.00 suv compact midsizesubcompact pickup minivan 2seater class count fl c d e p r 17
集合縦棒グラフ p + geom_bar(aes(fct_infreq(class), fill = fl), position = "dodge")
+ xlab("class") 0 10 20 30 40 50 suv compact midsize subcompact pickup minivan 2seater class count fl c d e p r 18
2 変数に対するプロット 19
x: 量的変数,y: 量的変数の場合(散布図) p + geom_point(aes(cty, hwy)) + geom_smooth(method =
"lm") 20 30 40 50 10 15 20 25 30 35 cty hwy 20
重複する点の処理(点のサイズに反映) mpg %>% group_by(cty, hwy) %>% mutate(n = n()) %>%
ggplot(aes(cty, hwy, size = n)) + geom_point() + geom_smooth(method = "lm") 20 30 40 50 10 15 20 25 30 35 cty hwy n 5 10 21
重複する点の処理(jitter 処理) p + geom_point(aes(cty, hwy), position = "jitter") +
geom_smooth(method = "lm") 10 20 30 40 50 10 20 30 cty hwy 22
x: 質的変数,y: 量的変数の場合(箱ひげ図) p + geom_boxplot(aes(class, hwy)) 20 30 40
2seater compact midsize minivan pickupsubcompact suv class hwy 23
中央値の降順で因子水準をソート p + geom_boxplot(aes( fct_reorder(class, hwy, median, .desc = TRUE),
hwy)) + xlab("class") 20 30 40 compact midsize subcompact 2seater minivan suv pickup class hwy 24
質的変数による プロット分割 (Facetting) 25
質的変数によるプロット分割 p <- ggplot(mpg, aes(cty, hwy)) p + geom_point() +
facet_grid(. ~ fl) c d e p r 101520253035 101520253035 101520253035 101520253035 101520253035 20 30 40 cty hwy 26
質的変数によるプロット分割 (2 変数で) p + geom_point() + facet_grid(year ~ fl)
c d e p r 1999 2008 101520253035 101520253035 101520253035 101520253035 101520253035 20 30 40 20 30 40 cty hwy 27
集計表に対する棒グラフ 28
集計表に対する棒グラフ df <- data.frame(btype = c("A", "B", "O", "AB"), female
= c(60, 40, 30, 15), male = c(40, 30, 20, 10)) btype female male A 60 40 B 40 30 O 30 20 AB 15 10 29
wide -> long 形式への変換 tidyr ::gather() を使う df.long <- gather(df,
key = gender, value = value, male, female) df.long ## btype gender value ## 1 A male 40 ## 2 B male 30 ## 3 O male 20 ## 4 AB male 10 ## 5 A female 60 ## 6 B female 40 ## 7 O female 30 ## 8 AB female 15 30
棒グラフの作成 stat = "identity" を指定 ggplot(df.long, aes(fct_reorder(btype, value, sum, .desc
= TRUE), val fill = gender)) + xlab("btype") + geom_bar(stat = "identity", position = "dodge") 0 20 40 60 A B O AB btype value gender female male 31
外見の調整 32
テーマの設定 ggplot(mpg, aes(displ, cty)) + geom_point() + theme_minimal(base_size = 18)
10 15 20 25 30 35 2 3 4 5 6 7 displ cty 33
タイトル・軸ラベルの設定 ggplot(mpg, aes(displ, cty)) + geom_point() + theme_minimal(base_size = 18)
+ labs(title = " 排気量と燃費の関係", x = " 排気量 [l]", y = " 市街地燃費 [mpg]") 10 15 20 25 30 35 2 3 4 5 6 7 排気量[l] 市街地燃費[mpg] 排気量と燃費の関係 34
GGally パッケージの利用 35
インストールとパッケージの読み込み install.packages("GGally") library(GGally) ## ## Attaching package: 'GGally' ## The
following object is masked from 'package:dplyr': ## ## nasa 36
平行座標プロット ggparcoord(mpg, columns = c(4, 3, 8, 9), groupColumn =
"class") + theme_minimal(base_size = 18) -2 0 2 4 year displ cty hwy variable value class 2seater compact midsize minivan pickup subcompact suv 37
相関行列の可視化(データの読み込み) nba <- read.csv( "http: //datasets.flowingdata.com/ppg2008.csv") 38
相関行列の可視化 ggcorr(nba[,-1]) G MIN PTS FGM FGA FGP FTM FTA
FTP X3PM X3PA X3PP ORB DRB TRB AST STL BLK TO PF -1.0 -0.5 0.0 0.5 1.0 39
相関行列の可視化(オプション設定) ggcorr(nba[,-1], label = TRUE, label_size = 1, label_round =
2, label_alpha = TRUE) 0.19 0.06 0.04 -0.06 0.18 -0.01 0.01 0.04 0.14 0.11 0.12 0.05 0.12 0.1 0.14 -0.03 0.13 -0.05 -0.03 0.4 0.3 0.41 -0.22 0.27 0.18 0.22 0.13 0.13 0.11 -0.07 0.05 0.01 0.28 0.33 -0.07 0.32 -0.39 0.85 0.83 0.07 0.67 0.61 0.03 0.03 0.04 0.01 0.01 0.25 0.17 0.22 0.36 0.24 0.34 -0.15 0.87 0.27 0.28 0.29 -0.13 -0.23 -0.22 -0.09 0.23 0.38 0.34 0.11 0.22 0.33 0.14 -0.12 -0.23 0.25 0.17 0.11 0.1 0.14 -0.02 -0.1 0.08 0.02 0.17 0.33 0 0.16 -0.24 0.07 0.25 -0.52 -0.64 -0.67 -0.16 0.66 0.61 0.65 -0.16 -0.23 0.68 -0.02 0.26 0.95 -0.03 -0.14 -0.13 -0.02 0.01 0.24 0.17 0.23 0.3 0.23 0.54 0.03 -0.33 -0.29 -0.27 -0.21 0.21 0.4 0.34 0.16 0.26 0.42 0.52 0.12 0.53 0.5 0.6 -0.62 -0.53 -0.58 0.2 0.07 -0.54 -0.02 -0.25 0.99 0.31 -0.64 -0.59 -0.63 0.09 0.16 -0.49 -0.11 -0.25 0.28 -0.65 -0.61 -0.65 0.16 0.24 -0.52 -0.06 -0.27 -0.31 -0.25 -0.28 0.04 -0.04 -0.26 0.1 -0.07 0.85 0.93 -0.46 -0.33 0.74 -0.13 0.41 0.98 -0.4 -0.27 0.77 0.03 0.43 -0.43 -0.3 0.79 -0.03 0.43 0.62 -0.32 0.5 -0.43 -0.26 0.45 -0.19 0.12 0.37 0.07 G MIN PTS FGM FGA FGP FTM FTA FTP X3PM X3PA X3PP ORB DRB TRB AST STL BLK TO PF -1.0 -0.5 0.0 0.5 1.0 40