Learn R Programming

DiagrammeR (version 0.8.4)

set_node_attrs: Set node attributes

Description

From a graph object of class dgr_graph or a node data frame, set node attribute properties for one or more nodes.

Usage

set_node_attrs(x, node_attr, values, nodes = NULL)

Arguments

x
either a graph object of class dgr_graph that is created using create_graph, or a node data frame.
node_attr
the name of the attribute to set.
values
the values to be set for the chosen attribute for the chosen nodes.
nodes
an optional vector of node IDs for filtering the list of nodes present in the graph.

Value

either a graph object of class dgr_graph or a node data frame, depending on what type of object was supplied to x.

Examples

Run this code
# Create a node data frame (ndf)
nodes <-
  create_nodes(
    nodes = c("a", "b", "c", "d"),
    type = "letter",
    label = TRUE,
    value = c(3.5, 2.6, 9.4, 2.7))

# Create an edge data frame (edf)
edges <-
  create_edges(
    from = c("a", "b", "c"),
    to = c("d", "c", "a"),
    rel = "leading_to")

# Create a graph
graph <-
  create_graph(
    nodes_df = nodes,
    edges_df = edges)

# Set attribute `color = "green"` for nodes `a`
# and `c` using the graph object
graph <-
  set_node_attrs(
    x = graph,
    node_attr = "color",
    values = "green",
    nodes = c("a", "c"))

# Set attribute `color = "green"`` for nodes `a`
# and `c` using the node data frame
nodes <-
  set_node_attrs(
    x = nodes,
    node_attr = "color",
    values = "green",
    nodes = c("a", "c"))

# Set attribute `color = "blue"` for all nodes
# the node data frame
nodes <-
  set_node_attrs(
    x = nodes,
    node_attr = "color",
    values = "blue")

Run the code above in your browser using DataLab