Skip to content

Instantly share code, notes, and snippets.

View rafapereirabr's full-sized avatar

Rafael H M Pereira rafapereirabr

View GitHub Profile
@rafapereirabr
rafapereirabr / 0_reuse_code.js
Created April 23, 2016 13:39
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@rafapereirabr
rafapereirabr / compute_distance_btw_coordinates.md
Last active April 24, 2016 18:27
How to efficiently calculate distance between pair of coordinates using data.table :=

This is the fastest way I've found so far to calculate the distances between pairs of lat long coordinates. The method takes advantage of distGeo{geosphere} and the crazy fast := operator of data.table.

Load libraries and data for Reproducible example

# load libraries
  library(data.table)
  library(dplyr)
  library(sp)
  library(rgeos)
  library(UScensus2000tract)
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")
@rafapereirabr
rafapereirabr / wordmap_cities.R
Last active May 2, 2019 23:14
Simple code to create a world map of cities with population > than 40K in R
library(tidyverse)
library(maps)
temp <- maps::world.cities %>%
ggplot() +
geom_point(aes(x=long, y=lat, size=pop, fill=pop, color=pop), alpha=.1, show.legend = FALSE) +
theme_void() +
coord_equal()
ggsave( temp, file="pop.png", dpi = 300, width = 20, height = 10)
library(data.table)
library(ggplot2)
library(sf)
library(dplyr)
library(maps)
library(rworldmap)
# Visitors data -------------------
@rafapereirabr
rafapereirabr / r_tip1_string_matching.md
Last active September 6, 2019 00:12
Using the data.table operator `%like%` for partial string matching:

R tip of the day: partial string matching

The data.table package has the operator %like%, which is super handy for partial string matching:

"system with blue screen" %in% "blue"
> FALSE

"system with blue screen" %like% "blue"
> TRUE

Simple example of how to create a world map using full hemispheric orthographic projection in R

library(sf)
library(ggplot2)
library(mapview)
library(lwgeom)
library(rnaturalearth)
@rafapereirabr
rafapereirabr / interactive_large_spatial.R
Last active October 14, 2019 13:08
Interactive map or large spatial data sets (demo geobr)
# Libraries
library(geobr)
library(leafgl)
library(leaflet)
library(sf)
library(colourvalues)
# get data of disaster risk areas in Brazil using the geobr package
@rafapereirabr
rafapereirabr / animated_transport_networks_GTFS.md
Last active April 24, 2020 19:00
Creating an animated gif of public transport networks using GTFS data

Creating an animated gif of public transport networks using GTFS data and the gtfs2gps package

In this gist we show with a reproducible example how to create an animation of public transport networks using GTFS data in R. We use a few packages to do this. One of the core packages here is the new gtfs2gps. The gtfs2gps package converts public transport data in GTFS format to GPS-like records in a data.frame/data.table, which we will be using to create a .gif with the gganimate package.

Step 1 - Convert GTFS to GPS-like records

The first step is to process a GTFS.zip file. The function gtfs2gps{gtfs2gps} interpolates the space-time position of each vehicle in each trip considering the network distance and average speed between stops. The output is a data.table where each row represents the timestamp of each vehicle at a given spatial resolution. In this example, we use a sample of the public transport network of Sao Paulo (Brazil) mapped e