In [1]:
require(fastcluster) 
library(TDAmapper)
library(devtools)
library(igraph)


Loading required package: fastcluster

Attaching package: ‘fastcluster’

The following object is masked from ‘package:stats’:

    hclust


Attaching package: ‘igraph’

The following objects are masked from ‘package:stats’:

    decompose, spectrum

The following object is masked from ‘package:base’:

    union


In [15]:
# sample points from two intertwined spirals
set.seed("1")
t <- runif(100, min=1, max=6.3) # theta
X <- data.frame( x = c( t*cos(t), -t*cos(t) ), y = c( t*sin(t), -t*sin(t) ) )
d <- dist(X)
plot(X[,1], X[,2])
X


xy
-1.7867054 1.6131571
-2.9297442 0.5009081
-2.5261500-3.1478332
5.1839651-2.6312171
-0.9884714 1.8175067
4.9949788-2.8713532
5.7787757-1.6392512
-0.9392416-4.4031676
-1.5999694-4.0281873
0.3198333 1.2883617
-1.0408838 1.8142835
-0.6908825 1.8082623
-0.3300272-4.6294725
-3.0187612 0.3207131
1.8265000-4.7404555
-3.1990564-1.7319558
0.4364523-4.7835082
6.2549740-0.1631855
-2.9897559 0.3829880
2.0319990-4.7000093
5.6341273-1.9250933
-1.1168120 1.8071017
-1.1386236-4.3058684
-0.1573917 1.6579882
-1.8080585 1.6028983
-3.0326138 0.2895433
0.5132877 0.9399520
-3.0066865 0.3471078
4.3833873-3.5001217
-2.6454461 0.9290792
2.6327364 -0.94466515
-3.6606962 4.03624496
2.7071729 -0.84981239
2.5790235 -1.00798719
3.2692009 1.31740832
-4.8701889 3.01640580
-4.2607135 3.60470140
3.0584035 -0.22872894
-2.0286352 4.70074337
-5.9794502 1.16181393
3.2603842 0.53319531
-0.3051847 4.76656790
3.1192408 -0.06745629
2.4906606 -1.10398466
-1.4821413 4.78842708
1.0007729 -1.81687791
-0.2695566 4.76131826
0.1218968 -1.64044451
1.5350285 -1.71426333
0.3300824 -1.72827438
1.4610766 -1.73733077
-0.3354066 -1.26876740
1.3362224 4.19652847
-4.5307186 3.36598952
-2.0716875 4.69116579
-2.5663399 4.55216701
3.2880634 0.91482131
3.1718359 0.10106661
-2.9263244 4.41603003
2.0395373 3.67858063

In [9]:
filter <- X[,2] # height projection
num_intervals <- 10
percent_overlap <- 50
num_bins_when_clustering <- 10

m3 <- mapper1D(
	distance_matrix = d, 
	filter_values = filter,	
	# num_intervals = 10, # use default
	# percent_overlap = 50, # use default
	# num_bins_when_clustering = 10 # use default
	)
    
g3 <- graph.adjacency(m3$adjacency, mode="undirected")
plot(g3, layout = layout.auto(g3) )



In [10]:
# parametrize a trefoil knot
n <- 100
t <- 2*pi*(1:n)/n
X <- data.frame(x = sin(t)+2*sin(2*t),
                y = cos(t)-2*cos(2*t),
                z = -sin(3*t))
f <- X

library(rgl)
plot3d(X$x, X$y, X$z)


Error in library(rgl): there is no package called ‘rgl’
Traceback:

1. library(rgl)
2. stop(txt, domain = NA)

In [13]:
# use the github version so that vertices stay on the canvas
library(devtools)
devtools::install_github("christophergandrud/networkD3")
library(networkD3)


Downloading GitHub repo christophergandrud/networkD3@master
from URL https://api.github.com/repos/christophergandrud/networkD3/zipball/master
Installing networkD3
'/usr/lib/R/bin/R' --no-site-file --no-environ --no-save --no-restore --quiet  \
  CMD INSTALL  \
  '/tmp/RtmpYukpUP/devtools514f28325bd1/christophergandrud-networkD3-1d217fb'  \
  --library='/home/raula-k/R/x86_64-pc-linux-gnu-library/3.2' --install-tests 


In [14]:
# parametrize a trefoil knot
n <- 100
t <- 2*pi*(1:n)/n
X <- data.frame(x = sin(t)+2*sin(2*t),
                y = cos(t)-2*cos(2*t),
                z = -sin(3*t))
f <- X

m5 <- mapper(dist(X), f, c(3,3,3), c(30,30,30), 5)
g5 <- graph.adjacency(m5$adjacency, mode="undirected")
plot(g5, layout = layout.auto(g5) )
tkplot(g5)

# create data frames for vertices and edges with the right variable names 
MapperNodes <- mapperVertices(m5, 1:dim(f)[1] )
MapperLinks <- mapperEdges(m5)

# interactive plot
forceNetwork(Nodes = MapperNodes, Links = MapperLinks, 
            Source = "Linksource", Target = "Linktarget",
            Value = "Linkvalue", NodeID = "Nodename",
            Group = "Nodegroup", opacity = 0.8, 
            linkDistance = 10, charge = -400)


1

In [ ]: