Add new edges in the opposite directions of a selection of edges
Source:R/add_reverse_edges_ws.R
add_reverse_edges_ws.Rd
Add edges in the opposite direction of one or more edges available as an edge
selection in a graph object of class dgr_graph
. New graph edges have the
opposite edge definitions as those in the selection. For example, a graph
with the edge 1->2
in its active selection will gain a new 2->1
edge.
There is also the option to assign a common rel
grouping to the newly
created edges. Upon addition of the edges, the edge selection will be
retained for further selection or traversal operations.
This function makes use of an active selection of edges (and the function
ending with _ws
hints at this).
Selections of edges can be performed using the following selection
(select_*()
) functions: select_edges()
, select_last_edges_created()
,
select_edges_by_edge_id()
, or select_edges_by_node_id()
.
Selections of edges can also be performed using the following traversal
(trav_*()
) functions: trav_out_edge()
, trav_in_edge()
,
trav_both_edge()
, or trav_reverse_edge()
.
Arguments
- graph
A graph object of class
dgr_graph
.- rel
An optional string to apply a
rel
attribute to all newly created edges.- edge_aes
An optional list of named vectors comprising edge aesthetic attributes. The helper function
edge_aes()
is strongly recommended for use here as it contains arguments for each of the accepted edge aesthetic attributes (e.g.,shape
,style
,penwidth
,color
).- edge_data
An optional list of named vectors comprising edge data attributes. The helper function
edge_data()
is strongly recommended for use here as it helps bind data specifically to the created edges.
See also
Other edge creation and removal:
add_edge()
,
add_edge_clone()
,
add_edge_df()
,
add_edges_from_table()
,
add_edges_w_string()
,
add_forward_edges_ws()
,
copy_edge_attrs()
,
create_edge_df()
,
delete_edge()
,
delete_edges_ws()
,
delete_loop_edges_ws()
,
drop_edge_attrs()
,
edge_data()
,
join_edge_attrs()
,
mutate_edge_attrs()
,
mutate_edge_attrs_ws()
,
recode_edge_attrs()
,
rename_edge_attrs()
,
rescale_edge_attrs()
,
rev_edge_dir()
,
rev_edge_dir_ws()
,
set_edge_attr_to_display()
,
set_edge_attrs()
,
set_edge_attrs_ws()
Examples
# Create an empty graph, add 2 nodes to it,
# and create the edge `1->2`
graph <-
create_graph() %>%
add_n_nodes(
n = 2,
type = "type_a",
label = c("a_1", "a_2")) %>%
add_edge(
from = 1,
to = 2,
rel = "a")
# Get the graph's edges
graph %>% get_edge_ids()
#> [1] 1
# Select the edge and create 2 additional edges
# with the opposite definition of `1->2`, which
# is `2->1`; also, apply, different `rel` values
# (`b` and `c`)
graph <-
graph %>%
select_edges() %>%
add_reverse_edges_ws(rel = "b") %>%
add_reverse_edges_ws(rel = "c") %>%
clear_selection()
# Get the graph's edge data frame
graph %>% get_edge_df()
#> id from to rel
#> 1 1 1 2 a
#> 2 2 2 1 b
#> 3 3 2 1 c