Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Created January 30, 2012 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhawksey/1704627 to your computer and use it in GitHub Desktop.
Save mhawksey/1704627 to your computer and use it in GitHub Desktop.
CaPReT (great circles) map
# Based on Nathan Yau's How to map connections with great circles
# http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/
# Prelim stages
library(maps)
library(geosphere)
# Google Spreadsheet key (must be published to the web)
key='0AqGkLMU9sHmLdDd5UXFGdEJGUjEyN3M5clU1X2R5V0E'
# Sheet gid name
gid=2
# Read data from spreadsheet
dataset = read.csv(paste('https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=', key ,'&single=true&gid=', gid, '&output=csv', sep=""), header = T)
# Colors
pdf("capret.pdf", width=11, height=7)
map("world", col="#F2F2F2", fill=TRUE, bg="#ffffff", lwd=0.05)
pal <- colorRampPalette(c("#333333", "white", "#1292db"))
colors <- pal(100)
# get a domain frquency table of text source domains
sourceFreq <- as.data.frame(table(dataset$domain))
# get the maximum frquency
maxcnt <- max(sourceFreq$Freq)
#order by domain frequency - most last
dataset <- [order(dataset$domain),]
for (j in 1:nrow(dataset)) {
inter <- gcIntermediate(c(dataset[j,]$source_long, dataset[j,]$source_lat), c(dataset[j,]$target_long, dataset[j,]$target_lat), n=100, addStartEnd=TRUE)
colindex <- round( (subset(sourceFreq,Var1==dataset[j,]$domain)$Freq / maxcnt) * length(colors) )
lines(inter, col=colors[colindex], lwd=0.8)
}
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment