可視化周辺の進化がヤヴァイ 〜2016〜

62
可視化周辺の進化がヤヴァイ 2016 〜Plotlyを中⼼として〜 Tokyo.R#55 2016-07-30 @kashitan

Upload: takashi-kitano

Post on 21-Apr-2017

3.247 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: 可視化周辺の進化がヤヴァイ 〜2016〜

可視化周辺の進化がヤヴァイ 2016

〜Plotlyを中⼼として〜

Tokyo.R#55 2016-07-30 @kashitan

Page 2: 可視化周辺の進化がヤヴァイ 〜2016〜

> summary(kashitan)

• TwitterID : @kashitan

• お仕事 : 某通信会社

2

Page 3: 可視化周辺の進化がヤヴァイ 〜2016〜

Rは可視化ツール

https://www.r-project.org

Page 4: 可視化周辺の進化がヤヴァイ 〜2016〜

可視化に関する過去の発表

2013-06-01 第31回 R勉強会@東京

2010-06-26 第6回 R勉強会@東京

2015-02-21 第46回 R勉強会@東京

Page 5: 可視化周辺の進化がヤヴァイ 〜2016〜

htmlwidgets多すぎ

Page 6: 可視化周辺の進化がヤヴァイ 〜2016〜

主要ライブラリの⽐較⼤分類 グラフ Plotly rbokeh highcharter

基本グラフ

折れ線グラフ ○ ○ ○バブルチャート ○ × ?

散布図 ○ ○ ○ヒートマップ ○ × ○

棒グラフ ○ × ○⾯グラフ ○ × ○円グラフ ○ × ○

統計グラフヒストグラム ○ ○ ○

箱ひげ図 ○ ○ ○

Page 7: 可視化周辺の進化がヤヴァイ 〜2016〜

圧倒的⼈気

Page 8: 可視化周辺の進化がヤヴァイ 〜2016〜

でも アカウント登録が 必要でしょう?

Page 9: 可視化周辺の進化がヤヴァイ 〜2016〜

オープンソース化 されました!

(アカウント不要) ※highcharts.jsは商⽤利⽤だと有償

Page 10: 可視化周辺の進化がヤヴァイ 〜2016〜

•plotlyデモ

•plotlyによるグラフの作成

•plotlyグラフの調整

Page 11: 可視化周辺の進化がヤヴァイ 〜2016〜

可視化のサンプルデータ• 東京都知事選挙候補者の中3名のTweetデータ

⼩池 百合⼦ 209,760 2,740

⿃越 俊太郎 159,788

541

増⽥寛也 6,476 265

(敬称略)

フォロワー数 ツイート数

Page 12: 可視化周辺の進化がヤヴァイ 〜2016〜

可視化のサンプルデータ• 東京都知事選挙候補者3名のTweetデータ

name id created favorite retweet⼩池百合⼦ 759032551348457472 2016-07-29 23:27:17 184 152

⼩池百合⼦ 759021573537878016 2016-07-29 22:43:39 177 178

⼩池百合⼦ 759021527157252096 2016-07-29 22:43:28 155 171

⼩池百合⼦ 759021455807873024 2016-07-29 22:43:11 144 163

⼩池百合⼦ 759021109580750849 2016-07-29 22:41:49 153 163

⼩池百合⼦ 759013802167848961 2016-07-29 22:12:46 209 194

⼩池百合⼦ 759013745624440832 2016-07-29 22:12:33 211 191

⼩池百合⼦ 759013630452965376 2016-07-29 22:12:05 225 196

⼩池百合⼦ 758959413692538881 2016-07-29 18:36:39 318 208

Page 13: 可視化周辺の進化がヤヴァイ 〜2016〜

•plotlyデモ

•plotlyによるグラフの作成

•plotlyグラフの調整

Page 14: 可視化周辺の進化がヤヴァイ 〜2016〜

デモhttps://code-estrellita.net/tokyo_election_2016.html

Page 15: 可視化周辺の進化がヤヴァイ 〜2016〜

•plotlyデモ

•plotlyによるグラフの作成

•plotlyグラフの調整

Page 16: 可視化周辺の進化がヤヴァイ 〜2016〜

折れ線グラフdf %>% mutate(date=as.Date(created)) %>% group_by(name, date) %>% summarise(n=n()) %>% plot_ly(x=date, y=n, color=name, type="scatter", mode="lines")

type = “scatter” mode = “lines”を指定

Page 17: 可視化周辺の進化がヤヴァイ 〜2016〜

折れ線グラフ

Page 18: 可視化周辺の進化がヤヴァイ 〜2016〜

