rescale_node_attrs: Rescale numeric node attribute values

View source: R/rescale_node_attrs.R

rescale_node_attrsR Documentation

Rescale numeric node attribute values

Description

From a graph object of class dgr_graph, take a set of numeric values for a node attribute, rescale to a new numeric or color range, then write to the same node attribute or to a new node attribute column.

Usage

rescale_node_attrs(
  graph,
  node_attr_from,
  to_lower_bound = 0,
  to_upper_bound = 1,
  node_attr_to = NULL,
  from_lower_bound = NULL,
  from_upper_bound = NULL
)

Arguments

graph

A graph object of class dgr_graph.

node_attr_from

The node attribute containing numeric data that is to be rescaled to new numeric or color values.

to_lower_bound

The lower bound value for the set of rescaled values. This can be a numeric value or an X11 color name.

to_upper_bound

The upper bound value for the set of rescaled values. This can be a numeric value or an X11 color name.

node_attr_to

An optional name of a new node attribute to which the recoded values will be applied. This will retain the original node attribute and its values.

from_lower_bound

An optional, manually set lower bound value for the rescaled values. If not set, the minimum value from the set will be used.

from_upper_bound

An optional, manually set upper bound value for the rescaled values. If not set, the minimum value from the set will be used.

Value

A graph object of class dgr_graph.

See Also

Other node creation and removal: add_n_node_clones(), add_n_nodes(), add_n_nodes_ws(), add_node(), add_node_clones_ws(), add_node_df(), add_nodes_from_df_cols(), add_nodes_from_table(), colorize_node_attrs(), copy_node_attrs(), create_node_df(), delete_node(), delete_nodes_ws(), drop_node_attrs(), join_node_attrs(), layout_nodes_w_string(), mutate_node_attrs(), mutate_node_attrs_ws(), node_data(), recode_node_attrs(), rename_node_attrs(), set_node_attr_to_display(), set_node_attr_w_fcn(), set_node_attrs(), set_node_attrs_ws(), set_node_position()

Examples

# Create a random graph using the
# `add_gnm_graph()` function
graph <-
  create_graph() %>%
  add_gnm_graph(
    n = 5,
    m = 10,
    set_seed = 23) %>%
  set_node_attrs(
    node_attr = value,
    values = rnorm(
      n = count_nodes(.),
      mean = 5,
      sd = 1) %>% round(1))

# Get the graph's internal ndf
# to show which node attributes
# are available
graph %>% get_node_df()

# Rescale the `value` node
# attribute, so that its values
# are rescaled between 0 and 1
graph <-
  graph %>%
  rescale_node_attrs(
    node_attr_from = value,
    to_lower_bound = 0,
    to_upper_bound = 1)

# Get the graph's internal ndf
# to show that the node attribute
# values had been rescaled
graph %>% get_node_df()

# Scale the values in the `value`
# node attribute to different
# shades of gray for the `fillcolor`
# and `fontcolor` node attributes
graph <-
  graph %>%
  rescale_node_attrs(
    node_attr_from = value,
    to_lower_bound = "gray80",
    to_upper_bound = "gray20",
    node_attr_to = fillcolor) %>%
  rescale_node_attrs(
    node_attr_from = value,
    to_lower_bound = "gray5",
    to_upper_bound = "gray95",
    node_attr_to = fontcolor)

# Get the graph's internal ndf
# once more to show that scaled
# grayscale colors are now available
# in the `fillcolor` and `fontcolor`
# node attributes
graph %>% get_node_df()


DiagrammeR documentation built on June 22, 2024, 11:21 a.m.