• Stars
    star
    118
  • Rank 289,230 (Top 6 %)
  • Language
    R
  • License
    Other
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A particle simulation engine based on a port of d3-force

particles

R-CMD-check Codecov test coverage CRAN_Release_Badge CRAN_Download_Badge

This package implements the d3-force algorithm developed by Mike Bostock in R, thus providing a way to run many types of particle simulations using its versatile interface.

While the first goal is to provide feature parity with its JavaScript origin, the intentions is to add more forces, constraints, etc. down the line. While d3-force is most well-known as a layout engine for visualising networks, it is capable of much more. Therefore, particles is provided as a very open framework to play with. Eventually ggraph will provide some shortcut layouts based on particles with the aim of facilitating network visualisation.

Usage

particles builds upon the framework provided by tidygraph and adds a set of verbs that defines the simulation:

  • simulate() : Creates a simulation based on the input graph, global parameters, and a genesis function that sets up the initial conditions of the simulation.
  • wield() : Adds a force to the simulation. All forces implemented in d3-force are available as well as some additionals.
  • impose() : Adds a constraint to the simulation. This function is a departure from d3-force, as d3-force only allowed for simple fixing of x and/or y coordinates through the use of the fx and fy accessors. particles formalises the use of simulation constraints and adds new functionalities.
  • evolve() : Progresses the simulation, either a predefined number of steps, or until the simulated annealing has cooled down.

Example

A recreation of the Les Miserable network in https://bl.ocks.org/mbostock/4062045

library(tidyverse)
library(ggraph)
library(tidygraph)
library(particles)
# Data preparation
d3_col <- c(
  '0' = "#98df8a",
  '1' = "#1f77b4",
  '2' = "#aec7e8",
  '3' = "#ff7f0e",
  '4' = "#ffbb78",
  '5' = "#2ca02c",
  '6' = "#d62728",
  '7' = "#ff9896",
  '8' = "#9467bd",
  '9' = "#c5b0d5",
  '10' =  "#8c564b"
)

raw_data <- 'https://gist.githubusercontent.com/mbostock/4062045/raw/5916d145c8c048a6e3086915a6be464467391c62/miserables.json'
miserable_data <- jsonlite::read_json(raw_data, simplifyVector = TRUE)
miserable_data$nodes$group <- as.factor(miserable_data$nodes$group)
miserable_data$links <- miserable_data$links |>  
  mutate(from = match(source, miserable_data$nodes$id),
         to = match(target, miserable_data$nodes$id))

# Actual particles part
mis_graph <- miserable_data |> 
  simulate() |> 
  wield(link_force) |> 
  wield(manybody_force) |> 
  wield(center_force) |> 
  evolve() |> 
  as_tbl_graph()

# Plotting with ggraph
ggraph(mis_graph, 'nicely') + 
  geom_edge_link(aes(width = sqrt(value)), colour = '#999999', alpha = 0.6) + 
  geom_node_point(aes(fill = group), shape = 21, colour = 'white', size = 4, 
                  stroke = 1.5) + 
  scale_fill_manual('Group', values = d3_col) + 
  scale_edge_width('Value', range = c(0.5, 3)) + 
  coord_fixed() +
  theme_graph()
#> Warning: Existing variables `x`, `y` overwritten by layout variables

If you intend to follow the steps of the simulation it is possible to attach an event handler that gets called ofter each generation of the simulation. If the handler produces a plot the result will be an animation of the simulation:

# Random overlapping circles
graph <- as_tbl_graph(igraph::erdos.renyi.game(100, 0)) |> 
  mutate(x = runif(100) - 0.5, 
         y = runif(100) - 0.5, 
         radius = runif(100, min = 0.1, 0.2))

# Plotting function
graph_plot <- function(sim) {
  gr <- as_tbl_graph(sim)
  p <- ggraph(gr, layout = as_tibble(gr)) +
    geom_node_circle(aes(r = radius), fill = 'forestgreen', alpha = 0.5) + 
    coord_fixed(xlim = c(-2.5, 2.5), ylim = c(-2.5, 2.5)) + 
    theme_graph()
  plot(p)
}

# Simulation
graph %>% simulate(velocity_decay = 0.7, setup = predefined_genesis(x, y)) |> 
  wield(collision_force, radius = radius, n_iter = 2) |> 
  wield(x_force, x = 0, strength = 0.002) |> 
  wield(y_force, y = 0, strength = 0.002) |> 
  evolve(on_generation = graph_plot)

Click here for resulting animation (GitHub donā€™t allow big gifs in readme)

Installation

You can install particles from CRAN using install.packages("particles") or alternatively install the development version from github with:

# install.packages("devtools")
devtools::install_github("thomasp85/particles")

