Visualization with R

Taiwan R user Group

陳奎銘 (benjamin0901@gmail.com)
2014.01.18

Why Visualization

  • 更快得到答案
  • 加深印象
  • 發現被忽略的資訊









假設今天我想要觀察台股最近幾年的走勢...

[1] "TWII"









這樣鬼才看得懂...



plot of chunk unnamed-chunk-2

Why R?

因為...

  • 我想迅速把圖畫好
  • 各種強大的套件
  • 可以和各種平台結合
  • 容易做資料分析
  • 資料量過大

R的視覺化軍火庫

靜態

  • graphics
  • ggplot2

動態

  • googleVis
  • rCharts

製作網頁

  • shiny









假設今天有個阿宅想要辦聯誼...



在網路上看到...

立馬找出男女比例懸殊的幾個系







接下來,聯誼什麼的,就不干R的事囉~~~

還是乖乖畫圖吧~~~

graphics

plot of chunk unnamed-chunk-4

graphics

優點

  • 簡單
  • 可快速觀察資料
  • 自由度大,易調整版面

缺點

  • 預設值較簡陋
  • 要改到好看,需要很多參數和美術天份

教學

ggplot2

plot of chunk unnamed-chunk-5

ggplot2

起手式

  • data.frame
  • ggplot(data,aes(x=...,y=...))+geom_XXX()

優點

  • 預設值佳、有質感

缺點

  • 需花點心思學習
  • 版面調整需要一些技巧

教學

延伸閱讀

googleVis

googleVis

  • R & Google Charts API
  • Visualize data with Google Chart Tools without manually upload data to Google.
  • data.frame

googleVis

優點

  • 互動式呈現
  • 簡單
  • 預設值就很讚

缺點

  • 需要網路
  • 需要稍微懂一點JaveScript,才比較好調整參數

教學

rCharts

rCharts

  • R與JavaScript的結合
  • 有一堆JavaScript的library:highchartsMorrisNVD3PolychartsRickshawxchartsLeaflet(for map)...

rCharts

install.package("devtools")
library(devtools)
install_github('rCharts', 'ramnathv')

rCharts

優點

  • 互動式呈現
  • 好看、新奇、又好玩
  • 方便上傳github和rPubs

缺點

  • 太多library,畫法沒整合
  • 開發中
  • 部分語法需要網路

教學

shiny

  • 以R做網頁的工具
  • 幾乎只要會R就行
  • 互動式呈現資料
  • 支援各式繪圖工具:graphics、ggplot2、googleVis和rCharts

教學

shiny

install.packages("shiny")
library(shiny)
runExample()
## Valid examples are "01_hello", "02_text", "03_reactivity", "04_mpg",
## "05_sliders", "06_tabsets", "07_widgets", "08_html", "09_upload",
## "10_download", "11_timer"

shiny

runExample("01_hello")









回到之前的例子

graphics ggplot2
googleVis
rCharts

Code

graphics

barplot(target1, beside=TRUE, legend=colnames(target1))

ggplot2

ggplot(aes(x=disp, y=number, fill=sex), data=target2)+
geom_bar(stat="identity", position="dodge")

googleVis

bar <- gvisColumnChart(target, xvar="disp", yvar=c("male","female"))
plot(bar)

rCharts

hPlot(number ~ disp, group = "sex", data = target2, type = "column")
googleVis graphics ggplot2, rCharts
data.frame matrix data.frame









偶爾也要關心一下台灣...









最近有個新聞

所得差距

二十等分位 vs. 五等分位

plot of chunk unnamed-chunk-12

低收入戶比例

plot of chunk unnamed-chunk-13

低收入戶分佈 (ggplot2::geom_polygon)

plot of chunk unnamed-chunk-14









過年去台東玩吧~~









救救老殘窮...









出去玩之前,要衡量一下自己的錢包...

歷年薪資分佈(gVisMotionChart)

資料來源

Taiwan R User Group

Thank you~