add_star {DiagrammeR} | R Documentation |
With a graph object of class
dgr_graph
, add a node star to the graph.
add_star(graph, n, type = NULL, label = TRUE, rel = NULL, ...)
graph |
a graph object of class
|
n |
the number of nodes comprising the star. The first node will be the center of the star. |
type |
an optional string that describes the entity type for the nodes to be added. |
label |
either a vector object of length
|
rel |
an optional string for providing a relationship label to all new edges created in the node star. |
... |
optional node attributes supplied as vectors. |
a graph object of class dgr_graph
.
# Create a new graph and add 2 stars of # varying numbers of nodes graph <- create_graph() %>% add_star( n = 4, type = "four_star") %>% add_star( n = 5, type = "five_star") # Get node information from this graph node_info(graph) #> id type label deg indeg outdeg loops #> 1 1 four_star 1 3 0 3 0 #> 2 2 four_star 2 1 1 0 0 #> 3 3 four_star 3 1 1 0 0 #> 4 4 four_star 4 1 1 0 0 #> 5 5 five_star 1 4 0 4 0 #> 6 6 five_star 2 1 1 0 0 #> 7 7 five_star 3 1 1 0 0 #> 8 8 five_star 4 1 1 0 0 #> 9 9 five_star 5 1 1 0 0 # Attributes can be specified in extra # arguments and these are applied in order; # Usually these attributes are applied to # nodes (e.g., `type` is a node attribute) # but the `rel` attribute will apply to the # edges graph_w_attrs <- create_graph() %>% add_star( n = 6, label = c("one", "two", "three", "four", "five", "six"), type = c("a", "a", "b", "b", "c", "c"), value = c(1.2, 8.4, 3.4, 5.2, 6.1, 2.6), rel = "star") # Get the graph's node data frame get_node_df(graph_w_attrs) #> id type label value #> 1 1 a one 1.2 #> 2 2 a two 8.4 #> 3 3 b three 3.4 #> 4 4 b four 5.2 #> 5 5 c five 6.1 #> 6 6 c six 2.6 # Get the graph's edge data frame get_edge_df(graph_w_attrs) #> id from to rel #> 1 1 1 2 star #> 2 2 1 3 star #> 3 3 1 4 star #> 4 4 1 5 star #> 5 5 1 6 star