# Data Art with 100k digits of pi

**R | Asitav Sen**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

## Data Art

As the name suggests, data art is an art created using data of any kind. Well, the definition may vary. It’s not an exact science. Some may use random data while others may use real life data. I recently came across the concept from the R graph Gallery.

I started off the experiments with digits of pi, denoted as π. It is a mathematical constant and defined as the ratio of a circle’s circumference to its diameter. Other definitions also exist, but this is good enough. It is also known as Archimedes constant.

It is an interesting number. It is an irrational number. \(\frac{22}{7}\) is commonly used as the value. The decimal equivalent doesn’t end and also doesn’t follow any known pattern. The lack of pattern satisfies a kind of statistical randomness. Anyway, that’s too much of information.

#Art with digits of Pi

##Extracting data and preparing coordinates

The first step to start the art is to find the value of pi up to a certain number of digits. Several options are available. I used this.

I used the following code to extract data and create a data frame of coordinates by putting alternative digits in x and y.

library(dplyr) library(ggplot2) #Defining URL to download csv (100k digits) url<-"https://www.angio.net/pi/digits/100000.txt" #reading file data.raw<-readr::read_file(url) #splitting string into single characters data.vec<-strsplit(data.raw, "") #vector to dataframe and data.df1<-data.frame(data.vec[1]) #removing the "." i.e. 2nd row data.df2<-data.frame(data.df1[-c(2),]) #renaming column to digits names(data.df2)[1]<-"digits" ##Dividing the digits into x and y #Adding a column of row number data.df3<-data.df2%>% mutate(id=seq(1:nrow(data.df2))) #Adding a column of coordinate (x or y) and reshuffling the columns data.df4<-data.df3%>% mutate(cor=ifelse(id%%2==0,"y","x")) #changing class of digits to integers. Earlier parsed as char data.df4$digits<-as.integer(data.df4$digits) #Creating data frame of x and y coordinates x<-data.df4[c(T,F),1] y<-data.df4[c(F,T),1] data.cor<-data.frame(x,y) head(data.cor) ## x y ## 1 3 1 ## 2 4 1 ## 3 5 9 ## 4 2 6 ## 5 5 3 ## 6 5 8

## Conceptualizing and creating the art

I decided to create an art using curves. I divided the coordinates into groups of thousand.In each group I plotted curve connecting all the dots to the first one. It created an interesting art which I like.

#creating a column showing row position data.cor$pos<-seq(1:nrow(data.cor)) #defining the size of each group lot=1000 #Creating groups data.plot<-data.cor%>% mutate(grp=floor(pos/lot)+1) #Creating vectors with coordinates of first dot of each group minx=NULL miny=NULL for(i in 0:(49999)){ j=(floor(i/lot)*lot)+1 minx[i+1]<-data.plot$x[j] miny[i+1]<-data.plot$y[j] } #creating columns from vector data.plot$xend=minx data.plot$yend=miny #Removing rows containing coordinates of first point of each group rowrmv<-c(which(data.plot$x==data.plot$xend & data.plot$y==data.plot$yend)) data.plotf<-data.plot[-rowrmv,] #Plotting art100k.1<-data.plotf%>% ggplot()+ geom_curve(aes(x = x, y = y, xend = xend, yend = yend, colour = factor(x)), curvature=0.2,alpha=0.2)+theme_void()+ scale_color_brewer(palette="Greys")+ theme(legend.position = "none",panel.background = element_rect(fill="#000000"))+ xlim(-1,10)+ylim(-1,10) plot(art100k.1)

I used a palette with limited number variants. Hence there was an error. But I don’t mind that. You can experiment with different colours.

Later, I edited the image output.

You can experiment further by changing the number or form of groups; even connect them to different points. Don’t forget to share with me what you created.

**leave a comment**for the author, please follow the link and comment on their blog:

**R | Asitav Sen**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.