christophergandrud / networkD3

D3 JavaScript Network Graphs from R
http://christophergandrud.github.io/networkD3
649 stars 270 forks source link

Not an issue. Need help! #254

Closed anukadur closed 5 years ago

anukadur commented 5 years ago

I have 2 dataframes df1, df2 with columns TERM,ACRONYM,DEFINITION from 2 sources S1,S2. I am trying to use force collapsible graph in Shiny to display this. The expected output S>TERM>ACRONYM>DEFINITION. I am unable to understand how to create nodes and links for the below syntax.

forceNetwork(Links = links, Nodes = nodes, Source = "source", Target = "target", Value = 'n', NodeID = 'name', Group = 'group')

cjyetman commented 5 years ago

You’ll have to be more specific. I don’t understand what you’re trying to achieve.

anukadur commented 5 years ago

The search term is the input from the shiny app. This search term is passed to the 2 data sources to get the relevant results. The output from these 2 datasources are the dataframes with columns TERM,ACRONYM,DEFINITION. I am trying to display the output as below. I am unable to understand how the links, nodes,source,target arguments should be defined.

image

cjyetman commented 5 years ago

Please read the help files. They are very explicit about what each argument is for and what can be passed to it...

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.

anukadur commented 5 years ago

Thanks for the quick response. I read it. I am a newbie. It must a simple question to you. My dataframes doesn't have links and nodes information. Should I create links and nodes dataframes? Like below? Source <- c("INPUT","TERM", "ACRONYM", "DEFINITION") Target <- c("TERM", "ACRONYM", "DEFINITION","INPUT") Data <- data.frame(Source, Target)

cjyetman commented 5 years ago

In the image you posted, the “nodes” are the blue ovals, so you need to create a nodes data frame with one column/variable that has the name of each node.

The arrows in your image are “links”. You need a data frame that defines each one of those. There should be at least a source and a target column/variable. The source value for each link is the numeric index (0-based indexing, first index = 0) of the node in your nodes data frame where the arrow begins. The target value for each link is the numeric index (0-based indexing, first index = 0) of the node in your nodes data frame where the arrow ends.

anukadur commented 5 years ago

Thank you :) I created links=data.frame(source=c(0,1,2),target=c(1,2,3)) and nodes=data.frame(name=c("INPUT","TERM","ACRONYM","DEFINITION"),group = 1). Nodes dataframe just has the column names of the dataframe I have now. How does it access the data in the dataframe to create this graph? How do we map to the dataframe?