数据增广:路线汇总统计
我们已经构建了几乎可以直接用于可视化的数据集 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 = ___)