particles

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

https://github.com/thomasp85/particles

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (17.5%) to scientific vocabulary

Keywords

d3js graph-layout network network-visualization particles rstats simulation
Last synced: 6 months ago · JSON representation

Repository

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

Basic Info
  • Host: GitHub
  • Owner: thomasp85
  • License: other
  • Language: R
  • Default Branch: main
  • Size: 20.7 MB
Statistics
  • Stars: 120
  • Watchers: 8
  • Forks: 9
  • Open Issues: 3
  • Releases: 3
Topics
d3js graph-layout network network-visualization particles rstats simulation
Created over 8 years ago · Last pushed about 1 year ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  ffmpeg.format='gif',
  interval = 1/15
)
```

# particles 


[![R-CMD-check](https://github.com/thomasp85/particles/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/thomasp85/particles/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/thomasp85/particles/branch/main/graph/badge.svg)](https://app.codecov.io/gh/thomasp85/particles?branch=main)
[![CRAN_Release_Badge](http://www.r-pkg.org/badges/version-ago/particles)](https://CRAN.R-project.org/package=particles)
[![CRAN_Download_Badge](http://cranlogs.r-pkg.org/badges/particles)](https://CRAN.R-project.org/package=particles)
[![Codecov test coverage](https://codecov.io/gh/thomasp85/particles/graph/badge.svg)](https://app.codecov.io/gh/thomasp85/particles)


This package implements the [d3-force](https://github.com/d3/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`](https://github.com/thomasp85/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`](https://github.com/thomasp85/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 

```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(ggraph)
library(tidygraph)
library(particles)
```

```{r}
# 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()
```

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:

```{r, eval=FALSE}
# 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](https://www.dropbox.com/s/c5fta49hk53ku0g/bubbles.gif?raw=1)
(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:

```{r gh-installation, eval = FALSE}
# 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](https://github.com/anvaka/quadtree.cc) 
  and made available under MIT license. Big thanks to Andrei as well.

Owner

  • Name: Thomas Lin Pedersen
  • Login: thomasp85
  • Kind: user
  • Location: Copenhagen
  • Company: @posit-pbc, part of @tidyverse team

Maker of tools focusing on data science and data visualisation

GitHub Events

Total
  • Watch event: 1
  • Push event: 9
Last Year
  • Watch event: 1
  • Push event: 9

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 124
  • Total Committers: 2
  • Avg Commits per committer: 62.0
  • Development Distribution Score (DDS): 0.016
Past Year
  • Commits: 10
  • Committers: 1
  • Avg Commits per committer: 10.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Thomas Lin Pedersen t****5@g****m 122
Silvio Waschina 3****a 2

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 10
  • Total pull requests: 2
  • Average time to close issues: about 1 year
  • Average time to close pull requests: about 1 month
  • Total issue authors: 9
  • Total pull request authors: 1
  • Average comments per issue: 2.7
  • Average comments per pull request: 1.5
  • Merged pull requests: 2
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • thomasp85 (2)
  • nfarmer01 (1)
  • jwretham (1)
  • PaulLantos (1)
  • batpigandme (1)
  • Lauler (1)
  • ncchung (1)
  • matthewhirschey (1)
  • loukesio (1)
Pull Request Authors
  • Waschina (2)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 237 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 1
  • Total versions: 4
  • Total maintainers: 1
cran.r-project.org: particles

A Graph Based Particle Simulator Based on D3-Force

  • Versions: 4
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 237 Last month
Rankings
Stargazers count: 3.4%
Forks count: 7.4%
Average: 23.6%
Dependent repos count: 24.4%
Dependent packages count: 28.0%
Downloads: 54.6%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action v4.5.0 composite
  • actions/checkout v4 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pr-commands.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/pr-fetch v2 composite
  • r-lib/actions/pr-push v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • actions/checkout v4 composite
  • actions/upload-artifact v4 composite
  • codecov/codecov-action v4 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION cran
  • digest * imports
  • dplyr * imports
  • igraph * imports
  • mgcv * imports
  • rlang * imports
  • stats * imports
  • tidygraph * imports
  • covr * suggests
  • ggraph * suggests
  • knitr * suggests
  • rmarkdown * suggests