Immense Thanks

  • A huge ā€œThank Youā€ to Mike Bostock is in place. Without d3-force, particles wouldnā€™t exist and without d3 in general the world would be a sadder place.
  • The C++ quad tree implementation that powers manbody_force and collision_force is a modification of the implementation made by Andrei Kashcha and made available under MIT license. Big thanks to Andrei as well.

More Repositories

1

patchwork

The Composer of ggplots
R
2,354
star
2

gganimate

A Grammar of Animated Graphics
R
1,920
star
3

ggraph

Grammar of Graph Graphics
R
1,032
star
4

ggforce

Accelerating ggplot2
R
898
star
5

tidygraph

A tidy API for graph manipulation
R
529
star
6

ggplot2_workshop

Material for "Drawing Anything with ggplot2" workshop
481
star
7

lime

Local Interpretable Model-Agnostic Explanations (R port of original Python package)
R
478
star
8

scico

Palettes for R based on the Scientific Colour-Maps
R
401
star
9

tweenr

Interpolate your data
R
395
star
10

fiery

A flexible and lightweight web server
R
239
star
11

shinyFiles

A shiny extension for server side file access
JavaScript
188
star
12

ggfx

Filters and Shaders for 'ggplot2'
R
161
star
13

densityClust

Clustering by fast search and find of density peaks
R
149
star
14

transformr

Smooth Polygon Transformations
R
116
star
15

farver

High Performance Colourspace Manipulation in R
R
112
star
16

ambient

A Generator of Multidimensional Noise
R
91
star
17

euclid

Exact Computation Geometry Framework Based on 'CGAL'
C++
82
star
18

hierarchicalSets

Scalable Set Visualization using Hierarchies
R
54
star
19

Hr

Easy Access to Uppercase H
R
53
star
20

routr

Routing of Web Requests in R
R
53
star
21

reqres

Powerful classes for http requests and responses
R
36
star
22

curry

Partial Function Application with %<%, %-<%, and %><%
R
30
star
23

FindMyFriends

Fast alignment-free pangenome creation and exploration
R
27
star
24

pipeplotter

Syntactic ggplot2 Sugar for a Tidy World
R
26
star
25

fawkes

An R Interface to the AxiDraw plotter
R
25
star
26

pearls

Operations on Lists of Data Frames
R
18
star
27

ink

The Modern, High-Performant, Graphic Device for R
C++
17
star
28

PanVizGenerator

Create your own PanViz visualizations
R
17
star
29

ggplot2_mechanics

The Mechanics of ggplot2
TeX
16
star
30

plotting_benchmark

Investigating R graphics performance
HTML
16
star
31

grid

personal devel version of grid
R
15
star
32

PanViz

D3 based visualisation for comparative genomics
JavaScript
14
star
33

boundaries

Algorithms for Working With and Modifying Polygon Boundaries
C++
13
star
34

data_imaginist

data_imaginist source
HTML
12
star
35

nanodev

Graphic Devices for R based on NanoVG
C
10
star
36

MSGFgui

A gui overlay and extension for MSGFplus
R
10
star
37

orion

Spatial Searching for Euclid
C++
9
star
38

web_dev_in_R

Web Development for R Users
TeX
9
star
39

heroku-fiery-demo

A demo fiery application for deployment on Heroku
R
8
star
40

marquee

Markdown Parser and Renderer for R Graphics
C
8
star
41

unmeshy

A Vector Based 3D Renderer
C++
7
star
42

MSsary

Mass spectrometry data in R
R
7
star
43

RcppSNAP

'Rcpp' Integration for the SNAP Network Library
C++
6
star
44

tidy_graph_analysis

Tidy Network Analysis in R
TeX
6
star
45

polyclid

Polygon Support for Euclid
C++
6
star
46

mzID

An mzIdentML parser for R
R
6
star
47

MSGFplus

An MSGF+ interface for R
R
6
star
48

thomasp85.github.io

The source for data-imaginist.com
HTML
6
star
49

mvpcran

CRAN on a stick
R
5
star
50

shady

Compile and Execute Shaders from R
C++
5
star
51

anomaly

Detecting those outliers
R
4
star
52

d3Disco

A showcase for Shiny and D3 integration
JavaScript
4
star
53

firedock

Dockerfiles for fiery
R
4
star
54

phd_dissertation

Pangenome Tools for Rapid, Large-Scale Analysis of Bacterial Genomes
TeX
4
star
55

pepmaps

R package for quantitative peptidomics
R
2
star
56

masochist

For some reason Iā€™m doing all of this from my phone
R
2
star
57

firedock_test

R
1
star
58

firesafety

Security for fiery apps
1
star
59

Biotools

Scripts for CMG-Biotools
Perl
1
star
60

circosScripts

Perl scripts to automate circos plots
Perl
1
star
61

CHtools

A list of diverse functions for CH
R
1
star