Sunday, August 14, 2016

Playing around with (messy) GPS data of the bus routes in Rio using R

click on the image to enlarge it

Here is a snippet of the code I've used to create this plot in R using ggmap. I've also created a gist with a fully reproducible example of how to make flow maps in R using another dataset, in case you're interested.

library(data.table)
library(ggmap)
# load data. Obs: The file used in this example does not allow to create flow maps
df <- fread("your_data.csv)
# Get Map background
map <- get_map(location = c(lon = -43.45967, lat = -22.92728),
zoom = 10, source = "stamen", maptype = "toner-background")
# plot
ggmap(map) +
geom_path(data = df, aes(x= longitude, y= latitude, color= route_id ), alpha=0.3) +
coord_map(xlim = c(-43.828854, -43.117908),ylim = c(-23.091709, -22.760493)) +
theme(legend.position="none")
view raw map_gps_bus.R hosted with ❤ by GitHub