library( igraph)
# Define the nodes and edges
nodes <- data.frame (
name = c( "Alice" , "Bob" , "CompanyX" ) ,
Profession = c( "Engineer" , "Doctor" , NA) ,
City = c( "New York" , "Los Angeles" , NA) ,
Type = c( NA, NA, "Corporation" )
)
edges <- data.frame (
from = c( "Alice" , "Bob" , "Alice" ) ,
to = c( "CompanyX" , "CompanyX" , "Bob" ) ,
relation = c( "works_at" , "works_at" , "knows" )
)
# Create the graph
g <- graph_from_data_frame( edges, vertices = nodes, directed = TRUE)
# Display node and edge attributes
print( "Node Attributes:" )
print( V( g) )
print( "\n Edge Attributes:" )
print( E( g) )
# Plot the graph
plot(
g,
vertex.size = 40 ,
vertex.label .cex = 1 ,
edge.label = E( g) $relation,
edge.color = "red" ,
vertex.label .color = "black" ,
vertex.color = "lightblue" ,
main = "Knowledge Graph Visualization"
)
bGlicmFyeShpZ3JhcGgpCgojIERlZmluZSB0aGUgbm9kZXMgYW5kIGVkZ2VzCm5vZGVzIDwtIGRhdGEuZnJhbWUoCiAgbmFtZSA9IGMoIkFsaWNlIiwgIkJvYiIsICJDb21wYW55WCIpLAogIFByb2Zlc3Npb24gPSBjKCJFbmdpbmVlciIsICJEb2N0b3IiLCBOQSksCiAgQ2l0eSA9IGMoIk5ldyBZb3JrIiwgIkxvcyBBbmdlbGVzIiwgTkEpLAogIFR5cGUgPSBjKE5BLCBOQSwgIkNvcnBvcmF0aW9uIikKKQoKZWRnZXMgPC0gZGF0YS5mcmFtZSgKICBmcm9tID0gYygiQWxpY2UiLCAiQm9iIiwgIkFsaWNlIiksCiAgdG8gPSBjKCJDb21wYW55WCIsICJDb21wYW55WCIsICJCb2IiKSwKICByZWxhdGlvbiA9IGMoIndvcmtzX2F0IiwgIndvcmtzX2F0IiwgImtub3dzIikKKQoKIyBDcmVhdGUgdGhlIGdyYXBoCmcgPC0gZ3JhcGhfZnJvbV9kYXRhX2ZyYW1lKGVkZ2VzLCB2ZXJ0aWNlcyA9IG5vZGVzLCBkaXJlY3RlZCA9IF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