# The Breathtaking 1-Matrix

La luna sale a caminar siguiendo tus pupilas (Ojos color sol, Calle 13)

This is a 5×5 1-matrix:

$\begin{bmatrix} 1 &1 &1 &1 &1 \\ 1 &1 &1 &1 &1 \\ 1 &1 &1 &1 &1 \\ 1 &1 &1 &1 &1 \\ 1 &1 &1 &1 &1 \end{bmatrix}$

And this is a 20×20 1-matrix visualized:

Maybe in some other galaxy, aliens represent matrix in this way.

par(mar = c(1, 1, 1, 1), bg="violetred4")
circlize::chordDiagram(matrix(1, 20, 20),
col="white",
symmetric = TRUE,
transparency = 0.85,
annotationTrack = NULL)


# Discovering Shiny

It is not an experiment if you know it is going to work (Jeff Bezos)

From time to time, I discover some of my experiments translated into Shiny Apps, like this one. Some days ago, I discovered one of these translations and I contacted the author, who was a guy from Vietnam called Vu Anh. I asked him to do a Shiny App from this experiment. Vu was enthusiastic with the idea. We defined some parameters to play with shape, number, width and alpha of lines as well as background color and I received a perfect release of the application in just a few hours.With just a handful of parameters, possible outputs are almost infinite. Following you can find some of them:

I think the code is a nice example to take the first steps in Shiny. If you are not used to Markdown files, you can follow this instructions to run the code.

Vu is a talented guy, who loves maths and programming. He represents the future of our nice profession and I predict a successful future for him. Do not miss his brand new blog. I am sure you will find amazing things there.

This is the code of the app:

---
title: "Maths, Music and Merkbar"
author: "Brother Rain"
date: "18/03/2015"
output: html_document
runtime: shiny
---

Original Post: https://aschinchon.wordpress.com/2015/01/15/maths-music-and-merkbar/

{r}
library(circlize)
library(scales)
factors = as.factor(0:9)
lines = 2000 #Number of lines to plot in the graph
alpha = 0.4  #Alpha for color lines
colors0=c(
rgb(239,143,121, max=255),
rgb(126,240,188, max=255),
rgb(111,228,235, max=255),
rgb(127,209,249, max=255),
rgb( 74,106,181, max=255),
rgb(114,100,188, max=255),
rgb(181,116,234, max=255),
rgb(226,135,228, max=255),
rgb(239,136,192, max=255),
rgb(233,134,152, max=255)
)
# You can find the txt file here:


## Visualization

{r, echo=FALSE}
fluidPage(
fluidRow(
column(width = 4,
sidebarPanel(
sliderInput("lines", "Number of lines:", min=100, max=100000, step=100, value=500),
sliderInput("alpha", "Alpha:", min=0.01, max=1, step=0.01, value=0.4),
sliderInput("lwd", "Line width", min=0, max=1, step=0.05, value=0.2),
selectInput("background", "Background:",
c("Purple" = "mediumpurple4", "Gray" = "gray25", "Orange"="orangered4",
"Violet"="palevioletred4", "Green"="forestgreen", "Pink"="deeppink"), selected="Purple"),
sliderInput("h0", "h0:", min=0, max=0.4,
step=0.0005, value=0.1375),
sliderInput("h1", "h1:", min=0, max=0.4,
step=0.0005, value=0.1125),
width=12
)
),
column(width = 8,
renderPlot({
# get data
phi=gsub("\\.","", substr(phi,1,input$lines)) phi=gsub("\\.","", phi) position=1/(nchar(phi)-1) # create circos circos.clear() par(mar = c(1, 1, 1, 1), lwd = 0.1, cex = 0.7, bg=alpha(input$background, 1))
circos.par(
"track.height" = 0.025,
"gap.degree" = 3
)
circos.initialize(factors = factors, xlim = c(0, 1))
circos.trackPlotRegion(factors = factors, ylim = c(0, 1))
## create first region
for (i in 0:9) {
circos.updatePlotRegion(
sector.index = as.character(i),
bg.col = alpha(input$background, 1), bg.border=alpha(colors0[i+1], 1) ) } for (i in 1:(nchar(phi)-1)) { m=min(as.numeric(substr(phi, i, i)), as.numeric(substr(phi, i+1, i+1))) M=max(as.numeric(substr(phi, i, i)), as.numeric(substr(phi, i+1, i+1))) d=min((M-m),((m+10)-M)) col=t(col2rgb(colors0[(as.numeric(substr(phi, i, i))+1)])) for(index in 1:3){ col[index] = max(min(255, col[index]), 0) } if (d>0) { circos.link( substr(phi, i, i), position*(i-1), substr(phi, i+1, i+1), position*i, h = input$h0 * d + input$h1, lwd=input$lwd,
col=alpha(rgb(col, max=255), input\$alpha), rou = 0.92
)
}
}
}, width=600, height=600, res=192)
)
)
)




# Maths, Music and Merkbar

Control is what we already know. Control is where we have already ventured. Control is what helps us predict the future. (Merkbar)

Maths and music get along very well. Last December I received a mail from a guy called Jesper. He is one of the two members of Merkbar: a electronic music band from Denmark. As can be read in their website:

Merkbar is Jesper and David who are both interested in the psychedelic worlds and oriental spiritualism. They both studied Computer Music, where they’ve done research in sound synthesis, generative composition and the design of new digital instrument.

