fork download
  1. library(igraph)
  2.  
  3. # Define the nodes and edges
  4. nodes <- data.frame(
  5. name = c("Alice", "Bob", "CompanyX"),
  6. Profession = c("Engineer", "Doctor", NA),
  7. City = c("New York", "Los Angeles", NA),
  8. Type = c(NA, NA, "Corporation")
  9. )
  10.  
  11. edges <- data.frame(
  12. from = c("Alice", "Bob", "Alice"),
  13. to = c("CompanyX", "CompanyX", "Bob"),
  14. relation = c("works_at", "works_at", "knows")
  15. )
  16.  
  17. # Create the graph
  18. g <- graph_from_data_frame(edges, vertices = nodes, directed = TRUE)
  19.  
  20. # Display node and edge attributes
  21. print("Node Attributes:")
  22. print(V(g))
  23. print("\nEdge Attributes:")
  24. print(E(g))
  25.  
  26. # Plot the graph
  27. plot(
  28. g,
  29. vertex.size = 40,
  30. vertex.label.cex = 1,
  31. edge.label = E(g)$relation,
  32. edge.color = "red",
  33. vertex.label.color = "black",
  34. vertex.color = "lightblue",
  35. main = "Knowledge Graph Visualization"
  36. )
  37.  
Success #stdin #stdout #stderr 0.34s 46512KB
stdin
Standard input is empty
stdout
[1] "Node Attributes:"
+ 3/3 vertices, named:
[1] Alice    Bob      CompanyX
[1] "\nEdge Attributes:"
+ 3/3 edges (vertex names):
[1] Alice->CompanyX Bob  ->CompanyX Alice->Bob     
stderr
Attaching package: ‘igraph’

The following objects are masked from ‘package:stats’:

    decompose, spectrum

The following object is masked from ‘package:base’:

    union