bvng1 / medium_visualization

Home Page:https://twitter.com/ricardobion

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

500k top Airbnb trips: created in R with ggplot2

A few people asked for a high-resolution image and the code that I used to generate the ggplot2 visualization on my recent Medium post "Using R Packages and Education to Scale Data Sience at Airbnb".

High-res image:



Link to high-res image for download



R code:

library(ggplot2)
library(ggmap)
library(sp)
library(grid)
library(geosphere)
library(plyr)
# source the theme_map for ggplot2
source("https://github.com/ricardo-bion/medium_visualization/blob/master/theme_map.R")

# in the original post I had a data.frame with 500k rows of top origin destination pairs
trips <- data.frame(origin = c("San Francisco", "Sydney"), 
                    destination = c("Paris", "Tokyo"), 
                    stringsAsFactors = FALSE)
  
  
# get lat and lon of cities
trips$geocode_origin <- suppressMessages(geocode(trips$origin))
trips$geocode_destination <- suppressMessages(geocode(trips$destination))


# get intermediate points between the two locations
arch <- gcIntermediate(trips$geocode_origin,
               trips$geocode_destination,
               n=100,
               breakAtDateLine=FALSE, 
               addStartEnd=TRUE, sp=TRUE)

# http://docs.ggplot2.org/0.9.3.1/fortify.map.html
arch_fortified <- ldply(arch@lines, fortify)

# a few lines of ggplot2 code
ggplot() +
  geom_line(aes(long,lat,group=group), data=arch_fortified, alpha=0.1,size=1, colour="skyblue1") +
  coord_cartesian(ylim =c(-45, 70), xlim=c(-165, 165)) +
  theme_map() +
  geom_point(aes(lon, lat),data=trips$geocode_origin, alpha = 0.8, size = 1, colour = "white") +
  geom_point(aes(lon, lat),data=trips$geocode_destination, alpha = 0.8, size = 1, colour = "white")



Growth over time:

About

https://twitter.com/ricardobion


Languages

Language:R 100.0%