Apply position information for a single node. This is done by setting the x
and y attrs for a node id or node label supplied in node. When
rendering the graph, nodes with attribute values set for x and y will be
fixed to those positions on the graph canvas.
Arguments
- graph
A graph object of class
dgr_graph.- node
A single-length vector containing either a node ID value (integer) or a node label (character) for which position information should be applied.
- x
The x coordinate to set for the node.
- y
The y coordinate to set for the node.
- use_labels
An option to use a node
labelvalue innode. Note that this is only possible if all nodes have distinctlabelvalues set and none exist as an NA value.
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(),
rescale_node_attrs(),
set_node_attr_to_display(),
set_node_attr_w_fcn(),
set_node_attrs(),
set_node_attrs_ws()
Examples
# Create a simple graph with 4 nodes
graph <-
create_graph() %>%
add_node(label = "one") %>%
add_node(label = "two") %>%
add_node(label = "three") %>%
add_node(label = "four")
# Add position information to each of
# the graph's nodes
graph <-
graph %>%
set_node_position(
node = 1,
x = 1, y = 1) %>%
set_node_position(
node = 2,
x = 2, y = 2) %>%
set_node_position(
node = 3,
x = 3, y = 3) %>%
set_node_position(
node = 4,
x = 4, y = 4)
# View the graph's node data frame to
# verify that the `x` and `y` node
# attributes are available and set to
# the values provided
graph %>% get_node_df()
#> id type label x y
#> 1 1 <NA> one 1 1
#> 2 2 <NA> two 2 2
#> 3 3 <NA> three 3 3
#> 4 4 <NA> four 4 4
# The same function can modify the data
# in the `x` and `y` attributes
graph <-
graph %>%
set_node_position(
node = 1,
x = 1, y = 4) %>%
set_node_position(
node = 2,
x = 3, y = 3) %>%
set_node_position(
node = 3,
x = 3, y = 2) %>%
set_node_position(
node = 4,
x = 4, y = 1)
# View the graph's node data frame
graph %>% get_node_df()
#> id type label x y
#> 1 1 <NA> one 1 4
#> 2 2 <NA> two 3 3
#> 3 3 <NA> three 3 2
#> 4 4 <NA> four 4 1
# Position changes can also be made by
# supplying a node `label` value (and setting
# `use_labels` to TRUE). For this to work,
# all `label` values in the graph's ndf must
# be unique and non-NA
graph <-
graph %>%
set_node_position(
node = "one",
x = 1, y = 1,
use_labels = TRUE) %>%
set_node_position(
node = "two",
x = 2, y = 2,
use_labels = TRUE)
# View the graph's node data frame
graph %>% get_node_df()
#> id type label x y
#> 1 1 <NA> one 1 1
#> 2 2 <NA> two 2 2
#> 3 3 <NA> three 3 2
#> 4 4 <NA> four 4 1