set.seed(1)
g <- sample_islands(3, 10, 5/10, 1)
i <- membership(cluster_louvain(g))
(graphjs(g, vertex.color=c("orange", "green", "blue")[i],
vertex.shape="sphere"))
# similar example with user-defined directional lighting
l1 <- light_directional(color="red", position=c(0, -0.8, 0.5))
l2 <- light_directional(color="yellow", position=c(0, 0.8, -0.5))
l3 <- light_ambient(color="#555555")
(graphjs(g, vertex.color="gray", vertex.shape="sphere",
lights=list(l1, l2, l3)))
# Les Miserables Character Co-appearance Data
data("LeMis")
(graphjs(LeMis))
# Use HTML and CSS directly in each vertex label to customize
# and align the legend:
(graphjs(LeMis,
vertex.label=sprintf("%s",
V(LeMis)$label)))
# The plot legend 'div' element is of CSS class 'infobox'. Use JavaScript
# to customize labels to hover near the mouse pointer:
program <- "document.addEventListener('mousemove', function(e) {
e.preventDefault();
let x = document.getElementsByClassName('infobox')[0];
x.style['background'] = '#00c9c2';
x.style['border-radius'] = '5px';
x.style['color'] = '#222';
x.style['font-family'] = 'sans-serif';
x.style['position'] = 'absolute';
x.style['top'] = e.pageY + 'px';
x.style['left'] = e.pageX + 'px';
})"
(graphjs(LeMis, program = program))
# ...plot Character names
(graphjs(LeMis, vertex.shape=V(LeMis)$label, vertex.size=0.3))
# SNAP Facebook ego network dataset
data("ego")
(graphjs(ego, bg="black"))
if (FALSE) {
# A shiny example
shiny::runApp(system.file("examples/graph", package="threejs"))
# A graph amination that shows several layouts
data("LeMis")
graphjs(LeMis,
layout=list(
layout_randomly(LeMis, dim=3),
layout_on_sphere(LeMis),
layout_with_drl(LeMis, dim=3), # note! somewhat slow...
layout_with_fr(LeMis, dim=3, niter=30)),
main=list("random layout", "sphere layout", "drl layout", "fr layout"),
fpl=300)
# A simple graph animation illustrating edge modification
g <- make_ring(5) - edges(1:5)
graph_list <- list(
g + edge(1, 2),
g + edge(1, 2) + edge(2, 3),
g + edge(1, 2) + edge(2, 3) + edge(3, 4),
g + edge(1, 2) + edge(2, 3) + edge(3, 4) + edge(4, 5),
g + edge(1, 2) + edge(2, 3) + edge(3, 4) + edge(4, 5) + edge(5, 1))
graphjs(graph_list, main=paste(1:5),
vertex.color=rainbow(5), vertex.shape="sphere", edge.width=3)
# see `demo(package="threejs") for more animation demos.
# A crosstalk example
library(crosstalk)
library(DT)
data(LeMis)
sd = SharedData$new(data.frame(Name = V(LeMis)$label))
print(bscols(
graphjs(LeMis, brush=TRUE, crosstalk=sd),
datatable(sd, rownames=FALSE, options=list(dom='tp'))
))
}
Run the code above in your browser using DataLab