Lucca Ramalho
Lucca Ramalho

Reputation: 593

forceNetwork networkD3 - Arrow issue

I'm working with the package and I've plotted this graph:

forceNetwork(Links = links, 
                      Nodes = nodes,
                      Source = "source", 
                      Target = "target",
                      NodeID = "nome",
                      Group = "tipo",
                      linkColour = links$cor,
                      Nodesize = "freq", 
                      zoom=TRUE,
                      legend = TRUE,
                      colourScale = JS(ColourScale),
                      fontSize = 14,
                      fontFamily = "serif",
                      opacity = 0.8)

enter image description here

Now I've tried to plot the graph with oriented arrows, and I've set the arrows=TRUE, but this error appeared.

enter image description here

any ideas of what's wrong?

here's the head of the data

> head(nodes)
                       nome      tipo freq
1   Adriano Carlos De Moura Doutorado    1
2        Aline Fraiha Paiva  Mestrado    2
3              Almir Cortes Doutorado    1
4 Ana Valéria Ramos Vicente  Mestrado    1
5 André Pessoa Silva Xavier  Mestrado    1
6    Antônio Alves Sobrinho  Mestrado    1


> head(links)
                            autor                 orientador      tipo source target  cor
1               Robert Gomes Melo       Juliano Manabu IYODA  Mestrado     50     87  red
2       Cynthia Campelo Schneider     Arnaldo Daraya Contier  Mestrado     10     64  red
3 Júlio César Fernandes Vila Nova             Nelly Carvalho Doutorado     31     99 blue
4          Vanildo Almeida Mendes       Júlio Cesar de SOUZA  Mestrado     57     89  red
5          Meiriédna Queiroz Mota     Ângela Freire Prysthon  Mestrado     44     60  red
6 Júlio Cesar Fernandes Vila Nova Nelly Medeiros de Carvalho  Mestrado     30    100  red

Upvotes: 0

Views: 1512

Answers (1)

CJ Yetman
CJ Yetman

Reputation: 8848

You need to have a numeric Value column/variable in your Links data frame, and you need to specify it by name with the Value parameter of forceNetwork()...

library(networkD3)

nodes <- read.csv(header = TRUE, text = "
nome,tipo,freq
Adriano Carlos De Moura,Doutorado,1
Aline Fraiha Paiva,Mestrado,2
Almir Cortes,Doutorado,1
Ana Valéria Ramos Vicente,Mestrado,1
André Pessoa Silva Xavier,Mestrado,1
Antônio Alves Sobrinho,Mestrado,1
")

links <- read.csv(header = TRUE, text = "
autor,orientador,tipo,source,target,cor
Robert Gomes Melo,Juliano Manabu IYODA,Mestrado,0,1,red
Cynthia Campelo Schneider,Arnaldo Daraya Contier,Mestrado,2,3,red
Júlio César Fernandes Vila Nova,Nelly Carvalho,Doutorado,4,5,blue
")

links$value = 1

forceNetwork(Links = links, 
             Nodes = nodes,
             Source = "source", 
             Target = "target",
             NodeID = "nome",
             Group = "tipo",
             Value = "value",
             # linkColour = links$cor,
             Nodesize = "freq", 
             zoom=TRUE,
             legend = TRUE,
             # colourScale = JS(ColourScale),
             fontSize = 14,
             fontFamily = "serif",
             opacity = 0.8,
             arrows = TRUE)

Also note, function arguments that do not list a default value in the help file are mandatory (otherwise you get unspecified behavior, or an error). That goes for all functions in R, not just functions. You can get to the help page for forceNetwork() by typing the command ?networkD3::forceNetwork() in the console. You can get to the help page for any command by preceding the function name with a ?.

Below is what the help file shows under the Usage heading near the top. You can also see it here. Notice that all of the following arguments do not have a default value and are therefore mandatory: Links, Nodes, Source, Target, Value, NodeID, Nodesize, Group.

forceNetwork(Links, Nodes, Source, Target, Value, NodeID, Nodesize, Group,
  height = NULL, width = NULL,
  colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);"), fontSize = 7,
  fontFamily = "serif", linkDistance = 50,
  linkWidth = JS("function(d) { return Math.sqrt(d.value); }"),
  radiusCalculation = JS(" Math.sqrt(d.nodesize)+6"), charge = -30,
  linkColour = "#666", opacity = 0.6, zoom = FALSE, legend = FALSE,
  arrows = FALSE, bounded = FALSE, opacityNoHover = 0,
  clickAction = NULL)

The following section, Arguments, lists each argument and gives explicit explanations of what type of objects/values are permitted for each argument. Again, that goes for all R functions, not just functions. This is what it looks like...

Links  a data frame object with the links between the nodes. It should include the Source and Target for each link. These should be numbered starting from 0. An optional Value variable can be included to specify how close the nodes are to one another.

Nodes  a data frame containing the node id and properties of the nodes. If no ID is specified then the nodes must be in the same order as the Source variable column in the Links data frame. Currently only a grouping variable is allowed.

Source  character string naming the network source variable in the Links data frame.

Target  character string naming the network target variable in the Links data frame.

Value  character string naming the variable in the Links data frame for how wide the links are.

NodeID  character string specifying the node IDs in the Nodes data frame.

Nodesize  character string specifying the a column in the Nodes data frame with some value to vary the node radius's with. See also radiusCalculation.

Group  character string specifying the group of each node in the Nodes data frame.

height  numeric height for the network graph's frame area in pixels.

width  numeric width for the network graph's frame area in pixels.

colourScale  character string specifying the categorical colour scale for the nodes. See https://github.com/d3/d3/blob/master/API.md#ordinal-scales.

fontSize  numeric font size in pixels for the node text labels.

fontFamily  font family for the node text labels.

linkDistance  numeric or character string. Either numberic fixed distance between the links in pixels (actually arbitrary relative to the diagram's size). Or a JavaScript function, possibly to weight by Value. For example: linkDistance = JS("function(d){return d.value * 10}").

linkWidth  numeric or character string. Can be a numeric fixed width in pixels (arbitrary relative to the diagram's size). Or a JavaScript function, possibly to weight by Value. The default is linkWidth = JS("function(d) { return Math.sqrt(d.value); }").

radiusCalculation  character string. A javascript mathematical expression, to weight the radius by Nodesize. The default value is radiusCalculation = JS("Math.sqrt(d.nodesize)+6").

charge  numeric value indicating either the strength of the node repulsion (negative value) or attraction (positive value).

linkColour  character vector specifying the colour(s) you want the link lines to be. Multiple formats supported (e.g. hexadecimal).

opacity  numeric value of the proportion opaque you would like the graph elements to be.

zoom  logical value to enable (TRUE) or disable (FALSE) zooming.

legend  logical value to enable node colour legends.

arrows  logical value to enable directional link arrows.

bounded  logical value to enable (TRUE) or disable (FALSE) the bounding box limiting the graph's extent. See http://bl.ocks.org/mbostock/1129492.

opacityNoHover  numeric value of the opacity proportion for node labels text when the mouse is not hovering over them.

clickAction  character string with a JavaScript expression to evaluate when a node is clicked.

Upvotes: 1

Related Questions