Description
This function assigns colors to edges in an igraph
based on the colors of the source nodes, with an optional transparency adjustment.
Usage
assign_edge_colors(graph, transparency = 0.4)
Arguments
graph | An |
transparency | A numeric value between 0 and 1 indicating the transparency level of the edge colors. Default is 0.4. |
Value
The input graph with updated edge color attributes.
Examples
library(igraph) # Creating a sample graph g <- erdos.renyi.game(10, 0.3) V(g)$name <- letters[1:10] V(g)$color <- rainbow(10) # Assigning edge colors based on source node colors g <- assign_edge_colors(g, transparency = 0.4) # Plotting the graph plot(g, edge.color = E(g)$color, vertex.color = V(g)$color)
Description
This function assigns colors to nodes in a graph based on specified attributes.
Usage
assign_node_colors(graph, attributes, custom_colors = NULL)
Arguments
graph | An |
attributes | A two-column matrix or data frame. The first column must contain node names, and the second column must contain the attributes that colors will be assigned off of. |
custom_colors | A character vector of colors to be used for different attribute values. If not provided, a default palette from |
Value
The input graph with updated vertex color attributes.
Examples
library(igraph) # Creating a sample graph g <- erdos.renyi.game(10, 0.3) V(g)$name <- letters[1:10] # Creating a sample attributes data frame attributes <- data.frame( Node = letters[1:10], Attribute = rep(c("Group1", "Group2", "Group3"), length.out = 10) ) # Assigning node colors using default colors g <- assign_node_colors(g, attributes) # Plotting the graph plot(g, vertex.color = V(g)$color) ##### Example with custom colors #### # Defining custom colors custom_colors <- c("red", "yellow", "pink") # Assigning node colors g <- assign_node_colors(g, attributes, custom_colors) # Plotting the graph plot(g, vertex.color = V(g)$color)
Description
This function provides an easy interface for plotting a graph in a similar style to ‘Gephi’.It has a customizable layout, label size, edge color, vertex size, edge arrow size, and vertex label color.
Usage
easyplot( graph, layout, label_size = 3, edge_color = NULL, vertex_size = rep(3, vcount(graph)), edge_arrow_size = 0.2, vertex_label_color = "black")
Arguments
graph | An |
layout | A matrix representing the layout of the graph. Typically obtained from layout functions like |
label_size | A numeric value indicating the size of the vertex labels. Default is 3. Note that when exporting a plot, label size will likely need to be adjusted depending on the plot size. |
edge_color | A character vector of colors for the edges. If not provided, the default color is black. |
vertex_size | A numeric vector indicating the size of the vertices. Default is 15 for all vertices. |
edge_arrow_size | A numeric value indicating the size of the arrows at the end of the edges. Default is 0.2. |
vertex_label_color | A character string specifying the color of the vertex labels. Default is "black". |
Value
A plot of the graph with the specified parameters.
Examples
library(igraph) # Creating a sample graph g <- erdos.renyi.game(10, 0.3) V(g)$name <- letters[1:10] V(g)$color <- rainbow(10) layout <- layout_with_fr(g) # Plot the graph using easyplot easyplot(g, layout, label_size = 1, vertex_size = rep(10, vcount(g))) # Assign edge colors based on source node colors g <- assign_edge_colors(g, transparency = 0.4) # Plot the graph using easyplot, now with edge color easyplot(g, layout, label_size = 1, vertex_size = rep(10, vcount(g)))
Description
This function applies Jacomy et al. (2014)'s 'ForceAtlas2' layout algorithm to an igraph
object.
Usage
layout.forceatlas2( g, iterations = 100, linlog = FALSE, pos = NULL, gravity = 1, center = NULL, plotstep = 10, plotlabels = TRUE, scalingratio = 10, stronggravity = FALSE, jittertol = 1)
Arguments
g | An |
iterations | Integer. The number of iterations to run the algorithm. Default is 100. |
linlog | Logical. If |
pos | A 2-column matrix of initial positions, where the columns contain x-coordinates and y-coordinates, respectively. If |
gravity | Numeric. The strength of the gravity force. Default is 1. Note that this is only included in calculations if |
center | A numeric vector of length 2 specifying the center of gravity. If |
plotstep | Integer. The number of iterations between plots. If |
plotlabels | Logical. If |
scalingratio | Numeric. The scaling ratio of the layout. Default is 10, in line with ‘Gephi’. |
stronggravity | Logical. If |
jittertol | Numeric. The tolerance for jittering nodes. Default is |
Details
This function implements Jacomy et al. (2014)'s ForceAtlas2 layout algorithm on an igraph
object.It can handle large graphs and is particularly suitable for visualizing networks. It also includes LinLog mode and a stronger gravity feature, like ‘Gephi’.
Value
A matrix of node positions.
References
Jacomy M, Venturini T, Heymann S, Bastian M (2014).“ForceAtlas2, a Continuous Graph Layout Algorithm for Handy Network Visualization Designed for the Gephi Software.”PLoS ONE, 9(6).doi:10.1371/journal.pone.0098679, https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0098679.
Noack A (2009).“Modularity clustering is force-directed layout.”Physical Review, 79.https://arxiv.org/pdf/0807.4052.
Examples
# Create a random graphlibrary(igraph)g <- erdos.renyi.game(100, 0.05)# Assign non-numeric row namesV(g)$name <- paste0("node", 1:vcount(g))# Apply ForceAtlas2 layoutpos <- layout.forceatlas2(g, linlog = TRUE, iterations = 100, scalingratio = 10)plot(g, layout = pos)
Description
This function rotates each node's position by a specified angle.
Usage
rotate_layout(layout, angle)
Arguments
layout | A 2-column matrix or dataframe of position data, where the columns are the x- and y-coordinates of each node, respectively. |
angle | The angle by which to rotate the layout, in degrees. |
Details
This function rotates each node position in a 2-column matrix/dataframe of position data by a specified angle.
Value
A matrix of node positions.
Examples
# Create a random graphlibrary(igraph)g <- erdos.renyi.game(100, 0.05)# Initializing position vector and plottingposition <- as.matrix(data.frame(X = c(1, 2, 3), Y = c(4, 5, 6)))plot(g, layout = position)# Rotating position vector 90 degrees and plottingrotated_df <- rotate_layout(position, 90)plot(g, layout = rotated_df)# Rotating position vector 283 degrees and plottingrotated_df <- rotate_layout(position, 283)plot(g, layout = rotated_df)
Description
This function scales the positions of nodes in a graph relative to their centroid; compatible with expansions and contractions.
Usage
scale_node_positions(layout, scale_factor = 1.2)
Arguments
layout | A 2-column matrix representing the position data, where the columns are the x- and y-coordinates of each node, respectively. |
scale_factor | A numeric value indicating the factor by which to scale the node positions. The default is |
Details
This function expands or contracts the graph around a centroid, facilitating visualization. Factors greater than 1 expand the network around the centroid while those less than 1 contract it around the centroid.scale_factor = 1
is no change. Note that unscaled plot commands like plot(g, layout = layout_expanded)
make the change difficult to see, so scaled plots are recommended for comparison.
Value
A matrix of updated node positions.
Examples
set.seed(10) library(igraph) # Generating graph and setting initial layout g <- erdos.renyi.game(100, 0.05) layout <- layout_with_fr(g) # Plotting original graph, maintaining fixed scale so expansion is clear plot(layout, main="Original Graph", xlab="", ylab="", xlim=c(-20, 15), ylim=c(-20, 15)) # Expanding node positions layout_expanded <- scale_node_positions(layout, 2) # Plotting expanded graph, maintaining fixed scale so expansion is clear plot(layout_expanded, main="Expanded Graph", xlab="", ylab="", xlim=c(-20, 15), ylim=c(-20, 15)) # Contract node positions layout_cont <- scale_node_positions(layout, 0.8) # Plotting contracted graph, maintaining fixed scale so transformation is clear plot(layout_cont, main="Contracted Graph", xlab="", ylab="", xlim=c(-20, 15), ylim=c(-20, 15)) # Note that igraph plots like below make it difficult to see the transformation, # because they are autoscaled. # plot(g, layout = layout_expanded) # The change is easy to see in scaled plots, as shown.