They asked me a front cover for their new album which will be released at the beginning of 2015. Why? Because they liked this post I did about circlizing numbers.  To do this plot I circlized the Golden Ratio number (Phi). But in this case I changed ribbons (all equal pairs of consecutive numbers gather together) by lines (every pair of consecutive numbers form a different line). As I did before, I used circlize package, which implements in R the features of Circos, a software to create stunning circular visualizations.

The final plot represents the first 2.000 digits of Phi:

You can hear an advancement of their new album here, which is called “Phi”. Enjoy their sensitive and full-of-shades music: you will be delightfully surprised as I was.

This is the code to circlize Phi:

library(circlize)
library(scales)
factors = as.factor(0:9)
lines = 2000 #Number of lines to plot in the graph
alpha = 0.4  #Alpha for color lines
colors0=c(rgb(239,143,121, max=255), rgb(126,240,188, max=255), rgb(111,228,235, max=255),
rgb(127,209,249, max=255), rgb( 74,106,181, max=255), rgb(114,100,188, max=255),
rgb(181,116,234, max=255), rgb(226,135,228, max=255), rgb(239,136,192, max=255),
rgb(233,134,152, max=255))
#You can find the txt file here: http://www.goldennumber.net/wp-content/uploads/2012/06/Phi-To-100000-Places.txt
phi=gsub("\\.","", substr(phi,1,lines))
phi=gsub("\\.","", phi)
position=1/(nchar(phi)-1)
#This code generates a pdf file in your working directory
pdf(file="CirclizePhi.pdf", width=25, height=25)
circos.clear()
par(mar = c(1, 1, 1, 1), lwd = 0.1, cex = 0.7, bg=alpha("black", 1))
circos.par("cell.padding"=c(0.01,0.01), "track.height" = 0.025, "gap.degree" = 3)
circos.initialize(factors = factors, xlim = c(0, 1))
circos.trackPlotRegion(factors = factors, ylim = c(0, 1))
for (i in 0:9) {circos.updatePlotRegion(sector.index = as.character(i), bg.col = alpha("black", 1), bg.border=alpha(colors0[i+1], 1))}
for (i in 1:(nchar(phi)-1)) {
m=min(as.numeric(substr(phi, i, i)), as.numeric(substr(phi, i+1, i+1)))
M=max(as.numeric(substr(phi, i, i)), as.numeric(substr(phi, i+1, i+1)))
d=min((M-m),((m+10)-M))
col=t(col2rgb(colors0[(as.numeric(substr(phi, i, i))+1)]))
if (col[1]>255) col[1]=255;if (col[2]>255) col[2]=255;if (col[3]>255) col[3]=255
if (col[1]<0) col[1]=0;if (col[2]<0) col[2]=0;if (col[3]<0) col[3]=0
if (d>0) circos.link(substr(phi, i, i), position*(i-1), substr(phi, i+1, i+1), position*i, h = 0.1375*d+0.1125, lwd=0, col=alpha(rgb(col, max=255), alpha), rou = 0.92)
}
dev.off()


# Circlizing Numbers

She makes the sound the sea makes to calm me down (Dissolve Me, Alt-J)

Searching how to do draw chord diagrams in the Internet with ggplot2 I found a very-easy-to-use package called circlize which does exactly that. A chord diagram shows relationships between things so the input to draw it is simply a matrix with the intensity of these relations. In this experiment I use this package to circlize numbers in this way:

• I take a number with many digits (Rmpfr package is very useful to obtain large numbers), I convert it to text and remove punctuation characters (necessary if number has decimals)
• Function CreateAdjacencyMatrix creates a 10×10 matrix where the element [i,j] contains the number of times that number “i” precedes to number “j” in the previous string (i and j from 0 to 9); this is the input to create diagram.

These diagrams are the result of circlizing four famous constants: Pi (green), Gamma (purple), Catalan (blue) and Logarithm constants (red):

Just two conclusions of my own to end:

• Circlize package is very easy to use and generates very nice diagrams
• Chord diagrams remember me to dreamcatchers
• The more I use RColorBrewer package the more I like it

This is the code to circlize numbers:

library(Rmpfr)
library(circlize)
library(RColorBrewer)
s=gsub("\\.", "", x)
m=matrix(0, 10, 10)
for (i in 1:(nchar(s)-1)) m[as.numeric(substr(s, i, i))+1, as.numeric(substr(s, i+1, i+1))+1]=m[as.numeric(substr(s, i, i))+1, as.numeric(substr(s, i+1, i+1))+1]+1
rownames(m) = 0:9;colnames(m) = 0:9
m}
jpeg(filename = "Chords.jpg", width = 800, height = 800, quality = 100)
par(mfrow=c(2,2), mar = c(1, 1, 1, 1))
chordDiagram(m1, grid.col = "darkgreen",
col = colorRamp2(quantile(m1, seq(0, 1, by = 0.25)), brewer.pal(5,"Greens")),
transparency = 0.4, annotationTrack = c("name", "grid"))
chordDiagram(m2, grid.col = "mediumpurple4",
col = colorRamp2(quantile(m2, seq(0, 1, by = 0.25)), brewer.pal(5,"Purples")),
transparency = 0.4, annotationTrack = c("name", "grid"))
chordDiagram(m3, grid.col = "midnightblue",
col = colorRamp2(quantile(m3, seq(0, 1, by = 0.25)), brewer.pal(5,"Blues")),
transparency = 0.4, annotationTrack = c("name", "grid"))
chordDiagram(m4, grid.col = "red3",
col = colorRamp2(quantile(m4, seq(0, 1, by = 0.25)), brewer.pal(5,"Reds")),
transparency = 0.4, annotationTrack = c("name", "grid"))
dev.off()