バブルチャートdf %>% mutate(date=as.Date(created)) %>% group_by(name, date) %>% summarise(n=n(), retweet=sum(retweetCount)) %>% plot_ly(x=date, y=n, color=name, type="scatter", mode="markers", size=retweet)

type = “scatter” mode = “markers” sizeを指定

Page 19: 可視化周辺の進化がヤヴァイ 〜2016〜

バブルチャート

Page 20: 可視化周辺の進化がヤヴァイ 〜2016〜

散布図df %>% plot_ly(x=favoriteCount, y=retweetCount, color=name, type="scatter", mode="markers")

type = “scatter” mode = “markers”を指定

Page 21: 可視化周辺の進化がヤヴァイ 〜2016〜

散布図

Page 22: 可視化周辺の進化がヤヴァイ 〜2016〜

ヒートマップdf %>% filter(screenName == "ecoyuri") %>% mutate(date=as.Date(created), hour=hour(created)) %>% group_by(date, hour) %>% summarise(n=n()) %>% plot_ly(x=hour, y=date, z=n, type="heatmap")

type = “heatmap”を指定

Page 23: 可視化周辺の進化がヤヴァイ 〜2016〜

ヒートマップ

Page 24: 可視化周辺の進化がヤヴァイ 〜2016〜

棒グラフ(1/3)df %>% group_by(name) %>% summarise(n=n()) %>% plot_ly(x=name, y=n, type="bar")

type = “bar”を指定

Page 25: 可視化周辺の進化がヤヴァイ 〜2016〜

棒グラフ(1/3)

Page 26: 可視化周辺の進化がヤヴァイ 〜2016〜

棒グラフ(2/3)df %>% group_by(name) %>% summarise(favorite=sum(favoriteCount), retweet=sum(retweetCount)) %>% plot_ly(x=name, y=favorite, type="bar", name="favorite") %>% add_trace(x=name, y=retweet, type="bar", name="retweet")

add_trace()で系列を追加

Page 27: 可視化周辺の進化がヤヴァイ 〜2016〜

棒グラフ(2/3)

Page 28: 可視化周辺の進化がヤヴァイ 〜2016〜

棒グラフ(3/3)df %>% group_by(name) %>% summarise(favorite=sum(favoriteCount), retweet=sum(retweetCount)) %>% plot_ly(x=name, y=favorite, type="bar", name="favorite") %>% add_trace(x=name, y=retweet, type="bar", name="retweet") %>% layout(barmode="stack")

barmode=“stack”で 積み上げ棒グラフ

Page 29: 可視化周辺の進化がヤヴァイ 〜2016〜

棒グラフ(3/3)

Page 30: 可視化周辺の進化がヤヴァイ 〜2016〜

⾯グラフdf %>% mutate(date=as.Date(created)) %>% group_by(name, date) %>% summarise(n=n()) %>% plot_ly(x=date, y=n, fill="tozeroy", color=name)

fill=“tozeroy”を指定

Page 31: 可視化周辺の進化がヤヴァイ 〜2016〜

⾯グラフ

Page 32: 可視化周辺の進化がヤヴァイ 〜2016〜

円グラフ(1/2)df %>% filter(screenName == "ecoyuri") %>% summarise(favorite = sum(favoriteCount), retweet = sum(retweetCount)) %>% plot_ly(labels=colnames(.), values=c(favorite, retweet), type="pie")

labels, values, type=“pie”を指定

Page 33: 可視化周辺の進化がヤヴァイ 〜2016〜

円グラフ(1/2)

Page 34: 可視化周辺の進化がヤヴァイ 〜2016〜

円グラフ(2/2)df %>% filter(screenName == "ecoyuri") %>% summarise(favorite = sum(favoriteCount), retweet = sum(retweetCount)) %>% plot_ly(labels=colnames(.), values=c(favorite, retweet), type="pie", hole = 0.6)

holeを指定するとドーナツグラフ

Page 35: 可視化周辺の進化がヤヴァイ 〜2016〜

円グラフ(2/2)

Page 36: 可視化周辺の進化がヤヴァイ 〜2016〜

ヒストグラムdf %>% mutate(date=as.Date(created)) %>% group_by(name, date) %>% summarise(n=n()) %>% plot_ly(x=n, type="histogram", color=name)

type=“histogram”を指定

Page 37: 可視化周辺の進化がヤヴァイ 〜2016〜

ヒストグラム

Page 38: 可視化周辺の進化がヤヴァイ 〜2016〜

箱ひげ図df %>% mutate(date=as.Date(created)) %>% group_by(name, date) %>% summarise(n=n()) %>% plot_ly(x=n, type="box", color=name)

type=“box”を指定

Page 39: 可視化周辺の進化がヤヴァイ 〜2016〜

箱ひげ図

