Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting email flow in map using R

Tags:

email

plot

r

map

I want to plot email from and to in world. For example, I received the following number of emails from the following countries and I live in USA.

recievedcountry <- c("India", "China", "France", "Chile", "Australia", "Chad",
                    "Nepal", "Burma")
rfrequency <- c(12, 20, 5, 2, 12, 1, 3, 2) # frequency of emails 
sendcountry <- c("Canda", "USA", "France", "India", "China", "Japan")
sfrequency <- c(14, 108, 12, 15, 18, 4)

This what I tried but I do not know how to conect with lines:

require(fields)
world(xlim=c(-90,90),ylim=c(-180,180), xaxt = "s", yaxt = "s")
grid()

The following is my hypothesize model:

enter image description here

like image 273
jon Avatar asked Mar 26 '12 13:03

jon


2 Answers

This has been thought before. Look at this blog entry, it describes how to construct such a map very carefully.

http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/

like image 55
aatrujillob Avatar answered Sep 30 '22 04:09

aatrujillob


Based on AdresT (thanks !) link provided above I could answer my own question except, line thinkness proportional to number of emails

Here is code: I used random data without searching exact longitude and latitude for each location.

library(maps)
library(geosphere)
map("world", col = "green4", bg="#F5FFFA", lwd=0.05)
myposition <- c(-74, 40) # my position (where I am opening emails)

rlong <- c(75, 105, 135, - 10.2,  45.2, -30.4, 105, 35, -150, 
   10.2,  145.2, 30.4) # received lat
rlat <- c(30, 43, 23, 12, 68, 55.6, 30, 43, 23, 12, 68, 55.6) # received long
nrecived <- c(4, 10, 5, 2, 4, 10, 4, 10, 5, 2, 4, 10 )     # number of email received
slong <- c(85, 85, 55, -40.2,  45.2, -30.4,45, 95, 55, 40.2,  55.2, 60.4 ) # send lat
slat <- c(10, 43, 13, 12, 68, 55.6,10, 43, 13, 12, 68, 55.6 ) # send long
nsend <- c(4, 10, 5, 2, 4, 10, 4, 10, 5, 2, 4, 10 )     # number of email send

mydf <- data.frame (rlat, rlong, nrecived, slat, slong, nsend)

for (i in 1: length (mydf) ) {
            send <- gcIntermediate(c(mydf[i,]$slong, mydf[i,]$slat), c(-74, 40), 
                   n=100, addStartEnd=TRUE)
            lines (send, col = "blue", lwd = mydf[i, "nsend"]) # edited 
                                                                following suggestion 
            received <- gcIntermediate(c(mydf[i,]$rlong, mydf[i,]$rlat), c(-74, 40), 
             n=100,  addStartEnd=TRUE)
            lines (received , col = "red", lwd = mydf[i, "nrecived"])
            }

The output map: enter image description here

If somebody can help to thickness of line proportional to number of emails will be great !

After fixing following line size suggestion from Roman Luštrik,

enter image description here

Line size to broad may be need to reclass into smaller numbers !

like image 29
jon Avatar answered Sep 30 '22 05:09

jon