Upload
takashi-kitano
View
3.236
Download
1
Embed Size (px)
Citation preview
主要ライブラリの⽐較⼤分類 グラフ Plotly rbokeh highcharter
基本グラフ
折れ線グラフ ○ ○ ○バブルチャート ○ × ?
散布図 ○ ○ ○ヒートマップ ○ × ○
棒グラフ ○ × ○⾯グラフ ○ × ○円グラフ ○ × ○
統計グラフヒストグラム ○ ○ ○
箱ひげ図 ○ ○ ○
可視化のサンプルデータ• 東京都知事選挙候補者の中3名のTweetデータ
⼩池 百合⼦ 209,760 2,740
⿃越 俊太郎 159,788
541
増⽥寛也 6,476 265
(敬称略)
フォロワー数 ツイート数
可視化のサンプルデータ• 東京都知事選挙候補者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
デモhttps://code-estrellita.net/tokyo_election_2016.html
折れ線グラフ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”を指定
バブルチャート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を指定
散布図df %>% plot_ly(x=favoriteCount, y=retweetCount, color=name, type="scatter", mode="markers")
type = “scatter” mode = “markers”を指定
ヒートマップ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”を指定
棒グラフ(1/3)df %>% group_by(name) %>% summarise(n=n()) %>% plot_ly(x=name, y=n, type="bar")
type = “bar”を指定
棒グラフ(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()で系列を追加
棒グラフ(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”で 積み上げ棒グラフ
⾯グラフ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”を指定
円グラフ(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”を指定
円グラフ(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を指定するとドーナツグラフ
ヒストグラムdf %>% mutate(date=as.Date(created)) %>% group_by(name, date) %>% summarise(n=n()) %>% plot_ly(x=n, type="histogram", color=name)
type=“histogram”を指定
箱ひげ図df %>% mutate(date=as.Date(created)) %>% group_by(name, date) %>% summarise(n=n()) %>% plot_ly(x=n, type="box", color=name)
type=“box”を指定
グラフタイトルdf %>% plot_ly(x=favoriteCount, y=retweetCount, color=name, type="scatter", mode="markers") %>% layout(title="correlation of favorite and retweet")
layout()内で指定
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=“”)で指定
凡例を消す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で凡例なし
凡例の位置を指定する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の範囲)
⾊を変える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の カラーパレット名を指定する
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")を指定する
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()でラップするだけ