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 / Flow Map in R
Last active February 24, 2024 00:46
Create Flow Map in R using ggplot2
## This gist shows how to create Flow Maps in R using ggplot2.
## source: This is based on different bits of code from other with amazing R skills:
@ceng_l : http://web.stanford.edu/~cengel/cgi-bin/anthrospace/great-circles-on-a-recentered-worldmap-in-ggplot
@3wen : http://egallic.fr/maps-with-r/
@spatialanalysis : http://spatialanalysis.co.uk/2012/06/mapping-worlds-biggest-airlines/
@freakonometrics : http://freakonometrics.hypotheses.org/48184
# Libraries
@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)
@rafapereirabr
rafapereirabr / clean_polygons_remove_slivers.md
Last active August 16, 2023 01:42
Clean polygons to remove slivers

Trying different methods to clean polygons to remove slivers

Load libraries

library(sf)
library(dplyr)
library(lwgeom)

# using planar geometry
sf::sf_use_s2(FALSE)
@rafapereirabr
rafapereirabr / walking_isochrone_sao_paulo_r5r.R
Created August 14, 2023 14:49
walking isochrone Sao Paulo with r5r
library(r5r)
library(mapview)
# build transport network
data_path <- system.file("extdata/spo", package = "r5r")
r5r_core <- setup_r5(data_path)
# load origin/destination points
points <- read.csv(file.path(data_path, "spo_hexgrid.csv"))
origin_1 <- subset(points, id =='89a8100c393ffff')
@rafapereirabr
rafapereirabr / flight_passengers_sdu_gig.R
Created August 8, 2023 22:58
Comparing number of passengers using Rio's airports
library(flightsbr)
library(ggplot2)
library(data.table)
# download data
df <- flightsbr::read_flights(date = 2019:2023)
# filters
df <- df[ nr_ano_chegada_real >= 2019,]
df_rj <- df[ sg_iata_origem %in% c('SDU', 'GIG') |
@rafapereirabr
rafapereirabr / stacked_map_R_ggplot2.md
Last active March 29, 2023 18:41
Creating a stacked map in R using ggplot2

This gist shows in two steps how to tilt and stack maps using ggplot2 in order to create an image like this one: [![enter image description here][1]][1]

Let's load the necessary libraries and data to use a reproducible example:

# load libraries
  library(rgeos)
  library(UScensus2000tract)
  library(ggplot2)
@rafapereirabr
rafapereirabr / heatmap_pseudo_log.R
Created February 12, 2023 15:51
heat map with pseudo_log scale
# Library
library(ggplot2)
library(viridis)
# Dummy data
set.seed(42)
x <- LETTERS[1:20]
y <- paste0("var", seq(1,20))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(400, 0, 10000)
@rafapereirabr
rafapereirabr / bivariate_lisa.R
Last active February 4, 2023 19:09
Map of bivariate spatial correlation in R (bivariate LISA)
library(stringr)
library(spdep)
library(rgdal)
library(magrittr)
library(ggplot2)
library(sf)
#======================================================
@rafapereirabr
rafapereirabr / test_gdalio.R
Created December 29, 2022 18:07
generating aerial images with gdalio
library(sf)
library(terra)
library(gdalio)
library(geobr)
### Choose either a small or large area
i = 49 # small area
i = 1066 # large area