graph_from_data_frame
时间: 2023-09-15 17:16:42 浏览: 143
graphframes
`graph_from_data_frame()` is a function in the `igraph` package in R that creates a graph object from a data frame. The data frame should contain two columns, representing the source and target nodes of each edge in the graph. The function can also take additional arguments to specify the type of graph to create (e.g. directed or undirected), the attribute names to use for edge weights, and the vertex labels.
Here's an example of how to use `graph_from_data_frame()`:
```r
library(igraph)
# create a data frame of edges
edges <- data.frame(
source = c("A", "B", "C", "D"),
target = c("B", "C", "D", "A")
)
# create an undirected graph from the data frame
g <- graph_from_data_frame(edges, directed = FALSE)
# plot the graph
plot(g, vertex.label.color = "red", vertex.size = 30)
```
This will create an undirected graph with four vertices labeled A, B, C, and D, and four edges connecting them in a cycle. The `plot()` function is used to visualize the graph, with vertex label color set to red and vertex size set to 30.
Note that the `graph_from_data_frame()` function can also take additional arguments to specify vertex attributes, edge attributes, and other properties of the graph. See the `igraph` documentation for more details.
阅读全文