Tuesday, October 13, 2015

Pi an experiment with circlize package

Pi to 1000 places connected

Idea:

In the past i have often observed circular plots (cant find a technical name) in 

1) New York times[1] to display human genome data
2) Migration patterns among humans[2]
3) to display import and export data (Bloomberg terminal).
4) Mathematical art [3]

My understanding of these circular plots is very new but considering the popularity of these plots i believe it would be unjust not to discuss them on my blog. This post is motivated by an Instagram image of a similar plot created using circos software[4]. The image is created using pi numbers upto 1000 decimal places and connecting the consecutive numbers. We know the value of pi is 3.141.... and the visualization is generated by connecting 3 to 1, 1 to 4, 4 to 1 and so on. We all know numbers in a pi are random and follow no particular order but we see a sort of an image emerge over here. A structure in random values.

Data:   


The data file was created by copying and pasting pi numbers from here. You would observe that copying and pasting values from web into excel is a problem but i prefer using csv format and hence i had no other choice. I further tidied the data using the =left() and = right() functions to display the values in one column.copied them to the next column. In case you like to access the file it is here .

Code:


install.packages("circlize")
library("circlize")
circos.par(gap.degree = 3)
pi= read.csv("pi.csv")
colors= c("#542D15","#C07F4D", "#4B2078","#C153C9","#0A6A6C","#1DCEC6","#094AA5","#0181FD","white","#92B966" )
chordDiagram(pi, grid.col= colors, grid.border=c("white"),transparency=0.5)

Load up RStudio:

Circlize is a beautiful package, especially if you are trying to visualize genome data or if you are trying to exhibit flow of information from one sector to the next. The author has done an amazing job at explaining the package [5]. 

In our case we just need 3 lines of code. The circos.par() function simply creates gaps between the sector so that the image looks prettier.

I sort of cheated by creating the image in R and then importing he same to inkscape to add the background black color. Also note that we have 10 sectors for values going from 0 to 9. However, they are not placed in ascending or descending order. I am still learning the package and can probably show you how to order the sectors in my next post.

References:
[1] NY Times :  Genome data
[2] Migration Flows :Migration Flows
[3] Mathematical Art :Pi
[4] Circos Sotware : circos website
[5] An Introduction to circlize package: circlize

12 comments: