關卡 1

大家好,本堂課主要跟大家解說,如何在R 裡面處理新時代的互動式繪圖與新型態的數據。

關卡 2

我們會介紹的是googleVis套件。 請同學安裝googleVis

check_then_install("googleVis", "0.5.10")

關卡 3

請同學載入googleVis套件。

library(googleVis)

關卡 4

同學請打開vignette("googleVis_examples", package = "googleVis"), 我們簡單用這個文件做範例。

vignette("googleVis_examples", package = "googleVis")

關卡 5

在第一章Line chart的部份,同學可以把滑鼠移動到線上。 這就是用網頁技術做視覺化的好處:圖形可以「直接」跟使用者互動,而不需要透過程式碼。

關卡 6

然而googleVis的API設計,需要我們完全自行整理資料。 因此我們得先載入dplyr。 請同學安裝dplyr或是輸入skip()跳過。

check_then_install("dplyr", "0.4.3")

關卡 7

請同學載入dplyr。

library(dplyr)

關卡 8

請同學輸入: dat1 <- group_by(hsb, race, sex) %>% summarise(math.avg = mean(math))

dat1 <- group_by(hsb, race, sex) %>% summarise(math.avg = mean(math))

關卡 9

接著我們輸入:g <- gvisBarChart(dat1)建立一個gvis物件。 如果同學探索這個物件,會發現它是一個網頁的HTML。

g <- gvisBarChart(dat1)

關卡 10

接著,我們輸入:plot(g)

plot(g)

關卡 11

同學的電腦瀏覽器中會打開一個網頁,上面繪製著一份barchart。 如果我們把滑鼠移上去,還會跳出確切的值。

關卡 12

然而,gvisBarChart接收資料的邏輯和dplyr所整理出的資料不太一致。 gvisBarChart將操作顏色的類別,放在data.frame的column方向。 因此,我們需要用一個可以快速做轉換的套件。 請同學輸入skip()檢查是否有安裝reshape2套件。 若無,課程會自動安裝該套件。

check_then_install("reshape2", "1.4.1")

關卡 13

請同學載入reshape2。

library(reshape2)

關卡 14

接著我們執行:dcast(dat1, race ~ sex)

dcast(dat1, race ~ sex)

關卡 15

同學會看到sex欄位被拉到data.frame的欄位,也就是female和male各一欄。

關卡 16

讓我們把這筆資料提供給gvisBarChart後呈現在網頁上。 請同學輸入:dcast(dat1, race ~ sex) %>% gvisBarChart() %>% plot()

dcast(dat1, race ~ sex) %>% gvisBarChart() %>% plot()

關卡 17

這次就會看到不同的性別有著不同的顏色。

關卡 18

接著我們挑出read和math做scatter plot。 請同學輸入:select(hsb, read, math) %>% gvisScatterChart() %>% plot()

select(hsb, read, math) %>% gvisScatterChart() %>% plot()

關卡 19

目前googleVis整合了google Chart API,提供了許多功能。 但是和ggplot2相比,它的功能仍是相對簡單。 googleVis這類套件帶給R社群的影響,是讓大家了解javascript based的繪圖引擎所帶來的可能性。

關卡 20

傳統的視覺化工具需要使用者與R互動,技術門檻很高。 但是javascript等網頁技術則可以大幅度的降低這類技術門檻。

關卡 21

因此,現在R 與javascript整合的套件可說是百花齊放,可惜目前為止我還沒有注意到有一套像ggplot2一樣有代表性的套件出現。

關卡 22

googleVis也帶來了傳統R所沒有提供的繪圖類型。例如:Sankey Diagram。

關卡 23

請同學輸入:gvisSankey(dat_sk) %>% plot()

gvisSankey(dat_sk) %>% plot()

關卡 24

這張圖描述了兩個state之間的流動。 舉例來說,如果我們知道去年購買不同品牌筆電的消費者,今年各自是買哪一個品牌的筆電,那透過分析品牌間的數據變化,我們就可以利用Sankey Diagram來觀察變化 的細節。

關卡 25

接著,請同學輸入: gvisCalendar(TWII) %>% plot

gvisCalendar(TWII) %>% plot

關卡 26

這是拿台股指數搭配googleVis提供的月曆API所繪製的圖。 我們可以從中清楚的看到台股停止交易(或是yahoo沒有抓到資料)的日期,大部份為週末。 這類和人的活動很相關的資料,使用googleVis進行呈現,會比直接用傳統的line chart還要容易發現數據和周末等日期之間的相關性。

關卡 27

還有其他範例,如:gvisOrgChart(Regions) %>% plot則是拿來繪製組織圖的選擇。

gvisOrgChart(Regions) %>% plot

關卡 28

接著我們介紹ggmap這個可以處理地理資料的套件。 請同學輸入skip()檢查有無安裝ggmap。

check_then_install("ggmap", "2.6.1")

關卡 29

接著,請載入ggmap。

library(ggmap)

關卡 30

eq是來自<http://earthquake.usgs.gov>所抓取的地震資料。 請同學輸入View(eq)觀察一下這筆資料。

View(eq)

關卡 31

同學可以看到,eq中有提供地震的位置(經緯度:longitude, latitude)和地震的規模(mag)。

關卡 32

接著我們示範一次如何將地震畫在地圖中。 首先我們要先從google拿到圖資。 請同學輸入:twmap <- try(get_map("Taiwan", 3), silent = TRUE)。 第一個參數是告訴google我們要抓取的圖資的位置。 第二個參數則是地圖的範圍大小,值從3(世界規模)到21(建築物的規模)。

twmap <- readRDS(.get_path("twmap.Rds"))

關卡 33

接著請同學輸入: skip()來確認圖資是否確實載入。

if (class(twmap)[1] == "try-error") twmap <- readRDS(.get_path("twmap.Rds"))

關卡 34

我們接著建立ggplot物件:g <- ggmap(twmap, extent = "device")

g <- ggmap(twmap, extent = "device")

關卡 35

我們輸入g看一下剛剛抓回來的圖資。

g

關卡 36

接著請同學輸入:g + geom_point(aes(x = longitude, y = latitude), data = eq), 來繪製新的圖層標示地震位置。

g + geom_point(aes(x = longitude, y = latitude), data = eq)

關卡 37

接著請同學輸入:g + geom_point(aes(x = longitude, y = latitude, size = mag), data = eq), 來用點的大小表示地震規模。

g + geom_point(aes(x = longitude, y = latitude, size = mag), data = eq)

關卡 38

如果同學熟悉ggplot2的話,ggmap應該就可以很快的上手了。

關卡 39

最後我們請同學利用之前所學,從pirate_path中爬取經緯度,再利用ggmap將海盜的通報事件畫到地圖上。

src <- readLines(file(pirate_path, encoding = "BIG5"))
tmp <- strsplit(src, readLines(.get_path(".hw"))[2])
key <- sapply(tmp, "[[", 1)
is_target <- key == readLines(.get_path(".hw"))[1]
value <- sapply(tmp[is_target], "[[", 2)
pirate <- data.frame(
  lat = substring(value, 3, 4) %>% as.numeric + substring(value, 6, 7) %>% as.numeric / 60,
  log = substring(value, 12, 14) %>% as.numeric + substring(value, 16, 17) %>% as.numeric / 60)
if (!exists("twmap")) twmap <- readRDS(.get_path("twmap.Rds"))
g <- ggmap(twmap, extent = "device")
g + geom_point(aes(x = log, y = lat), data = pirate)