Eric Lewis, Yanna Chen and Ricky Hardiyanto
We will present some visualization projects by mixing programming, mathematics and experimentation to showcase some artistic creations that can be made with R.
What exactly are contour projections, surface plots, heat maps?
Heat maps are graphs where a matrix’s individual values are denoted by colors. It is a 2D representation that uses colors to help explain relationships of data that would be easier to decipher than looking at a spreadsheet equivalent. We will implement several functions to show these images.
What are fractals? Fractals are geometric figures that are self-similar across all of its scalar levels. That is to say that a smaller part of a fractal at different scales usually resembles the whole fractal. We will implement several well-known recursive functions and dynamical systems to demonstrate fractals.
Our first goal is to make digital art. To do this we make a function z(X,Y) where we assign the independent variables X and Y with their corresponding vectors of possible values that we choose. Using the function z, we will generate the surface plots and heat maps which we will then overlay with our contour projections of two-dimensional surfaces.
Our second goal is to make fractals without using the built-in R functions. To do this we will explain how to make a basic well-known fractal; The Sierpinksi Carpet. To make the Sierpinski Carpet we will construct a matrix:
After making this matrix, the idea will be to make a for-loop that can manipulate this matrix in such a way that when the recursive property is in-effect, each element A is treated just as the initial matrix f(A) has been treated.
Here we initialize the surface z to show the 12 points we will sample from.
library("plot3D") #required library for plots
x <- c(1,2,3,4) #vector of x-values
y <- c(-1,0,1) #vector of y-values
z <- 2*x + 5*y #given function
M <- mesh(x,y) #creates a rectangular full 2D or 3D grid
(zz <- 2*M$x + 5*M$y) #Mesh in tandem with X & Y to get our 12 points
## [,1] [,2] [,3]
## [1,] -3 2 7
## [2,] -1 4 9
## [3,] 1 6 11
## [4,] 3 8 13
plot(M$x,M$y, xlim=c(0.5,4.5), ylim=c(-1.5,1.5), pch = 16, xlab="x-values", ylab="y-values")
This is the plot of the actual surface we generate from the given vectors of X and Y.
persp3D( M$x, M$y, zz,colvar=zz,colkey=FALSE,shade = 0.1,box=TRUE,theta=320) #plotting the surface
This is the corresonding heat map of the matrix \(zz\). As we can see colors are assigned to each number from the matrix to produce this heat map. Colors that are close to each other are indicative of close numbers.
## [,1] [,2] [,3]
## [1,] -3 2 7
## [2,] -1 4 9
## [3,] 1 6 11
## [4,] 3 8 13
image(zz, axes=FALSE)
Now that we made a heat map in 2D we are going to up the ante and the dimension by one. We will be using the function \(z = cos(x^{2}+y^{2})*e^{\frac{-1}{\pi}\sqrt{x^{2}+y^{2}}}\) as our given function. Repeating the same steps as above we would generate a 3D heat map. To get a better descriptive look at the graphical interpretation of this function we must set the independent variables to all the variables within its domain. Since we are dealing with trig functions the domain is \(-2\pi \ to \ 2\pi\).
We will now use our proven methods our gerenating heat maps and contour plots to generate digital art. Our new function is: \[z=cos(x^2+y^2)e^{\frac{-1}{\pi}\sqrt{x^2+y^2}}\] We will sample 27 points from the interval \((-4\pi, 4\pi)\).
We show the aerial view of the function now added with contour plots to finish our digital art masterpiece.
image2D(zz, axes=FALSE)
contour2D(zz,add=TRUE,colkey=FALSE,drawlabels=FALSE,axes=FALSE,frame=TRUE)
We repeat the same steps as before this time using \(image()\) to plot the heat map. We also use \(contour()\) to superimpose the contour plot of \(z\). Our new function is: \[z=tan(x^2+y^2)e^{\frac{-1}{\pi^2}\sqrt{x^2+y^2}}\] We will sample 27 points from the interval \((-7\pi, 7\pi)\).
image(zz,axes=FALSE)
contour(zz,add=TRUE,colkey=FALSE,drawlabels=FALSE,axes=FALSE,frame=TRUE)
To make the Sierpinksi Carpet we must first generate a matrix f(A).
An effective way to do this is with binding. R has built-in functions to bind elements by row and by column. We take advantage of this by using a couple of binds in quick-succesion. First we bind 3 copies of the element \(A\) by column to form a row of 3 elements of A. That will be our top row of our desired matrix. The middle element in the second row will eventually be our “hole” that we see through each of the iterations. To get the second row with a zero in the middle we do the same bind except that we multiply the middle element by 0. It must be multiplied and not just inputted as zero because if the element is a matrix, the act of multipliying by zero will count as multiplying by the zero matrix.
We will first generate the matrix above which is the same as a Level 1 Sierpinski Carpet.
Now that we have the Level 1 Carpet we must add a \(for-loop\) that will allow us to add the recursion that will show the self-similar property that fractals are known for.
Level 6 Sierpinski Carpet:
As you can see we manipulate recursion by changing the number of iteration in the \(for-loop\). Now that we know our function is reliable in finding fractals we get creative by manipulating the initial matrix \(f(A)\) solely to get various fractals.
Level 6 Sierpinski Triangle & Level 5 Sierpinski Snowflake
Fractals can also be created by simulating different kinds of dynamical systems.
Here we use Euler’s Method to simulate chaotic dynamical systems.
library(rgl)
library(knitr)
knit_hooks$set(webgl = hook_webgl)
LiSys <- function(n, a=5, b=16, c=1, x0=5, y0=4, z0=10){
x<-c(x0,rep(NA,n-1))
y<-c(y0,rep(NA,n-1))
z<-c(z0,rep(NA,n-1))
h<-0.01
for (i in 2:n){
x[i] = x[i-1] + h*(a*(y[i-1]-x[i-1]))
y[i] = y[i-1] + h*(x[i-1]*z[i-1] - y[i-1])
z[i] = z[i-1] + h*(b - x[i-1]*y[i-1] - c*z[i-1])
}
require(rgl)
rgl.clear()
rgl.points(x,y,z, color=heat.colors(n), size=2)
}
LiSys(20000)
You must enable Javascript to view this page properly.
library(rglwidget)
ThoSys <- function(n, b=0.209, x0=0.5, y0=0.4, z0=0.8){
x<-c(x0,rep(NA,n-1))
y<-c(y0,rep(NA,n-1))
z<-c(z0,rep(NA,n-1))
h<-0.01
for (i in 2:n){
x[i] = x[i-1] + h*(sin(y[i-1]) - b*x[i-1])
y[i] = y[i-1] + h*(sin(z[i-1]) - b*y[i-1])
z[i] = z[i-1] + h*(sin(x[i-1]) - b*z[i-1])
}
require(rgl)
rgl.clear()
rgl.points(x,y,z, color=heat.colors(n), size=2)
rglwidget(elementId="ThoSys3D")
}
ThoSys(20000)
The Fractal Dragon has many names, The Dragon Curve, The Heighway Dragon or even The Jurassic Park Fractal. It is very difficult to code but the concept is very easy to understand.
Level 2
Level 3
R is mainly known for its value in statistics and its number crunching abilities. However it is not well known that R is a software that you can use to make digital art. We used mathematics to show heat maps and fractals, both with ubiquitous application in the world. We have proved that R is a viable way to show amazing artistic creations.