if(require("TDAstats") & require("igraph"))
{
# simulate data from the unit circle and calculate
# its diagram
df <- TDAstats::circle2d[sample(1:100,25),]
diag <- TDAstats::calculate_homology(df,
dim = 1,
threshold = 2)
# get minimum death radius of any data cluster
min_death_H0 <-
min(diag[which(diag[,1] == 0),3L])
# get birth and death radius of the loop
loop_birth <- as.numeric(diag[nrow(diag),2L])
loop_death <- as.numeric(diag[nrow(diag),3L])
# compute VR graphs at radii half of
# min_death_H0 and the mean of loop_birth and
# loop_death, returning clusters
graphs <- vr_graphs(X = df,eps =
c(0.5*min_death_H0,(loop_birth + loop_death)/2))
# plot graph of smaller (first) radius
plot_vr_graph(graphs = graphs,eps = 0.5*min_death_H0,
plot_isolated_vertices = TRUE)
# plot graph of larger (second) radius
plot_vr_graph(graphs = graphs,eps = (loop_birth + loop_death)/2)
# repeat but with rownames for df, each vertex
# will be plotted with its rownames
rownames(df) <- paste0("V",1:25)
graphs <- vr_graphs(X = df,eps =
c(0.5*min_death_H0,(loop_birth + loop_death)/2))
plot_vr_graph(graphs = graphs,eps = 0.5*min_death_H0,
plot_isolated_vertices = TRUE)
# plot without vertex labels
plot_vr_graph(graphs = graphs,eps = (loop_birth + loop_death)/2,
vertex_labels = FALSE)
# plot only the graph component containing vertex "1"
plot_vr_graph(graphs = graphs,eps = 0.5*min_death_H0,
component_of = "V1",plot_isolated_vertices = TRUE)
# save the layout of the graph for adding features to
# the same graph layout, like color
layout <- plot_vr_graph(graphs = graphs,eps = (loop_birth + loop_death)/2,
return_layout = TRUE,vertex_labels = TRUE)
cols <- rep("blue",25)
cols[1:5] <- "red"
plot_vr_graph(graphs = graphs,eps = (loop_birth + loop_death)/2,cols = cols,
layout = layout)
}
Run the code above in your browser using DataLab