增補資料:路線摘要統計
我們已經建立了一個幾乎可以直接用於視覺化的資料集 route_hod。現在再加入幾個變數,作為認知指標(cognostics),以便你在互動檢視圖表時更好地探索。
本練習屬於課程
使用 R 的 Trelliscope 視覺化巨量資料
練習說明
- 針對每條路線,計算總騎乘次數。
- 針對每條路線,計算工作日與週末每小時平均騎乘數的差值。注意變數
n已彙總為每小時的計數,因此例如工作日的平均可用mean(n[weekday == "workweek"])計算。 - 針對每條路線,新增一個變數,內容為指向該路線之 Google Maps 的 URL,請使用提供的
make_gmap_url函式,並從資料中傳入合適的引數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
library(trelliscopejs)
library(ggplot2)
library(dplyr)
# Function to construct a Google maps URL with cycling directions
make_gmap_url <- function(start_lat, start_lon, end_lat, end_lon) {
paste0("https://www.google.com/maps/dir/?api=1",
"&origin=", start_lat, ",", start_lon,
"&destination=", end_lat, ",", end_lon,
"&travelmode=bicycling")
}
# Compute tot_rides, weekday_diff, and map_url
route_hod_updated <- route_hod %>% ungroup() %>%
group_by(start_station_code, end_station_code) %>%
mutate(
tot_rides = sum(___),
weekday_diff = mean(n[weekday == "workweek"]) - ___,
map_url = ___)