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 / 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 / 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 / life_expect_world_map_gif.md
Last active November 15, 2020 06:31
creating an animated (gif) world map of life expectancy using ggplot2

This gist shows how to create an animated world map of life expectancy using R. The data comes the UN World Population Prospects, 2015 Revision, and it brings life expectancy data from 1950 untill 2015 and projeceted data up to 2100. Thanks Topi Tjukanov, who reminded me of the UN DESA data portal, where you can find this dataset and many others

The idea is to use open data to create a GIF, much like the ones created by Aron Strandberg but his maps look much nicer. The output of this script is a map like this one:

[![enter image description here][1]][1]

Now diving into the code. First, let's load the necessary libraries and get the data. As of this writing, the current version of gganimate has a bug that messes the aesthetics of the .gif file. As a temporary solution, I've intalled an older version of the package, [as recommended by the author of the gganimate, David Ro

@rafapereirabr
rafapereirabr / jenks_natural_breaks.R
Last active June 21, 2021 21:48
categorize date using jenks natural breaks
library(stringr)
library(stringi)
library(ggplot2)
library(viridis)
library(data.table)
library(BAMMtools) # fast calculation of jenks natural breaks
library(ggt)
# load data
data("iris")
@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 / 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 -------------------