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 / build_balanced_panel.R
Created July 17, 2020 12:03
Build balanced panel data set
```
################# 4) Build a Balanced Panel data set ------------------------------
# data with one observation for each area each day, even if there was no notification on that day/area
# get all dates and munis
all_munis <- unique(df$code_muni)
all_dates <- seq(min(df$DT_NOTIFIC),
max(df$DT_NOTIFIC),
by = "day")
@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

@rafapereirabr
rafapereirabr / metro_areas_brazil_geobr.md
Last active May 23, 2021 18:54
plot Brazilian metropolitan areas in different years

Using R and the geobr package to plot Brazilian metropolitan areas in different years.

logo

library(geobr)
library(dplyr)
library(ggplot2)
library(sf)
@rafapereirabr
rafapereirabr / ortho_map.md
Last active September 27, 2023 18:34
how to create a world map using full hemispheric orthographic projection in `R`

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)

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 / 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
@rafapereirabr
rafapereirabr / geobr_intro.md
Last active March 14, 2021 23:10
Quick intro do geobr

Quick intro to geobr logo

geobr is an R package that allows users to easily download shapefiles of the Brazilian Institute of Geography and Statistics (IBGE) and other official spatial data sets of Brazil.

Installation

 install.packages("geobr")
  
  library(geobr)