A Minimal Example


library(cytoscape)


nodes <- data.frame(id = c('a','b'))
edges <- data.frame(id = 'ab', source = 'a', target = 'b')

cytoscape(nodes = nodes, edges = edges) 

Styling Edges and Nodes

Nodes and edges can all be styled using CSS.


ct <- cytoscape(nodes = nodes, edges = edges)

ct %>% node_style('background-color' = '#ff0000')

ct %>% edge_style('line-color' = '#ff0000')

Creating data

Every network chart requires two data.frames one for the nodes and another for the edges. Often you will already have a data.frame with the edge, or source, target. In the example below we have a data.frame of Comtrade waste plastic exports for December 2017 where the source counrty and the destination country are deliniated. These fields need to be renamed to source and target for cytoscape to use it. For the nodes data.frame you need to create an id field that contains all of the unique values found in the source and target fields of the edges data.frame.



df <- cytoscape::comtrade

edges <- df %>%
    dplyr::select(source = reporter,
                  target = partner) %>%
    dplyr::mutate(id = paste(source, '_', target))

nodes <- data.frame(id = unique(c(edges$source, edges$target)), stringsAsFactors = FALSE)
           
cytoscape(nodes = nodes, edges = edges) %>% 
  layout('grid', rows = 4)

With Geographic Location

You can use coordinates to fix the location of nodes. But the package is not spatially enabled and does not check or transform the coordinates you pass.

Image Node Example

Replicate the Cytograph.js images example. This example does not currently include the touch effects.

Node Gradient Background

Since cytoscape version 3.3.0 it has been possible to include background gradients in node styles. This is included in this package as of version 0.0.2.

Grouping Nodes

Compound graphs where nodes are grouped are also possible. Add parent and node_color columns to the nodes data.frame. The final step is to add nodes for each of the groups, note the bind_rows() function in the example below. Thanks to gilhornung for this example.

Cytoscape-Cola Layout

Constraint based layouts provided through (cola.js) can be used via the cytoscape-cola plugin. All options available in the plugin API are available but have not yet been tested.

Basic Cola Layout


cytoscape(nodes = nodes, edges = edges) %>% 
  cola_layout()

Cola Node Allignment

To use the Cola alignment variable you need to pass a javascript object or function. You can do this using htmltools::JS().

Shiny Example

A minimal shiny example can be run from the cytoscape package of from inst/shiny/minimum_shiny


shiny::runApp(system.file('shiny/minimum_shiny', package = 'cytoscape'))

Pass JSON Object

If you already have a complete JSON this can be passed through directly as a character string. The JSON character is parsed in javascript using JSON.parse() so it is good practice to first test your JSON is properly formed. On the R side you can use jsonlite::fromJSON() or the web-service http://json.parser.online.fr/ can sometimes give more meaningfull error messages.