Human diffusion and city influence

I recently co-authored a paper entitled “Human diffusion and city influence” in which we propose a new method to measure global and regional influence of world cities using geolocated tweets. In this paper, we select 58 out of the most populated cities of the world and analyze their influence in terms of the average radius traveled and the area covered by Twitter users visiting each of them as a function of time. In this post, I’ll show you how to make a movie displaying the “diffusion” of Twitter users from one city to the rest of the world with R. More precisely, we are going to generate and to merge into a gif file several maps of geolocated tweets of users who have been at least once in a city (Paris for example) according to the number of days since the first passage in the city.

Let’s consider a file tweet containing spatio-temporal information about tweets posted by Twitter users who have been at least once in Paris. Each line represents a tweet and each tweet has three attributes:

  • Longitude (column 1);
  • Latitude (column 2);
  • Number of days since the first passage in Paris (column 3). All the tweets posted before the first visit in Paris have been removed.

First, we have to generate the maps of geolocated tweets of users who have been at least once in Paris according to the number of days since the first passage in the city. To do so, we plot on a map the tweet for which the number of days since the first passage in Paris is lower that d using the function map of the fields package. Then, we export the map in png in a folder “HumanDiffusion_Paris”. We repeat the process for different value of d in order to obtain a temporal evolution. Here is the code:

dir.create("HumanDiffusion_Paris")

d=c(1:5, seq(10,100,10), seq(100,1000,100))
colo="#CC6666"

for(i in 1:length(d)){

    png(paste("HumanDiffusion_Paris/Diffusion_", letters[i], ".png",sep=""), width=5.916667, height=2.885417, units="in", res=300)

    colomap=rgb(230,230,230,maxColorValue=255)
    map("world", col=colomap, fill=TRUE, bg="white", lwd=0.05, border=colomap, mar=c(0,0,0,0), resolution=0)
    points(tweet[ tweet[,3]<=d[i] ,c(1,2)], pch=16, col=colo, cex=0.15)
    legend("topleft", legend=paste("Day ",d[i],sep=""), text.col=colo, cex=1.5, bty="n")

    dev.off()

}

We can now make the movie by merging the png files into a single gif file. First install ImageMagick and then invoke convert with a system command. You can play with the parameter delay to adjust the speed.

system("cmd.exe /c cd HumanDiffusion_Paris && convert -delay 80 Diffusion*.png HumanDiffusion.gif")

The results is quite nice, you can observe the diffusion process, very fast during the first weeks due to the presence of non-local users coming back home. If you are interested in this work, the paper is available here .

comments powered by Disqus