R Language Igraph Package-build Network Diagram

Source: Internet
Author: User

Igaph is a project, the goal is to build a simple, easy-to-use network analysis tools, have R, Python, C + + and other languages of the specific implementation;

Project homepage:

http://igraph.org/

In the R language, the corresponding is igraph this R package

Installation:

Install.packages ("Igraph")

Use:

For graph graph Such data structure, the most basic element includes node and edge (connection between nodes, Edge);

Igraph this R package provides many ways to create graph, let's look at one of the most basic examples and create a graph from a data frame.

The graph we are going to create is as follows:

As you can see, this graph consists of 5 nodes, first creating a data frame, each row in this data frame is a node, each column is a node attribute

The code is as follows:

> Actors <-data.frame (Name=c ("Alice", "Bob", "Cecil", "David",                            "Esmeralda"),                     age=c (48,33,45,34,21),                     Gender=c ("F", "M", "F", "M", "F") > Actors       name age Gender1     Alice      F2       Bob      M3     Cecil      F4     David      M5 Esmeralda      F

Actors this data frame holds information about all the nodes in the graph, a total of 5 nodes, with name, age, gender 3 attributes; Note that the properties of the node are custom

Next, create another data frame, save the edge information,

The code is as follows:

> Relations <-data.frame (from=c ("Bob", "Cecil", "Cecil", "David", +                                "David", "Esmeralda"), +                         to=c ("Alice "," Bob "," Alice "," Alice "," Bob "," Alice "), +                         same.dept=c (false,false,true,false,false,true), +                         Friendship=c (4,5,5,2,1,1), Advice=c (4,5,5,4,2,3)) > Relations from to same.dept Friendship Advice1       Bob Alice     False          4     Cecil   Bob     false          5     Cecil Alice      TRUE          5      54     David Alice     False          2     David   Bob     false          1      Esmeralda Alice      TRUE          1      3

The relations data frame holds the connection information between the nodes, from and to two, which describes which node the edge is from and to which, and the last 3 columns are custom properties for each edge

Once the two data frames have been created, you can create a graph with the Graph_from_data_frame function, with the following code:

G <-graph_from_data_frame (relations, directed=true, vertices=actors)

The first parameter is the data frame that holds the edge information, the directed parameter controls whether the graph has direction, and the vertices parameter is the data frame that holds the node information.

Finally, a simple visualization

Plot (P)

You can get a diagram like this:

Above is just the most basic visualization example, for graph, there are directed graph and undirected graph two, in the visualization, the most important thing is to calculate the position of each node, for different types of graph, there are different automated layout algorithm

Resources:

https://rdatamining.wordpress.com/2012/05/17/an-example-of-social-network-analysis-with-r-using-package-igraph/

R Language Igraph Package-build Network Diagram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.