node_count {DiagrammeR} | R Documentation

## Get count of all nodes or certain types of nodes

### Description

From a graph object of class `dgr_graph`, get a count of nodes in the graph and optionally obtain a count of nodes by their type.

### Usage

    node_count(graph, type = FALSE)

### Arguments

`graph` | a graph object of class `dgr_graph` that is created using `create_graph`.
`type` | either a logical value, where `TRUE` provides a named vector of node count by type and `FALSE` (the default) provides a total count, or, a character vector of `type` values to filter the node count.

### Value

a numeric vector of single length.

### Examples

    ## Not run:
    # Before getting counts of nodes, create a simple graph
    nodes <-
      create_nodes(nodes = LETTERS,
                   label = TRUE,
                   type = c(rep("a_to_g", 7),
                            rep("h_to_p", 9),
                            rep("q_to_x", 8),
                            rep("y_and_z",2)))

    edges <-
      create_edges(from = sample(LETTERS, replace = TRUE),
                   to = sample(LETTERS, replace = TRUE),
                   rel = "letter_to_letter")

    graph <-
      create_graph(nodes_df = nodes,
                   edges_df = edges,
                   graph_attrs = "layout = neato",
                   node_attrs = c("fontname = Helvetica",
                                  "shape = circle"))

    # Get counts of nodes grouped by the "type" attribute
    node_count(graph, type = TRUE) # the default
    #> a_to_g  h_to_p  q_to_x y_and_z
    #>      7       9       8       2

    # Get a total count of nodes with no grouping
    node_count(graph, type = FALSE)
    #> [1] 26

    # Get a count of nodes of one or more specified types
    node_count(graph, type = "a_to_g")
    #> [1] 7

    node_count(graph, type = c("a_to_g", "q_to_x"))
    #> [1] 15

    ## End(Not run)

[Package DiagrammeR version 0.8.2 Index]