Page 40: 可視化周辺の進化がヤヴァイ 〜2016〜

•plotlyデモ

•plotlyによるグラフの作成

•plotlyグラフの調整

Page 41: 可視化周辺の進化がヤヴァイ 〜2016〜

グラフタイトルdf %>% plot_ly(x=favoriteCount, y=retweetCount, color=name, type="scatter", mode="markers") %>% layout(title="correlation of favorite and retweet")

layout()内で指定

Page 42: 可視化周辺の進化がヤヴァイ 〜2016〜

グラフタイトル

Page 43: 可視化周辺の進化がヤヴァイ 〜2016〜

x/y軸ラベルdf %>% plot_ly(x=favoriteCount, y=retweetCount, color=name, type="scatter", mode="markers") %>% layout(title="correlation of favorite and retweet", xaxis=list(title="favorite"), yaxis=list(title="retweet"))

xaxis=list(title=“”), yaxis=list(title=“”)で指定

Page 44: 可視化周辺の進化がヤヴァイ 〜2016〜

x/y軸ラベル

Page 45: 可視化周辺の進化がヤヴァイ 〜2016〜

凡例を消すdf %>% plot_ly(x=favoriteCount, y=retweetCount, color=name, type="scatter", mode="markers") %>% layout(title="correlation of favorite and retweet", xaxis=list(title="favorite"), yaxis=list(title="retweet"), showlegend=FALSE)

showlegend=FALSEで凡例なし

Page 46: 可視化周辺の進化がヤヴァイ 〜2016〜

凡例を消す

Page 47: 可視化周辺の進化がヤヴァイ 〜2016〜

凡例の位置を指定するdf %>% plot_ly(x=favoriteCount, y=retweetCount, color=name, type="scatter", mode="markers") %>% layout(title="correlation of favorite and retweet", xaxis=list(title="favorite"), yaxis=list(title="retweet"), legend=list(x=0.1, y=0.7))

legend=list(x=, y=)で 位置を指定(0から1の範囲)

Page 48: 可視化周辺の進化がヤヴァイ 〜2016〜

凡例の位置を指定する

Page 49: 可視化周辺の進化がヤヴァイ 〜2016〜

⾊を変えるdf %>% filter(screenName == "ecoyuri") %>% mutate(date=as.Date(created), hour=hour(created)) %>% group_by(date, hour) %>% summarise(n=n()) %>% plot_ly(x=hour, y=date, z=n, type="heatmap", colors="Spectral")

colorsでRColorBrewerの カラーパレット名を指定する

Page 50: 可視化周辺の進化がヤヴァイ 〜2016〜

⾊を変える

Page 51: 可視化周辺の進化がヤヴァイ 〜2016〜

2軸グラフを作るdf %>% filter(screenName == "ecoyuri") %>% mutate(date = as.Date(created), ratio = retweetCount/favoriteCount) %>% group_by(name, date) %>% summarise(n=n(), ratio=mean(ratio)) %>% plot_ly(x=date, y=n, type="bar", name="n") %>% add_trace(x=date, y=ratio, name="ratio", yaxis="y2") %>% layout(yaxis2=list(overlaying="y",side="right"))

yaxis2=list(overlaying="y",side="right")を指定する

Page 52: 可視化周辺の進化がヤヴァイ 〜2016〜

2軸グラフを作る

Page 53: 可視化周辺の進化がヤヴァイ 〜2016〜

ggplotをインタラクティブに

library(ggplot2) p <- df %>% ggplot(aes(x = favoriteCount, y = retweetCount)) + geom_point(aes(text = paste("Name:", name)), size = 4) + geom_smooth(aes(colour = name, fill = name)) + facet_wrap(~ name) ggplotly(p)

ggplotly()でラップするだけ

Page 54: 可視化周辺の進化がヤヴァイ 〜2016〜

ggplotをインタラクティブに

Page 55: 可視化周辺の進化がヤヴァイ 〜2016〜

その他のチャート

Page 56: 可視化周辺の進化がヤヴァイ 〜2016〜

チートシート

https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf

Page 57: 可視化周辺の進化がヤヴァイ 〜2016〜

まとめ

Page 58: 可視化周辺の進化がヤヴァイ 〜2016〜

•plotlyデモ

•plotlyによるグラフの作成

•plotlyグラフの調整

Page 59: 可視化周辺の進化がヤヴァイ 〜2016〜

おまけ

Page 60: 可視化周辺の進化がヤヴァイ 〜2016〜

各候補者と単語の対応分析

Page 61: 可視化周辺の進化がヤヴァイ 〜2016〜

各候補者と単語の共起ネットワーク

Page 62: 可視化周辺の進化がヤヴァイ 〜2016〜